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