|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Paweł Jędrzejewski |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Sylius\Bundle\ApiBundle\CommandHandler\Cart; |
| 15 | + |
| 16 | +use Sylius\Bundle\ApiBundle\Command\Cart\ChangeItemQuantityInCart; |
| 17 | +use Sylius\Component\Core\Model\OrderInterface; |
| 18 | +use Sylius\Component\Core\Model\OrderItemInterface; |
| 19 | +use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface; |
| 20 | +use Sylius\Component\Order\Processor\OrderProcessorInterface; |
| 21 | +use Sylius\Component\Order\Repository\OrderItemRepositoryInterface; |
| 22 | +use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
| 23 | +use Webmozart\Assert\Assert; |
| 24 | + |
| 25 | +final class ChangeItemQuantityInCartHandler implements MessageHandlerInterface |
| 26 | +{ |
| 27 | + /** @var OrderItemRepositoryInterface */ |
| 28 | + private $orderItemRepository; |
| 29 | + |
| 30 | + /** @var OrderItemQuantityModifierInterface */ |
| 31 | + private $orderItemQuantityModifier; |
| 32 | + |
| 33 | + /** @var OrderProcessorInterface */ |
| 34 | + private $orderProcessor; |
| 35 | + |
| 36 | + public function __construct( |
| 37 | + OrderItemRepositoryInterface $orderItemRepository, |
| 38 | + OrderItemQuantityModifierInterface $orderItemQuantityModifier, |
| 39 | + OrderProcessorInterface $orderProcessor |
| 40 | + ) { |
| 41 | + $this->orderItemRepository = $orderItemRepository; |
| 42 | + $this->orderItemQuantityModifier = $orderItemQuantityModifier; |
| 43 | + $this->orderProcessor = $orderProcessor; |
| 44 | + } |
| 45 | + |
| 46 | + public function __invoke(ChangeItemQuantityInCart $command): OrderInterface |
| 47 | + { |
| 48 | + /** @var OrderItemInterface|null $orderItem */ |
| 49 | + $orderItem = $this->orderItemRepository->findOneByIdAndCartTokenValue( |
| 50 | + $command->orderItemId, |
| 51 | + $command->orderTokenValue |
| 52 | + ); |
| 53 | + |
| 54 | + Assert::notNull($orderItem); |
| 55 | + |
| 56 | + /** @var OrderInterface $cart */ |
| 57 | + $cart = $orderItem->getOrder(); |
| 58 | + |
| 59 | + Assert::same($cart->getTokenValue(), $command->orderTokenValue); |
| 60 | + |
| 61 | + $this->orderItemQuantityModifier->modify($orderItem, $command->newQuantity); |
| 62 | + $this->orderProcessor->process($cart); |
| 63 | + |
| 64 | + return $cart; |
| 65 | + } |
| 66 | +} |
0 commit comments