From da515f48ea331386c2b49bdbad404fbbf03f89eb Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Wed, 24 Jul 2019 12:28:16 +0200 Subject: [PATCH] [Cart] Add custom put item to cart command provider --- .../PutItemToCartCommandProviderSpec.php | 87 +++++++++++++++++++ .../PutItemToCartCommandProvider.php | 56 ++++++++++++ src/Controller/Cart/PutItemToCartAction.php | 46 ++-------- src/DependencyInjection/Configuration.php | 6 +- .../config/services/actions/cart.xml | 2 +- .../services/command_providers/cart.xml | 7 ++ 6 files changed, 163 insertions(+), 41 deletions(-) create mode 100644 spec/CommandProvider/PutItemToCartCommandProviderSpec.php create mode 100644 src/CommandProvider/PutItemToCartCommandProvider.php diff --git a/spec/CommandProvider/PutItemToCartCommandProviderSpec.php b/spec/CommandProvider/PutItemToCartCommandProviderSpec.php new file mode 100644 index 000000000..fe8aeafb0 --- /dev/null +++ b/spec/CommandProvider/PutItemToCartCommandProviderSpec.php @@ -0,0 +1,87 @@ +beConstructedWith($validator); + } + + function it_implements_command_provider_interface(): void + { + $this->shouldHaveType(CommandProviderInterface::class); + } + + function it_validates_put_simple_item_to_cart_request( + ValidatorInterface $validator, + Request $httpRequest, + ConstraintViolationListInterface $constraintViolationList + ): void { + $httpRequest->attributes = new ParameterBag(['token' => 'ORDERTOKEN']); + $httpRequest->request = new ParameterBag([ + 'productCode' => 'HACKTOBERFEST_TSHIRT_CODE', + 'quantity' => 4, + ]); + + $validator + ->validate(PutSimpleItemToCartRequest::fromHttpRequest($httpRequest->getWrappedObject())) + ->willReturn($constraintViolationList) + ; + + $this->validate($httpRequest)->shouldReturn($constraintViolationList); + } + + function it_validates_put_variant_based_configurable_item_to_cart_request( + ValidatorInterface $validator, + Request $httpRequest, + ConstraintViolationListInterface $constraintViolationList + ): void { + $httpRequest->attributes = new ParameterBag(['token' => 'ORDERTOKEN']); + $httpRequest->request = new ParameterBag([ + 'productCode' => 'HACKTOBERFEST_TSHIRT_CODE', + 'variantCode' => 'LARGE_HACKTOBERFEST_TSHIRT_CODE', + 'quantity' => 4, + ]); + + $validator + ->validate(PutVariantBasedConfigurableItemToCartRequest::fromHttpRequest($httpRequest->getWrappedObject())) + ->willReturn($constraintViolationList) + ; + + $this->validate($httpRequest)->shouldReturn($constraintViolationList); + } + + function it_validates_put_option_based_configurable_item_to_cart_request( + ValidatorInterface $validator, + Request $httpRequest, + ConstraintViolationListInterface $constraintViolationList + ): void { + $httpRequest->attributes = new ParameterBag(['token' => 'ORDERTOKEN']); + $httpRequest->request = new ParameterBag([ + 'productCode' => 'HACKTOBERFEST_TSHIRT_CODE', + 'options' => ['LARGE_CODE'], + 'quantity' => 4, + ]); + + $validator + ->validate(PutOptionBasedConfigurableItemToCartRequest::fromHttpRequest($httpRequest->getWrappedObject())) + ->willReturn($constraintViolationList) + ; + + $this->validate($httpRequest)->shouldReturn($constraintViolationList); + } +} diff --git a/src/CommandProvider/PutItemToCartCommandProvider.php b/src/CommandProvider/PutItemToCartCommandProvider.php new file mode 100644 index 000000000..73274a36e --- /dev/null +++ b/src/CommandProvider/PutItemToCartCommandProvider.php @@ -0,0 +1,56 @@ +validator = $validator; + } + + public function validate(Request $httpRequest): ConstraintViolationListInterface + { + return $this->validator->validate($this->transformHttpRequest($httpRequest)); + } + + public function getCommand(Request $httpRequest): CommandInterface + { + return $this->transformHttpRequest($httpRequest)->getCommand(); + } + + private function transformHttpRequest(Request $httpRequest): RequestInterface + { + $hasVariantCode = $httpRequest->request->has('variantCode'); + $hasOptionCode = $httpRequest->request->has('options'); + + if (!$hasVariantCode && !$hasOptionCode) { + return PutSimpleItemToCartRequest::fromHttpRequest($httpRequest); + } + + if ($hasVariantCode && !$hasOptionCode) { + return PutVariantBasedConfigurableItemToCartRequest::fromHttpRequest($httpRequest); + } + + if (!$hasVariantCode && $hasOptionCode) { + return PutOptionBasedConfigurableItemToCartRequest::fromHttpRequest($httpRequest); + } + + throw new NotFoundHttpException('Variant not found for given configuration'); + } +} diff --git a/src/Controller/Cart/PutItemToCartAction.php b/src/Controller/Cart/PutItemToCartAction.php index 034257594..b05d20316 100644 --- a/src/Controller/Cart/PutItemToCartAction.php +++ b/src/Controller/Cart/PutItemToCartAction.php @@ -9,19 +9,14 @@ use Sylius\ShopApiPlugin\Command\Cart\PutOptionBasedConfigurableItemToCart; use Sylius\ShopApiPlugin\Command\Cart\PutSimpleItemToCart; use Sylius\ShopApiPlugin\Command\Cart\PutVariantBasedConfigurableItemToCart; +use Sylius\ShopApiPlugin\CommandProvider\CommandProviderInterface; 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; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Messenger\MessageBusInterface; -use Symfony\Component\Validator\Validator\ValidatorInterface; final class PutItemToCartAction { @@ -31,9 +26,6 @@ final class PutItemToCartAction /** @var MessageBusInterface */ private $bus; - /** @var ValidatorInterface */ - private $validator; - /** @var ValidationErrorViewFactoryInterface */ private $validationErrorViewFactory; @@ -43,20 +35,23 @@ final class PutItemToCartAction /** @var RequestCartTokenNormalizerInterface */ private $requestCartTokenNormalizer; + /** @var CommandProviderInterface */ + private $putItemToCartCommandProvider; + public function __construct( ViewHandlerInterface $viewHandler, MessageBusInterface $bus, - ValidatorInterface $validator, ValidationErrorViewFactoryInterface $validationErrorViewFactory, CartViewRepositoryInterface $cartQuery, - RequestCartTokenNormalizerInterface $requestCartTokenNormalizer + RequestCartTokenNormalizerInterface $requestCartTokenNormalizer, + CommandProviderInterface $putItemToCartCommandProvider ) { $this->viewHandler = $viewHandler; $this->bus = $bus; - $this->validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; $this->cartQuery = $cartQuery; $this->requestCartTokenNormalizer = $requestCartTokenNormalizer; + $this->putItemToCartCommandProvider = $putItemToCartCommandProvider; } public function __invoke(Request $request): Response @@ -67,10 +62,7 @@ public function __invoke(Request $request): Response throw new BadRequestHttpException($exception->getMessage()); } - $commandRequest = $this->provideCommandRequest($request); - - $validationResults = $this->validator->validate($commandRequest); - + $validationResults = $this->putItemToCartCommandProvider->validate($request); if (0 !== count($validationResults)) { return $this->viewHandler->handle(View::create( $this->validationErrorViewFactory->create($validationResults), @@ -79,7 +71,7 @@ public function __invoke(Request $request): Response } /** @var PutOptionBasedConfigurableItemToCart|PutSimpleItemToCart|PutVariantBasedConfigurableItemToCart $command */ - $command = $commandRequest->getCommand(); + $command = $this->putItemToCartCommandProvider->getCommand($request); $this->bus->dispatch($command); try { @@ -91,24 +83,4 @@ public function __invoke(Request $request): Response throw new BadRequestHttpException($exception->getMessage()); } } - - private function provideCommandRequest(Request $request): RequestInterface - { - $hasVariantCode = $request->request->has('variantCode'); - $hasOptionCode = $request->request->has('options'); - - if (!$hasVariantCode && !$hasOptionCode) { - return PutSimpleItemToCartRequest::fromHttpRequest($request); - } - - if ($hasVariantCode && !$hasOptionCode) { - return PutVariantBasedConfigurableItemToCartRequest::fromHttpRequest($request); - } - - if (!$hasVariantCode && $hasOptionCode) { - return PutOptionBasedConfigurableItemToCartRequest::fromHttpRequest($request); - } - - throw new NotFoundHttpException('Variant not found for given configuration'); - } } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 8489d5c00..7ce400995 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -8,10 +8,10 @@ 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\Request\Product\AddProductReviewByCodeRequest; +use Sylius\ShopApiPlugin\Request\Product\AddProductReviewBySlugRequest; use Sylius\ShopApiPlugin\View; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; @@ -91,7 +91,7 @@ private function buildRequestClassesNode(ArrayNodeDefinition $rootNode): void ->arrayNode('request_classes') ->addDefaultsIfNotSet() ->children() - ->scalarNode('add_coupon')->defaultValue(AddCouponRequest::class)->end() + ->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() diff --git a/src/Resources/config/services/actions/cart.xml b/src/Resources/config/services/actions/cart.xml index 18c6cd092..a52b75c5b 100644 --- a/src/Resources/config/services/actions/cart.xml +++ b/src/Resources/config/services/actions/cart.xml @@ -95,10 +95,10 @@ > - + + + + +