Skip to content

Commit

Permalink
[Cart] Refactor cart actions to use command providers
Browse files Browse the repository at this point in the history
  • Loading branch information
GSadee committed Jul 25, 2019
1 parent 192f633 commit 91bd466
Show file tree
Hide file tree
Showing 34 changed files with 326 additions and 167 deletions.
4 changes: 3 additions & 1 deletion src/Command/Cart/AddCoupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Sylius\ShopApiPlugin\Command\Cart;

class AddCoupon
use Sylius\ShopApiPlugin\Command\CommandInterface;

class AddCoupon implements CommandInterface
{
/** @var string */
protected $orderToken;
Expand Down
3 changes: 2 additions & 1 deletion src/Command/Cart/AddressOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/Command/Cart/ChangeItemQuantity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/Command/Cart/ChoosePaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Sylius\ShopApiPlugin\Command\Cart;

class ChoosePaymentMethod
use Sylius\ShopApiPlugin\Command\CommandInterface;

class ChoosePaymentMethod implements CommandInterface
{
/** @var mixed */
protected $paymentIdentifier;
Expand Down
4 changes: 3 additions & 1 deletion src/Command/Cart/ChooseShippingMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Sylius\ShopApiPlugin\Command\Cart;

class ChooseShippingMethod
use Sylius\ShopApiPlugin\Command\CommandInterface;

class ChooseShippingMethod implements CommandInterface
{
/** @var mixed */
protected $shipmentIdentifier;
Expand Down
4 changes: 3 additions & 1 deletion src/Command/Cart/CompleteOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Sylius\ShopApiPlugin\Command\Cart;

class CompleteOrder
use Sylius\ShopApiPlugin\Command\CommandInterface;

class CompleteOrder implements CommandInterface
{
/** @var string */
protected $orderToken;
Expand Down
3 changes: 2 additions & 1 deletion src/Command/Cart/PutOptionBasedConfigurableItemToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/Command/Cart/PutSimpleItemToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/Command/Cart/PutVariantBasedConfigurableItemToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/Command/Cart/RemoveCoupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Sylius\ShopApiPlugin\Command\Cart;

class RemoveCoupon
use Sylius\ShopApiPlugin\Command\CommandInterface;

class RemoveCoupon implements CommandInterface
{
/** @var string */
protected $orderToken;
Expand Down
4 changes: 3 additions & 1 deletion src/Command/Cart/RemoveItemFromCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Sylius\ShopApiPlugin\Command\Cart;

class RemoveItemFromCart
use Sylius\ShopApiPlugin\Command\CommandInterface;

class RemoveItemFromCart implements CommandInterface
{
/** @var string */
protected $orderToken;
Expand Down
48 changes: 25 additions & 23 deletions src/Controller/Cart/AddCouponAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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));
}
}
29 changes: 15 additions & 14 deletions src/Controller/Cart/ChangeItemQuantityAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);

Expand Down
1 change: 0 additions & 1 deletion src/Controller/Cart/EstimateShippingCostAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
30 changes: 17 additions & 13 deletions src/Controller/Cart/PutItemToCartAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
Loading

0 comments on commit 91bd466

Please sign in to comment.