Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cart] Refactor cart actions to use command providers #479

Merged
merged 2 commits into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions spec/CommandProvider/PutItemToCartCommandProviderSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace spec\Sylius\ShopApiPlugin\CommandProvider;

use PhpSpec\ObjectBehavior;
use Sylius\ShopApiPlugin\CommandProvider\CommandProviderInterface;
use Sylius\ShopApiPlugin\Request\Cart\PutOptionBasedConfigurableItemToCartRequest;
use Sylius\ShopApiPlugin\Request\Cart\PutSimpleItemToCartRequest;
use Sylius\ShopApiPlugin\Request\Cart\PutVariantBasedConfigurableItemToCartRequest;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

final class PutItemToCartCommandProviderSpec extends ObjectBehavior
{
function let(ValidatorInterface $validator): void
{
$this->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);
}
}
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
56 changes: 56 additions & 0 deletions src/CommandProvider/PutItemToCartCommandProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Sylius\ShopApiPlugin\CommandProvider;

use Sylius\ShopApiPlugin\Command\CommandInterface;
use Sylius\ShopApiPlugin\Request\Cart\PutOptionBasedConfigurableItemToCartRequest;
use Sylius\ShopApiPlugin\Request\Cart\PutSimpleItemToCartRequest;
use Sylius\ShopApiPlugin\Request\Cart\PutVariantBasedConfigurableItemToCartRequest;
use Sylius\ShopApiPlugin\Request\RequestInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

final class PutItemToCartCommandProvider implements CommandProviderInterface
{
/** @var ValidatorInterface */
private $validator;

public function __construct(ValidatorInterface $validator)
{
$this->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');
}
}
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));
}
}
Loading