Skip to content

Commit

Permalink
OP-291: Refactor, simplify processing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hmfilar committed Jul 22, 2024
1 parent 1984c2d commit 6561922
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 62 deletions.
36 changes: 3 additions & 33 deletions spec/Checker/ProductProcessingCheckerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@

final class ProductProcessingCheckerSpec extends ObjectBehavior
{
public function let(
ProductQuantityCheckerInterface $productQuantityChecker,
): void {
$this->beConstructedWith(
$productQuantityChecker,
);
}

public function it_is_initializable(): void
{
$this->shouldHaveType(ProductProcessingChecker::class);
Expand All @@ -39,44 +31,22 @@ public function it_can_be_processed(
WishlistItem $wishlistProduct,
AddToCartCommandInterface $addToCartCommand,
OrderItemInterface $orderItem,
ProductQuantityCheckerInterface $productQuantityChecker,
): void {
$wishlistProduct->getCartItem()->willReturn($addToCartCommand);
$addToCartCommand->getCartItem()->willReturn($orderItem);
$wishlistProduct->getOrderItemQuantity()->willReturn(5);
$productQuantityChecker->hasPositiveQuantity($orderItem)->willReturn(true);
$orderItem->getQuantity()->willReturn(5);

$this->canBeProcessed($wishlistProduct)->shouldReturn(true);
}

public function it_can_not_be_processed_due_to_lack_in_stock(
WishlistItem $wishlistProduct,
AddToCartCommandInterface $addToCartCommand,
OrderItemInterface $orderItem,
ProductQuantityCheckerInterface $productQuantityChecker,
FlashBagInterface $flashBag,
TranslatorInterface $translator,
): void {
$wishlistProduct->getCartItem()->willReturn($addToCartCommand);
$addToCartCommand->getCartItem()->willReturn($orderItem);
$wishlistProduct->getOrderItemQuantity()->willReturn(5);
$productQuantityChecker->hasPositiveQuantity($orderItem)->willReturn(false);

$this->canBeProcessed($wishlistProduct)->shouldReturn(false);
}

public function it_can_not_be_processed_due_to_lack_in_quantity(
public function it_cannot_be_processed(
WishlistItem $wishlistProduct,
AddToCartCommandInterface $addToCartCommand,
OrderItemInterface $orderItem,
ProductQuantityCheckerInterface $productQuantityChecker,
FlashBagInterface $flashBag,
TranslatorInterface $translator,
): void {
$wishlistProduct->getCartItem()->willReturn($addToCartCommand);
$addToCartCommand->getCartItem()->willReturn($orderItem);
$wishlistProduct->getOrderItemQuantity()->willReturn(0);
$productQuantityChecker->hasPositiveQuantity($orderItem)->willReturn(false);
$orderItem->getQuantity()->willReturn(0);

$this->canBeProcessed($wishlistProduct)->shouldReturn(false);
}
Expand Down
7 changes: 0 additions & 7 deletions spec/CommandHandler/Wishlist/CreateNewWishlistHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace spec\BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;

use BitBag\SyliusWishlistPlugin\Checker\WishlistNameCheckerInterface;
use BitBag\SyliusWishlistPlugin\Command\Wishlist\CreateNewWishlist;
use BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist\CreateNewWishlistHandler;
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
Expand All @@ -35,7 +34,6 @@ public function let(
WishlistFactoryInterface $wishlistFactory,
WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver,
ChannelRepositoryInterface $channelRepository,
WishlistNameCheckerInterface $wishlistNameChecker,
TokenUserResolverInterface $tokenUserResolver,
): void {
$this->beConstructedWith(
Expand All @@ -44,7 +42,6 @@ public function let(
$wishlistFactory,
$wishlistCookieTokenResolver,
$channelRepository,
$wishlistNameChecker,
$tokenUserResolver,
);
}
Expand All @@ -59,7 +56,6 @@ public function it_creates_new_wishlist_for_user(
TokenStorageInterface $tokenStorage,
WishlistFactoryInterface $wishlistFactory,
WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver,
WishlistNameCheckerInterface $wishlistNameChecker,
ChannelRepositoryInterface $channelRepository,
TokenInterface $token,
ShopUserInterface $shopUser,
Expand All @@ -85,7 +81,6 @@ public function it_creates_new_wishlist_for_user(
$wishlist->setChannel($channel)->shouldBeCalled();

$existingWishlist->getName()->willReturn('existing');
$wishlistNameChecker->check('existing', 'New wishlist')->willReturn(false);
$wishlist->setName('New wishlist');

$wishlistRepository->add($wishlist)->shouldBeCalled();
Expand Down Expand Up @@ -132,7 +127,6 @@ public function it_doesnt_add_duplicated_wishlist_name_for_user(
TokenStorageInterface $tokenStorage,
WishlistFactoryInterface $wishlistFactory,
WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver,
WishlistNameCheckerInterface $wishlistNameChecker,
ChannelRepositoryInterface $channelRepository,
TokenInterface $token,
ShopUserInterface $shopUser,
Expand All @@ -158,7 +152,6 @@ public function it_doesnt_add_duplicated_wishlist_name_for_user(
$wishlist->setChannel($channel)->shouldBeCalled();

$existingWishlist->getName()->willReturn('existing');
$wishlistNameChecker->check('existing', 'existing')->willReturn(true);
$wishlist->setName('existing')->shouldNotBeCalled();

$wishlistRepository->add($wishlist)->shouldNotBeCalled();
Expand Down
16 changes: 1 addition & 15 deletions src/Checker/ProductProcessingChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@

final class ProductProcessingChecker implements ProductProcessingCheckerInterface
{
public function __construct(
private readonly ProductQuantityCheckerInterface $productQuantityChecker,
) {
}

public function canBeProcessed(WishlistItemInterface $wishlistItem): bool
{
/** @var ?AddToCartCommandInterface $addToCartCommand */
Expand All @@ -32,15 +27,6 @@ public function canBeProcessed(WishlistItemInterface $wishlistItem): bool

$cartItem = $addToCartCommand->getCartItem();

return $this->isInStock($wishlistItem) && $this->productQuantityChecker->hasPositiveQuantity($cartItem);
}

private function isInStock(WishlistItemInterface $wishlistItem): bool
{
if (0 < $wishlistItem->getOrderItemQuantity()) {
return true;
}

return false;
return 0 < $cartItem->getQuantity();
}
}
3 changes: 3 additions & 0 deletions src/Checker/ProductQuantityChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

use Sylius\Component\Order\Model\OrderItemInterface;

/**
* @deprecated
*/
final class ProductQuantityChecker implements ProductQuantityCheckerInterface
{
public function hasPositiveQuantity(OrderItemInterface $product): bool
Expand Down
3 changes: 3 additions & 0 deletions src/Checker/ProductQuantityCheckerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

use Sylius\Component\Order\Model\OrderItemInterface;

/**
* @deprecated
*/
interface ProductQuantityCheckerInterface
{
public function hasPositiveQuantity(OrderItemInterface $product): bool;
Expand Down
3 changes: 3 additions & 0 deletions src/Checker/WishlistNameChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace BitBag\SyliusWishlistPlugin\Checker;

/**
* @deprecated
*/
final class WishlistNameChecker implements WishlistNameCheckerInterface
{
public function check(string $existingWishlistName, string $wishlistToCreate): bool
Expand Down
3 changes: 3 additions & 0 deletions src/Checker/WishlistNameCheckerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace BitBag\SyliusWishlistPlugin\Checker;

/**
* @deprecated
*/
interface WishlistNameCheckerInterface
{
public function check(string $existingWishlistName, string $wishlistToCreate): bool;
Expand Down
4 changes: 1 addition & 3 deletions src/CommandHandler/Wishlist/CreateNewWishlistHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;

use BitBag\SyliusWishlistPlugin\Checker\WishlistNameCheckerInterface;
use BitBag\SyliusWishlistPlugin\Command\Wishlist\CreateNewWishlist;
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
use BitBag\SyliusWishlistPlugin\Exception\WishlistNameIsTakenException;
Expand All @@ -34,7 +33,6 @@ public function __construct(
private WishlistFactoryInterface $wishlistFactory,
private WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver,
private ChannelRepositoryInterface $channelRepository,
private WishlistNameCheckerInterface $wishlistNameChecker,
private TokenUserResolverInterface $tokenUserResolver,
) {
}
Expand Down Expand Up @@ -73,7 +71,7 @@ public function __invoke(CreateNewWishlist $createNewWishlist): int
} else {
/** @var WishlistInterface $newWishlist */
foreach ($wishlists as $newWishlist) {
if (!$this->wishlistNameChecker->check((string) $newWishlist->getName(), $createNewWishlist->getName())) {
if ((string) $newWishlist->getName() !== $createNewWishlist->getName()) {
$wishlist->setName($createNewWishlist->getName());
} else {
throw new WishlistNameIsTakenException();
Expand Down
4 changes: 1 addition & 3 deletions src/Resources/config/services/checker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

<service id="bitbag_sylius_wishlist_plugin.services.product_quantity_checker" class="BitBag\SyliusWishlistPlugin\Checker\ProductQuantityChecker"/>

<service id="bitbag_sylius_wishlist_plugin.services.product_processing_checker" class="BitBag\SyliusWishlistPlugin\Checker\ProductProcessingChecker">
<argument type="service" id="bitbag_sylius_wishlist_plugin.services.product_quantity_checker"/>
</service>
<service id="bitbag_sylius_wishlist_plugin.services.product_processing_checker" class="BitBag\SyliusWishlistPlugin\Checker\ProductProcessingChecker"/>
</services>
</container>
1 change: 0 additions & 1 deletion src/Resources/config/services/message_handler.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@
<argument type="service" id="bitbag_sylius_wishlist_plugin.factory.wishlist"/>
<argument type="service" id="bitbag_sylius_wishlist_plugin.resolver.wishlist_cookie_token_resolver"/>
<argument type="service" id="sylius.repository.channel"/>
<argument type="service" id="bitbag_sylius_wishlist_plugin.checker.wishlist_name_checker"/>
<argument type="service" id="bitbag_sylius_wishlist_plugin.resolver.token_user_resolver"/>
<tag name="messenger.message_handler"/>
</service>
Expand Down

0 comments on commit 6561922

Please sign in to comment.