From 91bd4662e9b438ca8b0a0f96be6626757b474460 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Tue, 23 Jul 2019 13:49:57 +0200 Subject: [PATCH] [Cart] Refactor cart actions to use command providers --- src/Command/Cart/AddCoupon.php | 4 +- src/Command/Cart/AddressOrder.php | 3 +- src/Command/Cart/ChangeItemQuantity.php | 3 +- src/Command/Cart/ChoosePaymentMethod.php | 4 +- src/Command/Cart/ChooseShippingMethod.php | 4 +- src/Command/Cart/CompleteOrder.php | 4 +- .../PutOptionBasedConfigurableItemToCart.php | 3 +- src/Command/Cart/PutSimpleItemToCart.php | 3 +- .../PutVariantBasedConfigurableItemToCart.php | 3 +- src/Command/Cart/RemoveCoupon.php | 4 +- src/Command/Cart/RemoveItemFromCart.php | 4 +- src/Controller/Cart/AddCouponAction.php | 48 +++++++-------- .../Cart/ChangeItemQuantityAction.php | 29 ++++----- .../Cart/EstimateShippingCostAction.php | 1 - src/Controller/Cart/PutItemToCartAction.php | 30 ++++++---- src/Controller/Cart/RemoveCouponAction.php | 48 +++++++-------- .../Cart/RemoveItemFromCartAction.php | 36 +++++------ src/DependencyInjection/Configuration.php | 8 +++ src/Request/Cart/AddCouponRequest.php | 13 +++- .../Cart/ChangeItemQuantityRequest.php | 13 +++- src/Request/Cart/PickupCartRequest.php | 2 +- ...tionBasedConfigurableItemToCartRequest.php | 31 +++++++--- .../Cart/PutSimpleItemToCartRequest.php | 20 +++++-- ...iantBasedConfigurableItemToCartRequest.php | 29 +++++++-- src/Request/Cart/RemoveCouponRequest.php | 13 +++- .../Cart/RemoveItemFromCartRequest.php | 13 +++- .../config/services/actions/cart.xml | 59 +++++++++++-------- .../services/command_providers/cart.xml | 32 ++++++++++ tests/Request/AddCouponRequestTest.php | 2 +- .../Request/ChangeItemQuantityRequestTest.php | 14 +++-- tests/Request/PickupCartRequestTest.php | 7 ++- ...BasedConfigurableItemToCartRequestTest.php | 2 +- .../PutSimpleItemToCartRequestTest.php | 2 +- ...BasedConfigurableItemToCartRequestTest.php | 2 +- 34 files changed, 326 insertions(+), 167 deletions(-) diff --git a/src/Command/Cart/AddCoupon.php b/src/Command/Cart/AddCoupon.php index e0396c991..946101541 100644 --- a/src/Command/Cart/AddCoupon.php +++ b/src/Command/Cart/AddCoupon.php @@ -4,7 +4,9 @@ namespace Sylius\ShopApiPlugin\Command\Cart; -class AddCoupon +use Sylius\ShopApiPlugin\Command\CommandInterface; + +class AddCoupon implements CommandInterface { /** @var string */ protected $orderToken; diff --git a/src/Command/Cart/AddressOrder.php b/src/Command/Cart/AddressOrder.php index f4885289c..079bf91e0 100644 --- a/src/Command/Cart/AddressOrder.php +++ b/src/Command/Cart/AddressOrder.php @@ -4,9 +4,10 @@ namespace Sylius\ShopApiPlugin\Command\Cart; +use Sylius\ShopApiPlugin\Command\CommandInterface; use Sylius\ShopApiPlugin\Model\Address; -class AddressOrder +class AddressOrder implements CommandInterface { /** @var string */ protected $orderToken; diff --git a/src/Command/Cart/ChangeItemQuantity.php b/src/Command/Cart/ChangeItemQuantity.php index ba3dccc0f..0e4738179 100644 --- a/src/Command/Cart/ChangeItemQuantity.php +++ b/src/Command/Cart/ChangeItemQuantity.php @@ -4,9 +4,10 @@ namespace Sylius\ShopApiPlugin\Command\Cart; +use Sylius\ShopApiPlugin\Command\CommandInterface; use Webmozart\Assert\Assert; -class ChangeItemQuantity +class ChangeItemQuantity implements CommandInterface { /** @var string */ protected $orderToken; diff --git a/src/Command/Cart/ChoosePaymentMethod.php b/src/Command/Cart/ChoosePaymentMethod.php index 1a0da1ebf..e9cc019d4 100644 --- a/src/Command/Cart/ChoosePaymentMethod.php +++ b/src/Command/Cart/ChoosePaymentMethod.php @@ -4,7 +4,9 @@ namespace Sylius\ShopApiPlugin\Command\Cart; -class ChoosePaymentMethod +use Sylius\ShopApiPlugin\Command\CommandInterface; + +class ChoosePaymentMethod implements CommandInterface { /** @var mixed */ protected $paymentIdentifier; diff --git a/src/Command/Cart/ChooseShippingMethod.php b/src/Command/Cart/ChooseShippingMethod.php index e4a056a81..8a4d41561 100644 --- a/src/Command/Cart/ChooseShippingMethod.php +++ b/src/Command/Cart/ChooseShippingMethod.php @@ -4,7 +4,9 @@ namespace Sylius\ShopApiPlugin\Command\Cart; -class ChooseShippingMethod +use Sylius\ShopApiPlugin\Command\CommandInterface; + +class ChooseShippingMethod implements CommandInterface { /** @var mixed */ protected $shipmentIdentifier; diff --git a/src/Command/Cart/CompleteOrder.php b/src/Command/Cart/CompleteOrder.php index dda566d2e..934e1d4ed 100644 --- a/src/Command/Cart/CompleteOrder.php +++ b/src/Command/Cart/CompleteOrder.php @@ -4,7 +4,9 @@ namespace Sylius\ShopApiPlugin\Command\Cart; -class CompleteOrder +use Sylius\ShopApiPlugin\Command\CommandInterface; + +class CompleteOrder implements CommandInterface { /** @var string */ protected $orderToken; diff --git a/src/Command/Cart/PutOptionBasedConfigurableItemToCart.php b/src/Command/Cart/PutOptionBasedConfigurableItemToCart.php index c897ba2cb..5f83c860e 100644 --- a/src/Command/Cart/PutOptionBasedConfigurableItemToCart.php +++ b/src/Command/Cart/PutOptionBasedConfigurableItemToCart.php @@ -4,9 +4,10 @@ namespace Sylius\ShopApiPlugin\Command\Cart; +use Sylius\ShopApiPlugin\Command\CommandInterface; use Webmozart\Assert\Assert; -class PutOptionBasedConfigurableItemToCart +class PutOptionBasedConfigurableItemToCart implements CommandInterface { /** @var string */ protected $orderToken; diff --git a/src/Command/Cart/PutSimpleItemToCart.php b/src/Command/Cart/PutSimpleItemToCart.php index 271567e1e..ab4a573f6 100644 --- a/src/Command/Cart/PutSimpleItemToCart.php +++ b/src/Command/Cart/PutSimpleItemToCart.php @@ -4,9 +4,10 @@ namespace Sylius\ShopApiPlugin\Command\Cart; +use Sylius\ShopApiPlugin\Command\CommandInterface; use Webmozart\Assert\Assert; -class PutSimpleItemToCart +class PutSimpleItemToCart implements CommandInterface { /** @var string */ protected $orderToken; diff --git a/src/Command/Cart/PutVariantBasedConfigurableItemToCart.php b/src/Command/Cart/PutVariantBasedConfigurableItemToCart.php index 605d9e3a1..b31ec8638 100644 --- a/src/Command/Cart/PutVariantBasedConfigurableItemToCart.php +++ b/src/Command/Cart/PutVariantBasedConfigurableItemToCart.php @@ -4,9 +4,10 @@ namespace Sylius\ShopApiPlugin\Command\Cart; +use Sylius\ShopApiPlugin\Command\CommandInterface; use Webmozart\Assert\Assert; -class PutVariantBasedConfigurableItemToCart +class PutVariantBasedConfigurableItemToCart implements CommandInterface { /** @var string */ protected $orderToken; diff --git a/src/Command/Cart/RemoveCoupon.php b/src/Command/Cart/RemoveCoupon.php index 9a476766c..f9ee0059b 100644 --- a/src/Command/Cart/RemoveCoupon.php +++ b/src/Command/Cart/RemoveCoupon.php @@ -4,7 +4,9 @@ namespace Sylius\ShopApiPlugin\Command\Cart; -class RemoveCoupon +use Sylius\ShopApiPlugin\Command\CommandInterface; + +class RemoveCoupon implements CommandInterface { /** @var string */ protected $orderToken; diff --git a/src/Command/Cart/RemoveItemFromCart.php b/src/Command/Cart/RemoveItemFromCart.php index 2367ae53c..f2bc5842f 100644 --- a/src/Command/Cart/RemoveItemFromCart.php +++ b/src/Command/Cart/RemoveItemFromCart.php @@ -4,7 +4,9 @@ namespace Sylius\ShopApiPlugin\Command\Cart; -class RemoveItemFromCart +use Sylius\ShopApiPlugin\Command\CommandInterface; + +class RemoveItemFromCart implements CommandInterface { /** @var string */ protected $orderToken; diff --git a/src/Controller/Cart/AddCouponAction.php b/src/Controller/Cart/AddCouponAction.php index 17fa9bb32..6a9e0b98a 100644 --- a/src/Controller/Cart/AddCouponAction.php +++ b/src/Controller/Cart/AddCouponAction.php @@ -6,14 +6,14 @@ use FOS\RestBundle\View\View; use FOS\RestBundle\View\ViewHandlerInterface; +use Sylius\ShopApiPlugin\Command\Cart\AddCoupon; +use Sylius\ShopApiPlugin\CommandProvider\CommandProviderInterface; use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; -use Sylius\ShopApiPlugin\Request\Cart\AddCouponRequest; use Sylius\ShopApiPlugin\ViewRepository\Cart\CartViewRepositoryInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Messenger\MessageBusInterface; -use Symfony\Component\Validator\Validator\ValidatorInterface; final class AddCouponAction { @@ -23,49 +23,51 @@ final class AddCouponAction /** @var MessageBusInterface */ private $bus; - /** @var ValidatorInterface */ - private $validator; - /** @var ValidationErrorViewFactoryInterface */ private $validationErrorViewFactory; /** @var CartViewRepositoryInterface */ private $cartQuery; + /** @var CommandProviderInterface */ + private $addCouponCommandProvider; + public function __construct( ViewHandlerInterface $viewHandler, MessageBusInterface $bus, - ValidatorInterface $validator, ValidationErrorViewFactoryInterface $validationErrorViewFactory, - CartViewRepositoryInterface $cartQuery + CartViewRepositoryInterface $cartQuery, + CommandProviderInterface $addCouponCommandProvider ) { $this->viewHandler = $viewHandler; $this->bus = $bus; - $this->validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; $this->cartQuery = $cartQuery; + $this->addCouponCommandProvider = $addCouponCommandProvider; } public function __invoke(Request $request): Response { - $addCouponRequest = new AddCouponRequest($request); - - $validationResults = $this->validator->validate($addCouponRequest); + $validationResults = $this->addCouponCommandProvider->validate($request); + if (0 !== count($validationResults)) { + return $this->viewHandler->handle(View::create( + $this->validationErrorViewFactory->create($validationResults), + Response::HTTP_BAD_REQUEST + )); + } - if (0 === count($validationResults)) { - $addCouponCommand = $addCouponRequest->getCommand(); + /** @var AddCoupon $addCouponCommand */ + $addCouponCommand = $this->addCouponCommandProvider->getCommand($request); - $this->bus->dispatch($addCouponCommand); + $this->bus->dispatch($addCouponCommand); - try { - return $this->viewHandler->handle( - View::create($this->cartQuery->getOneByToken($addCouponCommand->orderToken()), Response::HTTP_OK) - ); - } catch (\InvalidArgumentException $exception) { - throw new BadRequestHttpException($exception->getMessage()); - } + try { + return $this->viewHandler->handle(View::create( + $this->cartQuery->getOneByToken($addCouponCommand->orderToken()), + Response::HTTP_OK + )); + } catch (\InvalidArgumentException $exception) { + throw new BadRequestHttpException($exception->getMessage()); } - - return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); } } diff --git a/src/Controller/Cart/ChangeItemQuantityAction.php b/src/Controller/Cart/ChangeItemQuantityAction.php index 38bab58a3..6b915d403 100644 --- a/src/Controller/Cart/ChangeItemQuantityAction.php +++ b/src/Controller/Cart/ChangeItemQuantityAction.php @@ -6,14 +6,14 @@ use FOS\RestBundle\View\View; use FOS\RestBundle\View\ViewHandlerInterface; +use Sylius\ShopApiPlugin\Command\Cart\ChangeItemQuantity; +use Sylius\ShopApiPlugin\CommandProvider\CommandProviderInterface; use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; -use Sylius\ShopApiPlugin\Request\Cart\ChangeItemQuantityRequest; use Sylius\ShopApiPlugin\ViewRepository\Cart\CartViewRepositoryInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Messenger\MessageBusInterface; -use Symfony\Component\Validator\Validator\ValidatorInterface; final class ChangeItemQuantityAction { @@ -23,40 +23,41 @@ final class ChangeItemQuantityAction /** @var MessageBusInterface */ private $bus; - /** @var ValidatorInterface */ - private $validator; - /** @var ValidationErrorViewFactoryInterface */ private $validationErrorViewFactory; /** @var CartViewRepositoryInterface */ private $cartQuery; + /** @var CommandProviderInterface */ + private $changeItemQuantityCommandProvider; + public function __construct( ViewHandlerInterface $viewHandler, MessageBusInterface $bus, - ValidatorInterface $validator, ValidationErrorViewFactoryInterface $validationErrorViewFactory, - CartViewRepositoryInterface $cartQuery + CartViewRepositoryInterface $cartQuery, + CommandProviderInterface $changeItemQuantityCommandProvider ) { $this->viewHandler = $viewHandler; $this->bus = $bus; - $this->validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; $this->cartQuery = $cartQuery; + $this->changeItemQuantityCommandProvider = $changeItemQuantityCommandProvider; } public function __invoke(Request $request): Response { - $changeItemQuantityRequest = new ChangeItemQuantityRequest($request); - - $validationResults = $this->validator->validate($changeItemQuantityRequest); - + $validationResults = $this->changeItemQuantityCommandProvider->validate($request); if (0 !== count($validationResults)) { - return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); + return $this->viewHandler->handle(View::create( + $this->validationErrorViewFactory->create($validationResults), + Response::HTTP_BAD_REQUEST + )); } - $changeItemQuantityCommand = $changeItemQuantityRequest->getCommand(); + /** @var ChangeItemQuantity $changeItemQuantityCommand */ + $changeItemQuantityCommand = $this->changeItemQuantityCommandProvider->getCommand($request); $this->bus->dispatch($changeItemQuantityCommand); diff --git a/src/Controller/Cart/EstimateShippingCostAction.php b/src/Controller/Cart/EstimateShippingCostAction.php index 4b5b29ff5..c08c06190 100644 --- a/src/Controller/Cart/EstimateShippingCostAction.php +++ b/src/Controller/Cart/EstimateShippingCostAction.php @@ -50,7 +50,6 @@ public function __invoke(Request $request): Response $estimateShippingCostRequest = new EstimateShippingCostRequest($request); $validationResults = $this->validator->validate($estimateShippingCostRequest); - if (0 !== count($validationResults)) { return $this->viewHandler->handle( View::create( diff --git a/src/Controller/Cart/PutItemToCartAction.php b/src/Controller/Cart/PutItemToCartAction.php index f9a8aa5c9..034257594 100644 --- a/src/Controller/Cart/PutItemToCartAction.php +++ b/src/Controller/Cart/PutItemToCartAction.php @@ -6,11 +6,15 @@ use FOS\RestBundle\View\View; use FOS\RestBundle\View\ViewHandlerInterface; +use Sylius\ShopApiPlugin\Command\Cart\PutOptionBasedConfigurableItemToCart; +use Sylius\ShopApiPlugin\Command\Cart\PutSimpleItemToCart; +use Sylius\ShopApiPlugin\Command\Cart\PutVariantBasedConfigurableItemToCart; use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; use Sylius\ShopApiPlugin\Normalizer\RequestCartTokenNormalizerInterface; use Sylius\ShopApiPlugin\Request\Cart\PutOptionBasedConfigurableItemToCartRequest; use Sylius\ShopApiPlugin\Request\Cart\PutSimpleItemToCartRequest; use Sylius\ShopApiPlugin\Request\Cart\PutVariantBasedConfigurableItemToCartRequest; +use Sylius\ShopApiPlugin\Request\RequestInterface; use Sylius\ShopApiPlugin\ViewRepository\Cart\CartViewRepositoryInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -68,41 +72,41 @@ public function __invoke(Request $request): Response $validationResults = $this->validator->validate($commandRequest); if (0 !== count($validationResults)) { - return $this->viewHandler->handle( - View::create($this->validationErrorViewFactory->create($validationResults), - Response::HTTP_BAD_REQUEST - ) - ); + return $this->viewHandler->handle(View::create( + $this->validationErrorViewFactory->create($validationResults), + Response::HTTP_BAD_REQUEST + )); } + /** @var PutOptionBasedConfigurableItemToCart|PutSimpleItemToCart|PutVariantBasedConfigurableItemToCart $command */ $command = $commandRequest->getCommand(); $this->bus->dispatch($command); try { - return $this->viewHandler->handle( - View::create($this->cartQuery->getOneByToken($command->orderToken()), Response::HTTP_CREATED) - ); + return $this->viewHandler->handle(View::create( + $this->cartQuery->getOneByToken($command->orderToken()), + Response::HTTP_CREATED + )); } catch (\InvalidArgumentException $exception) { throw new BadRequestHttpException($exception->getMessage()); } } - /** @return PutOptionBasedConfigurableItemToCartRequest|PutSimpleItemToCartRequest|PutVariantBasedConfigurableItemToCartRequest */ - private function provideCommandRequest(Request $request) + private function provideCommandRequest(Request $request): RequestInterface { $hasVariantCode = $request->request->has('variantCode'); $hasOptionCode = $request->request->has('options'); if (!$hasVariantCode && !$hasOptionCode) { - return PutSimpleItemToCartRequest::fromRequest($request); + return PutSimpleItemToCartRequest::fromHttpRequest($request); } if ($hasVariantCode && !$hasOptionCode) { - return PutVariantBasedConfigurableItemToCartRequest::fromRequest($request); + return PutVariantBasedConfigurableItemToCartRequest::fromHttpRequest($request); } if (!$hasVariantCode && $hasOptionCode) { - return PutOptionBasedConfigurableItemToCartRequest::fromRequest($request); + return PutOptionBasedConfigurableItemToCartRequest::fromHttpRequest($request); } throw new NotFoundHttpException('Variant not found for given configuration'); diff --git a/src/Controller/Cart/RemoveCouponAction.php b/src/Controller/Cart/RemoveCouponAction.php index 41f4fd6d5..8d95b72ec 100644 --- a/src/Controller/Cart/RemoveCouponAction.php +++ b/src/Controller/Cart/RemoveCouponAction.php @@ -6,14 +6,14 @@ use FOS\RestBundle\View\View; use FOS\RestBundle\View\ViewHandlerInterface; +use Sylius\ShopApiPlugin\Command\Cart\RemoveCoupon; +use Sylius\ShopApiPlugin\CommandProvider\CommandProviderInterface; use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; -use Sylius\ShopApiPlugin\Request\Cart\RemoveCouponRequest; use Sylius\ShopApiPlugin\ViewRepository\Cart\CartViewRepositoryInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Messenger\MessageBusInterface; -use Symfony\Component\Validator\Validator\ValidatorInterface; final class RemoveCouponAction { @@ -23,49 +23,51 @@ final class RemoveCouponAction /** @var MessageBusInterface */ private $bus; - /** @var ValidatorInterface */ - private $validator; - /** @var ValidationErrorViewFactoryInterface */ private $validationErrorViewFactory; /** @var CartViewRepositoryInterface */ private $cartQuery; + /** @var CommandProviderInterface */ + private $removeCouponCommandProvider; + public function __construct( ViewHandlerInterface $viewHandler, MessageBusInterface $bus, - ValidatorInterface $validator, ValidationErrorViewFactoryInterface $validationErrorViewFactory, - CartViewRepositoryInterface $cartQuery + CartViewRepositoryInterface $cartQuery, + CommandProviderInterface $removeCouponCommandProvider ) { $this->viewHandler = $viewHandler; $this->bus = $bus; - $this->validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; $this->cartQuery = $cartQuery; + $this->removeCouponCommandProvider = $removeCouponCommandProvider; } public function __invoke(Request $request): Response { - $removeCouponRequest = new RemoveCouponRequest($request); - - $validationResults = $this->validator->validate($removeCouponRequest); + $validationResults = $this->removeCouponCommandProvider->validate($request); + if (0 !== count($validationResults)) { + return $this->viewHandler->handle(View::create( + $this->validationErrorViewFactory->create($validationResults), + Response::HTTP_BAD_REQUEST + )); + } - if (0 === count($validationResults)) { - $removeCouponCommand = $removeCouponRequest->getCommand(); + /** @var RemoveCoupon $removeCouponCommand */ + $removeCouponCommand = $this->removeCouponCommandProvider->getCommand($request); - $this->bus->dispatch($removeCouponCommand); + $this->bus->dispatch($removeCouponCommand); - try { - return $this->viewHandler->handle( - View::create($this->cartQuery->getOneByToken($removeCouponCommand->orderToken()), Response::HTTP_OK) - ); - } catch (\InvalidArgumentException $exception) { - throw new BadRequestHttpException($exception->getMessage()); - } + try { + return $this->viewHandler->handle(View::create( + $this->cartQuery->getOneByToken($removeCouponCommand->orderToken()), + Response::HTTP_OK + )); + } catch (\InvalidArgumentException $exception) { + throw new BadRequestHttpException($exception->getMessage()); } - - return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); } } diff --git a/src/Controller/Cart/RemoveItemFromCartAction.php b/src/Controller/Cart/RemoveItemFromCartAction.php index 952d89b53..46a512f54 100644 --- a/src/Controller/Cart/RemoveItemFromCartAction.php +++ b/src/Controller/Cart/RemoveItemFromCartAction.php @@ -6,14 +6,14 @@ use FOS\RestBundle\View\View; use FOS\RestBundle\View\ViewHandlerInterface; +use Sylius\ShopApiPlugin\Command\Cart\RemoveItemFromCart; +use Sylius\ShopApiPlugin\CommandProvider\CommandProviderInterface; use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; -use Sylius\ShopApiPlugin\Request\Cart\RemoveItemFromCartRequest; use Sylius\ShopApiPlugin\ViewRepository\Cart\CartViewRepositoryInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Messenger\MessageBusInterface; -use Symfony\Component\Validator\Validator\ValidatorInterface; final class RemoveItemFromCartAction { @@ -23,47 +23,49 @@ final class RemoveItemFromCartAction /** @var MessageBusInterface */ private $bus; - /** @var ValidatorInterface */ - private $validator; - /** @var ValidationErrorViewFactoryInterface */ private $validationErrorViewFactory; /** @var CartViewRepositoryInterface */ private $cartQuery; + /** @var CommandProviderInterface */ + private $removeItemFromCartCommandProvider; + public function __construct( ViewHandlerInterface $viewHandler, MessageBusInterface $bus, - ValidatorInterface $validator, ValidationErrorViewFactoryInterface $validationErrorViewFactory, - CartViewRepositoryInterface $cartQuery + CartViewRepositoryInterface $cartQuery, + CommandProviderInterface $removeItemFromCartCommandProvider ) { $this->viewHandler = $viewHandler; $this->bus = $bus; - $this->validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; $this->cartQuery = $cartQuery; + $this->removeItemFromCartCommandProvider = $removeItemFromCartCommandProvider; } public function __invoke(Request $request): Response { - $removeItemFromCartRequest = new RemoveItemFromCartRequest($request); - - $validationResults = $this->validator->validate($removeItemFromCartRequest); - + $validationResults = $this->removeItemFromCartCommandProvider->validate($request); if (0 !== count($validationResults)) { - return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); + return $this->viewHandler->handle(View::create( + $this->validationErrorViewFactory->create($validationResults), + Response::HTTP_BAD_REQUEST + )); } - $removeItemFromCartCommand = $removeItemFromCartRequest->getCommand(); + /** @var RemoveItemFromCart $removeItemFromCartCommand */ + $removeItemFromCartCommand = $this->removeItemFromCartCommandProvider->getCommand($request); $this->bus->dispatch($removeItemFromCartCommand); try { - return $this->viewHandler->handle( - View::create($this->cartQuery->getOneByToken($removeItemFromCartCommand->orderToken()), Response::HTTP_OK) - ); + return $this->viewHandler->handle(View::create( + $this->cartQuery->getOneByToken($removeItemFromCartCommand->orderToken()), + Response::HTTP_OK + )); } catch (\InvalidArgumentException $exception) { throw new BadRequestHttpException($exception->getMessage()); } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 640ec65e4..8489d5c00 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -4,10 +4,14 @@ namespace Sylius\ShopApiPlugin\DependencyInjection; +use Sylius\ShopApiPlugin\Request\Cart\AddCouponRequest; +use Sylius\ShopApiPlugin\Request\Cart\ChangeItemQuantityRequest; use Sylius\ShopApiPlugin\Request\Cart\DropCartRequest; use Sylius\ShopApiPlugin\Request\Cart\PickupCartRequest; use Sylius\ShopApiPlugin\Request\Product\AddProductReviewByCodeRequest; use Sylius\ShopApiPlugin\Request\Product\AddProductReviewBySlugRequest; +use Sylius\ShopApiPlugin\Request\Cart\RemoveCouponRequest; +use Sylius\ShopApiPlugin\Request\Cart\RemoveItemFromCartRequest; use Sylius\ShopApiPlugin\View; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; @@ -87,10 +91,14 @@ private function buildRequestClassesNode(ArrayNodeDefinition $rootNode): void ->arrayNode('request_classes') ->addDefaultsIfNotSet() ->children() + ->scalarNode('add_coupon')->defaultValue(AddCouponRequest::class)->end() ->scalarNode('add_product_review_by_code')->defaultValue(AddProductReviewByCodeRequest::class)->end() ->scalarNode('add_product_review_by_slug')->defaultValue(AddProductReviewBySlugRequest::class)->end() + ->scalarNode('change_item_quantity')->defaultValue(ChangeItemQuantityRequest::class)->end() ->scalarNode('drop_cart')->defaultValue(DropCartRequest::class)->end() ->scalarNode('pickup_cart')->defaultValue(PickupCartRequest::class)->end() + ->scalarNode('remove_coupon')->defaultValue(RemoveCouponRequest::class)->end() + ->scalarNode('remove_item_from_cart')->defaultValue(RemoveItemFromCartRequest::class)->end() ->end() ->end() ->end() diff --git a/src/Request/Cart/AddCouponRequest.php b/src/Request/Cart/AddCouponRequest.php index ce3db4f81..81141cdce 100644 --- a/src/Request/Cart/AddCouponRequest.php +++ b/src/Request/Cart/AddCouponRequest.php @@ -5,9 +5,11 @@ namespace Sylius\ShopApiPlugin\Request\Cart; use Sylius\ShopApiPlugin\Command\Cart\AddCoupon; +use Sylius\ShopApiPlugin\Command\CommandInterface; +use Sylius\ShopApiPlugin\Request\RequestInterface; use Symfony\Component\HttpFoundation\Request; -class AddCouponRequest +class AddCouponRequest implements RequestInterface { /** @var string|null */ protected $token; @@ -15,13 +17,18 @@ class AddCouponRequest /** @var string|null */ protected $coupon; - public function __construct(Request $request) + private function __construct(Request $request) { $this->token = $request->attributes->get('token'); $this->coupon = $request->request->get('coupon'); } - public function getCommand(): AddCoupon + public static function fromHttpRequest(Request $request): RequestInterface + { + return new self($request); + } + + public function getCommand(): CommandInterface { return new AddCoupon($this->token, $this->coupon); } diff --git a/src/Request/Cart/ChangeItemQuantityRequest.php b/src/Request/Cart/ChangeItemQuantityRequest.php index 391e83af6..351ff2654 100644 --- a/src/Request/Cart/ChangeItemQuantityRequest.php +++ b/src/Request/Cart/ChangeItemQuantityRequest.php @@ -5,9 +5,11 @@ namespace Sylius\ShopApiPlugin\Request\Cart; use Sylius\ShopApiPlugin\Command\Cart\ChangeItemQuantity; +use Sylius\ShopApiPlugin\Command\CommandInterface; +use Sylius\ShopApiPlugin\Request\RequestInterface; use Symfony\Component\HttpFoundation\Request; -class ChangeItemQuantityRequest +class ChangeItemQuantityRequest implements RequestInterface { /** @var string */ protected $token; @@ -18,14 +20,19 @@ class ChangeItemQuantityRequest /** @var int */ protected $quantity; - public function __construct(Request $request) + private function __construct(Request $request) { $this->token = $request->attributes->get('token'); $this->id = $request->attributes->get('id'); $this->quantity = $request->request->getInt('quantity'); } - public function getCommand(): ChangeItemQuantity + public static function fromHttpRequest(Request $request): RequestInterface + { + return new self($request); + } + + public function getCommand(): CommandInterface { return new ChangeItemQuantity($this->token, $this->id, $this->quantity); } diff --git a/src/Request/Cart/PickupCartRequest.php b/src/Request/Cart/PickupCartRequest.php index 4be0ab617..603ede3d3 100644 --- a/src/Request/Cart/PickupCartRequest.php +++ b/src/Request/Cart/PickupCartRequest.php @@ -19,7 +19,7 @@ class PickupCartRequest implements ChannelBasedRequestInterface /** @var string */ protected $channelCode; - public function __construct(string $channelCode) + private function __construct(string $channelCode) { $this->token = Uuid::uuid4()->toString(); $this->channelCode = $channelCode; diff --git a/src/Request/Cart/PutOptionBasedConfigurableItemToCartRequest.php b/src/Request/Cart/PutOptionBasedConfigurableItemToCartRequest.php index f2882672e..baa83bbee 100644 --- a/src/Request/Cart/PutOptionBasedConfigurableItemToCartRequest.php +++ b/src/Request/Cart/PutOptionBasedConfigurableItemToCartRequest.php @@ -5,9 +5,11 @@ namespace Sylius\ShopApiPlugin\Request\Cart; use Sylius\ShopApiPlugin\Command\Cart\PutOptionBasedConfigurableItemToCart; +use Sylius\ShopApiPlugin\Command\CommandInterface; +use Sylius\ShopApiPlugin\Request\RequestInterface; use Symfony\Component\HttpFoundation\Request; -class PutOptionBasedConfigurableItemToCartRequest +class PutOptionBasedConfigurableItemToCartRequest implements RequestInterface { /** @var string */ protected $token; @@ -21,7 +23,7 @@ class PutOptionBasedConfigurableItemToCartRequest /** @var int */ protected $quantity; - private function __construct($token, $productCode, $options, $quantity) + private function __construct(?string $token, ?string $productCode, ?array $options, ?int $quantity) { $this->token = $token; $this->productCode = $productCode; @@ -31,16 +33,31 @@ private function __construct($token, $productCode, $options, $quantity) public static function fromArray(array $item): self { - return new self($item['token'] ?? null, $item['productCode'] ?? null, $item['options'] ?? null, $item['quantity'] ?? null); + return new self( + $item['token'] ?? null, + $item['productCode'] ?? null, + $item['options'] ?? null, + $item['quantity'] ?? null + ); } - public static function fromRequest(Request $request): self + public static function fromHttpRequest(Request $request): RequestInterface { - return new self($request->attributes->get('token'), $request->request->get('productCode'), $request->request->get('options'), $request->request->getInt('quantity', 1)); + return new self( + $request->attributes->get('token'), + $request->request->get('productCode'), + $request->request->get('options'), + $request->request->getInt('quantity', 1) + ); } - public function getCommand(): PutOptionBasedConfigurableItemToCart + public function getCommand(): CommandInterface { - return new PutOptionBasedConfigurableItemToCart($this->token, $this->productCode, $this->options, $this->quantity); + return new PutOptionBasedConfigurableItemToCart( + $this->token, + $this->productCode, + $this->options, + $this->quantity + ); } } diff --git a/src/Request/Cart/PutSimpleItemToCartRequest.php b/src/Request/Cart/PutSimpleItemToCartRequest.php index 9c4519d11..7ca77b01c 100644 --- a/src/Request/Cart/PutSimpleItemToCartRequest.php +++ b/src/Request/Cart/PutSimpleItemToCartRequest.php @@ -5,9 +5,11 @@ namespace Sylius\ShopApiPlugin\Request\Cart; use Sylius\ShopApiPlugin\Command\Cart\PutSimpleItemToCart; +use Sylius\ShopApiPlugin\Command\CommandInterface; +use Sylius\ShopApiPlugin\Request\RequestInterface; use Symfony\Component\HttpFoundation\Request; -class PutSimpleItemToCartRequest +class PutSimpleItemToCartRequest implements RequestInterface { /** @var string */ protected $token; @@ -27,15 +29,23 @@ private function __construct($token, $productCode, $quantity) public static function fromArray(array $item): self { - return new self($item['token'] ?? null, $item['productCode'] ?? null, $item['quantity'] ?? null); + return new self( + $item['token'] ?? null, + $item['productCode'] ?? null, + $item['quantity'] ?? null + ); } - public static function fromRequest(Request $request): self + public static function fromHttpRequest(Request $request): RequestInterface { - return new self($request->attributes->get('token'), $request->request->get('productCode'), $request->request->getInt('quantity', 1)); + return new self( + $request->attributes->get('token'), + $request->request->get('productCode'), + $request->request->getInt('quantity', 1) + ); } - public function getCommand(): PutSimpleItemToCart + public function getCommand(): CommandInterface { return new PutSimpleItemToCart($this->token, $this->productCode, $this->quantity); } diff --git a/src/Request/Cart/PutVariantBasedConfigurableItemToCartRequest.php b/src/Request/Cart/PutVariantBasedConfigurableItemToCartRequest.php index fad029709..884d5c44d 100644 --- a/src/Request/Cart/PutVariantBasedConfigurableItemToCartRequest.php +++ b/src/Request/Cart/PutVariantBasedConfigurableItemToCartRequest.php @@ -5,9 +5,11 @@ namespace Sylius\ShopApiPlugin\Request\Cart; use Sylius\ShopApiPlugin\Command\Cart\PutVariantBasedConfigurableItemToCart; +use Sylius\ShopApiPlugin\Command\CommandInterface; +use Sylius\ShopApiPlugin\Request\RequestInterface; use Symfony\Component\HttpFoundation\Request; -class PutVariantBasedConfigurableItemToCartRequest +class PutVariantBasedConfigurableItemToCartRequest implements RequestInterface { /** @var string */ protected $token; @@ -31,16 +33,31 @@ private function __construct($token, $productCode, $variantCode, $quantity) public static function fromArray(array $item): self { - return new self($item['token'] ?? null, $item['productCode'] ?? null, $item['variantCode'] ?? null, $item['quantity'] ?? null); + return new self( + $item['token'] ?? null, + $item['productCode'] ?? null, + $item['variantCode'] ?? null, + $item['quantity'] ?? null + ); } - public static function fromRequest(Request $request): self + public static function fromHttpRequest(Request $request): RequestInterface { - return new self($request->attributes->get('token'), $request->request->get('productCode'), $request->request->get('variantCode'), $request->request->getInt('quantity', 1)); + return new self( + $request->attributes->get('token'), + $request->request->get('productCode'), + $request->request->get('variantCode'), + $request->request->getInt('quantity', 1) + ); } - public function getCommand(): PutVariantBasedConfigurableItemToCart + public function getCommand(): CommandInterface { - return new PutVariantBasedConfigurableItemToCart($this->token, $this->productCode, $this->variantCode, $this->quantity); + return new PutVariantBasedConfigurableItemToCart( + $this->token, + $this->productCode, + $this->variantCode, + $this->quantity + ); } } diff --git a/src/Request/Cart/RemoveCouponRequest.php b/src/Request/Cart/RemoveCouponRequest.php index c27cc0085..22e40fa84 100644 --- a/src/Request/Cart/RemoveCouponRequest.php +++ b/src/Request/Cart/RemoveCouponRequest.php @@ -5,19 +5,26 @@ namespace Sylius\ShopApiPlugin\Request\Cart; use Sylius\ShopApiPlugin\Command\Cart\RemoveCoupon; +use Sylius\ShopApiPlugin\Command\CommandInterface; +use Sylius\ShopApiPlugin\Request\RequestInterface; use Symfony\Component\HttpFoundation\Request; -class RemoveCouponRequest +class RemoveCouponRequest implements RequestInterface { /** @var string */ protected $token; - public function __construct(Request $request) + private function __construct(Request $request) { $this->token = $request->attributes->get('token'); } - public function getCommand(): RemoveCoupon + public static function fromHttpRequest(Request $request): RequestInterface + { + return new self($request); + } + + public function getCommand(): CommandInterface { return new RemoveCoupon($this->token); } diff --git a/src/Request/Cart/RemoveItemFromCartRequest.php b/src/Request/Cart/RemoveItemFromCartRequest.php index 27782bb8d..976d6c550 100644 --- a/src/Request/Cart/RemoveItemFromCartRequest.php +++ b/src/Request/Cart/RemoveItemFromCartRequest.php @@ -5,9 +5,11 @@ namespace Sylius\ShopApiPlugin\Request\Cart; use Sylius\ShopApiPlugin\Command\Cart\RemoveItemFromCart; +use Sylius\ShopApiPlugin\Command\CommandInterface; +use Sylius\ShopApiPlugin\Request\RequestInterface; use Symfony\Component\HttpFoundation\Request; -class RemoveItemFromCartRequest +class RemoveItemFromCartRequest implements RequestInterface { /** @var string */ protected $token; @@ -15,13 +17,18 @@ class RemoveItemFromCartRequest /** @var mixed */ protected $id; - public function __construct(Request $request) + private function __construct(Request $request) { $this->token = $request->attributes->get('token'); $this->id = $request->attributes->get('id'); } - public function getCommand(): RemoveItemFromCart + public static function fromHttpRequest(Request $request): RequestInterface + { + return new self($request); + } + + public function getCommand(): CommandInterface { return new RemoveItemFromCart($this->token, $this->id); } diff --git a/src/Resources/config/services/actions/cart.xml b/src/Resources/config/services/actions/cart.xml index 1dc7cdc47..18c6cd092 100644 --- a/src/Resources/config/services/actions/cart.xml +++ b/src/Resources/config/services/actions/cart.xml @@ -4,15 +4,17 @@ - - @@ -20,8 +22,10 @@ - + @@ -29,8 +33,9 @@ - @@ -40,48 +45,53 @@ - - + - - + - - + - - + - @@ -91,8 +101,9 @@ - diff --git a/src/Resources/config/services/command_providers/cart.xml b/src/Resources/config/services/command_providers/cart.xml index 7f9cee064..14768ded9 100644 --- a/src/Resources/config/services/command_providers/cart.xml +++ b/src/Resources/config/services/command_providers/cart.xml @@ -4,6 +4,22 @@ + + %sylius.shop_api.request.add_coupon.class% + + + + + %sylius.shop_api.request.change_item_quantity.class% + + + %sylius.shop_api.request.pickup_cart.class% + + + %sylius.shop_api.request.remove_coupon.class% + + + + + %sylius.shop_api.request.remove_item_from_cart.class% + + diff --git a/tests/Request/AddCouponRequestTest.php b/tests/Request/AddCouponRequestTest.php index 97400acfa..df6be589e 100644 --- a/tests/Request/AddCouponRequestTest.php +++ b/tests/Request/AddCouponRequestTest.php @@ -16,7 +16,7 @@ final class AddCouponRequestTest extends TestCase */ public function it_creates_pickup_cart_command() { - $pickupCartRequest = new AddCouponRequest(new Request([], ['coupon' => 'SUMMER_SALE'], ['token' => 'ORDERTOKEN'])); + $pickupCartRequest = AddCouponRequest::fromHttpRequest(new Request([], ['coupon' => 'SUMMER_SALE'], ['token' => 'ORDERTOKEN'])); $this->assertEquals($pickupCartRequest->getCommand(), new AddCoupon('ORDERTOKEN', 'SUMMER_SALE')); } diff --git a/tests/Request/ChangeItemQuantityRequestTest.php b/tests/Request/ChangeItemQuantityRequestTest.php index 2a5577385..741b2e67a 100644 --- a/tests/Request/ChangeItemQuantityRequestTest.php +++ b/tests/Request/ChangeItemQuantityRequestTest.php @@ -16,11 +16,15 @@ final class ChangeItemQuantityRequestTest extends TestCase */ public function it_creates_pickup_cart_command() { - $changeItemQuantityRequest = new ChangeItemQuantityRequest(new Request([], ['quantity' => 5], [ - 'token' => 'ORDERTOKEN', - 'id' => 1, - ])); + $changeItemQuantityRequest = ChangeItemQuantityRequest::fromHttpRequest(new Request( + [], + ['quantity' => 5], + ['token' => 'ORDERTOKEN', 'id' => 1] + )); - $this->assertEquals(new ChangeItemQuantity('ORDERTOKEN', 1, 5), $changeItemQuantityRequest->getCommand()); + $this->assertEquals( + new ChangeItemQuantity('ORDERTOKEN', 1, 5), + $changeItemQuantityRequest->getCommand() + ); } } diff --git a/tests/Request/PickupCartRequestTest.php b/tests/Request/PickupCartRequestTest.php index 4bd9a4359..6698d6360 100644 --- a/tests/Request/PickupCartRequestTest.php +++ b/tests/Request/PickupCartRequestTest.php @@ -5,14 +5,19 @@ namespace Tests\Sylius\ShopApiPlugin\Request; use PHPUnit\Framework\TestCase; +use Sylius\Component\Core\Model\Channel; use Sylius\ShopApiPlugin\Request\Cart\PickupCartRequest; +use Symfony\Component\HttpFoundation\Request; final class PickupCartRequestTest extends TestCase { /** @test */ public function it_creates_pickup_cart_command(): void { - $pickupCartRequest = new PickupCartRequest('WEB_GB'); + $channel = new Channel(); + $channel->setCode('WEB_GB'); + + $pickupCartRequest = PickupCartRequest::fromHttpRequestAndChannel(new Request(), $channel); $pickupCartCommand = $pickupCartRequest->getCommand(); diff --git a/tests/Request/PutOptionBasedConfigurableItemToCartRequestTest.php b/tests/Request/PutOptionBasedConfigurableItemToCartRequestTest.php index cc5aa9fbb..8a33d9574 100644 --- a/tests/Request/PutOptionBasedConfigurableItemToCartRequestTest.php +++ b/tests/Request/PutOptionBasedConfigurableItemToCartRequestTest.php @@ -16,7 +16,7 @@ final class PutOptionBasedConfigurableItemToCartRequestTest extends TestCase */ public function it_creates_put_simple_item_to_cart_command_from_request() { - $putConfigurableItemToCartRequest = PutOptionBasedConfigurableItemToCartRequest::fromRequest(new Request([], [ + $putConfigurableItemToCartRequest = PutOptionBasedConfigurableItemToCartRequest::fromHttpRequest(new Request([], [ 'productCode' => 'HACKTOBERFEST_TSHIRT_CODE', 'options' => ['LARGE__CODE'], 'quantity' => 4, diff --git a/tests/Request/PutSimpleItemToCartRequestTest.php b/tests/Request/PutSimpleItemToCartRequestTest.php index 6a56cbd3b..e1906e949 100644 --- a/tests/Request/PutSimpleItemToCartRequestTest.php +++ b/tests/Request/PutSimpleItemToCartRequestTest.php @@ -16,7 +16,7 @@ final class PutSimpleItemToCartRequestTest extends TestCase */ public function it_creates_put_simple_item_to_cart_command_from_request() { - $putSimpleItemToCartRequest = PutSimpleItemToCartRequest::fromRequest(new Request([], [ + $putSimpleItemToCartRequest = PutSimpleItemToCartRequest::fromHttpRequest(new Request([], [ 'productCode' => 'HACKTOBERFEST_TSHIRT_CODE', 'quantity' => 4, ], ['token' => 'ORDERTOKEN'])); diff --git a/tests/Request/PutVariantBasedConfigurableItemToCartRequestTest.php b/tests/Request/PutVariantBasedConfigurableItemToCartRequestTest.php index 55728636c..9eb53b357 100644 --- a/tests/Request/PutVariantBasedConfigurableItemToCartRequestTest.php +++ b/tests/Request/PutVariantBasedConfigurableItemToCartRequestTest.php @@ -16,7 +16,7 @@ final class PutVariantBasedConfigurableItemToCartRequestTest extends TestCase */ public function it_creates_put_simple_item_to_cart_command_from_request() { - $putConfigurableItemToCartRequest = PutVariantBasedConfigurableItemToCartRequest::fromRequest(new Request([], [ + $putConfigurableItemToCartRequest = PutVariantBasedConfigurableItemToCartRequest::fromHttpRequest(new Request([], [ 'productCode' => 'HACKTOBERFEST_TSHIRT_CODE', 'variantCode' => 'LARGE_HACKTOBERFEST_TSHIRT_CODE', 'quantity' => 4,