Code Duplication    Length = 39-61 lines in 3 locations

src/Handler/AddCouponHandler.php 1 location

@@ 16-76 (lines=61) @@
13
use Sylius\ShopApiPlugin\Command\AddCoupon;
14
use Webmozart\Assert\Assert;
15
16
final class AddCouponHandler
17
{
18
    /**
19
     * @var OrderRepositoryInterface
20
     */
21
    private $orderRepository;
22
23
    /**
24
     * @var PromotionCouponRepositoryInterface
25
     */
26
    private $couponRepository;
27
28
    /**
29
     * @var OrderProcessorInterface
30
     */
31
    private $orderProcessor;
32
33
    /**
34
     * @var PromotionCouponEligibilityCheckerInterface
35
     */
36
    private $couponEligibilityChecker;
37
38
    /**
39
     * @param OrderRepositoryInterface $orderRepository
40
     * @param PromotionCouponRepositoryInterface $couponRepository
41
     * @param OrderProcessorInterface $orderProcessor
42
     * @param PromotionCouponEligibilityCheckerInterface $couponEligibilityChecker
43
     */
44
    public function __construct(
45
        OrderRepositoryInterface $orderRepository,
46
        PromotionCouponRepositoryInterface $couponRepository,
47
        OrderProcessorInterface $orderProcessor,
48
        PromotionCouponEligibilityCheckerInterface $couponEligibilityChecker
49
    ) {
50
        $this->orderRepository = $orderRepository;
51
        $this->couponRepository = $couponRepository;
52
        $this->orderProcessor = $orderProcessor;
53
        $this->couponEligibilityChecker = $couponEligibilityChecker;
54
    }
55
56
    /**
57
     * @param AddCoupon $addCoupon
58
     */
59
    public function handle(AddCoupon $addCoupon)
60
    {
61
        /** @var OrderInterface $cart */
62
        $cart = $this->orderRepository->findOneBy(['tokenValue' => $addCoupon->orderToken()]);
63
64
        Assert::notNull($cart, sprintf('Cart with token %s has not been found.', $addCoupon->orderToken()));
65
66
        /** @var PromotionCouponInterface $coupon */
67
        $coupon = $this->couponRepository->findOneBy(['code' => $addCoupon->couponCode()]);
68
69
        Assert::notNull($coupon, sprintf('Coupon with code %s has not been found.', $addCoupon->couponCode()));
70
        Assert::true($this->couponEligibilityChecker->isEligible($cart, $coupon));
71
72
        $cart->setPromotionCoupon($coupon);
73
74
        $this->orderProcessor->process($cart);
75
    }
76
}
77

src/Handler/ChangeItemQuantityHandler.php 1 location

@@ 16-73 (lines=58) @@
13
use Sylius\ShopApiPlugin\Command\ChangeItemQuantity;
14
use Webmozart\Assert\Assert;
15
16
final class ChangeItemQuantityHandler
17
{
18
    /**
19
     * @var OrderItemRepositoryInterface
20
     */
21
    private $orderItemRepository;
22
23
    /**
24
     * @var OrderRepositoryInterface
25
     */
26
    private $orderRepository;
27
28
    /**
29
     * @var OrderItemQuantityModifierInterface
30
     */
31
    private $orderItemModifier;
32
33
    /**
34
     * @var OrderProcessorInterface
35
     */
36
    private $orderProcessor;
37
38
    /**
39
     * @param OrderItemRepositoryInterface $orderItemRepository
40
     * @param OrderRepositoryInterface $orderRepository
41
     * @param OrderItemQuantityModifierInterface $orderItemModifier
42
     * @param OrderProcessorInterface $orderProcessor
43
     */
44
    public function __construct(
45
        OrderItemRepositoryInterface $orderItemRepository,
46
        OrderRepositoryInterface $orderRepository,
47
        OrderItemQuantityModifierInterface $orderItemModifier,
48
        OrderProcessorInterface $orderProcessor
49
    ) {
50
        $this->orderItemRepository = $orderItemRepository;
51
        $this->orderRepository = $orderRepository;
52
        $this->orderItemModifier = $orderItemModifier;
53
        $this->orderProcessor = $orderProcessor;
54
    }
55
56
    public function handle(ChangeItemQuantity $changeItemQuantity)
57
    {
58
        /** @var OrderInterface $order */
59
        $order = $this->orderRepository->findOneBy(['tokenValue' => $changeItemQuantity->orderToken(), 'state' => OrderInterface::STATE_CART]);
60
61
        Assert::notNull($order, sprintf('Cart with %s token has not been found.', $changeItemQuantity->orderToken()));
62
63
        /** @var OrderItemInterface $orderItem */
64
        $orderItem = $this->orderItemRepository->find($changeItemQuantity->itemIdentifier());
65
66
        Assert::notNull($orderItem, 'Cart item has not been found.');
67
        Assert::true($order->hasItem($orderItem), sprintf('Cart item with %s id does not exists', $changeItemQuantity->itemIdentifier()));
68
69
        $this->orderItemModifier->modify($orderItem, $changeItemQuantity->quantity());
70
71
        $this->orderProcessor->process($order);
72
    }
73
}
74

src/Handler/RemoveItemFromCartHandler.php 1 location

@@ 15-53 (lines=39) @@
12
use Sylius\ShopApiPlugin\Command\RemoveItemFromCart;
13
use Webmozart\Assert\Assert;
14
15
final class RemoveItemFromCartHandler
16
{
17
    /** @var OrderItemRepositoryInterface */
18
    private $orderItemRepository;
19
20
    /** @var OrderRepositoryInterface */
21
    private $orderRepository;
22
23
    /** @var OrderProcessorInterface */
24
    private $orderProcessor;
25
26
    public function __construct(
27
        OrderItemRepositoryInterface $orderItemRepository,
28
        OrderRepositoryInterface $orderRepository,
29
        OrderProcessorInterface $orderProcessor
30
    ) {
31
        $this->orderItemRepository = $orderItemRepository;
32
        $this->orderRepository = $orderRepository;
33
        $this->orderProcessor = $orderProcessor;
34
    }
35
36
    public function handle(RemoveItemFromCart $command): void
37
    {
38
        $order = $this->orderRepository->findOneBy(['tokenValue' => $command->orderToken(), 'state' => OrderInterface::STATE_CART]);
39
40
        Assert::notNull($order, sprintf('Cart with %s token has not been found.', $command->orderToken()));
41
42
        /** @var OrderItemInterface $orderItem */
43
        $orderItem = $this->orderItemRepository->find($command->itemIdentifier());
44
45
        Assert::notNull($orderItem, 'Cart item has not been found.');
46
        Assert::true($order->hasItem($orderItem), sprintf('Cart item with %s id does not exists', $command->itemIdentifier()));
47
48
        $order->removeItem($orderItem);
49
        $this->orderItemRepository->remove($orderItem);
50
51
        $this->orderProcessor->process($order);
52
    }
53
}
54