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

[Checkout] Refactor checkout actions to use command providers #491

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
4 changes: 3 additions & 1 deletion src/Command/Cart/AssignCustomerToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Sylius\ShopApiPlugin\Command\Cart;

class AssignCustomerToCart
use Sylius\ShopApiPlugin\Command\CommandInterface;

class AssignCustomerToCart implements CommandInterface
{
/** @var string */
protected $orderToken;
Expand Down
35 changes: 16 additions & 19 deletions src/Controller/Checkout/AddressAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sylius\ShopApiPlugin\CommandProvider\CommandProviderInterface;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Request\Checkout\AddressOrderRequest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

final class AddressAction
{
Expand All @@ -21,38 +20,36 @@ final class AddressAction
/** @var MessageBusInterface */
private $bus;

/** @var ValidatorInterface */
private $validator;

/** @var ValidationErrorViewFactoryInterface */
private $validationErrorViewFactory;

/** @var CommandProviderInterface */
private $addressOrderCommandProvider;

public function __construct(
ViewHandlerInterface $viewHandler,
MessageBusInterface $bus,
ValidatorInterface $validator,
ValidationErrorViewFactoryInterface $validationErrorViewFactory
ValidationErrorViewFactoryInterface $validationErrorViewFactory,
CommandProviderInterface $addressOrderCommandProvider
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->validator = $validator;
$this->validationErrorViewFactory = $validationErrorViewFactory;
$this->addressOrderCommandProvider = $addressOrderCommandProvider;
}

public function __invoke(Request $request): Response
{
$addressOrderRequest = new AddressOrderRequest($request);

$validationResults = $this->validator->validate($addressOrderRequest);
if (0 === count($validationResults)) {
$this->bus->dispatch($addressOrderRequest->getCommand());

return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
$validationResults = $this->addressOrderCommandProvider->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
));
$this->bus->dispatch($this->addressOrderCommandProvider->getCommand($request));

return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
}
}
19 changes: 11 additions & 8 deletions src/Controller/Checkout/ChoosePaymentMethodAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sylius\ShopApiPlugin\Command\Cart\ChoosePaymentMethod;
use Sylius\ShopApiPlugin\CommandProvider\CommandProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
Expand All @@ -19,19 +19,22 @@ final class ChoosePaymentMethodAction
/** @var MessageBusInterface */
private $bus;

public function __construct(ViewHandlerInterface $viewHandler, MessageBusInterface $bus)
{
/** @var CommandProviderInterface */
private $choosePaymentMethodCommandProvider;

public function __construct(
ViewHandlerInterface $viewHandler,
MessageBusInterface $bus,
CommandProviderInterface $choosePaymentMethodCommandProvider
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->choosePaymentMethodCommandProvider = $choosePaymentMethodCommandProvider;
}

public function __invoke(Request $request): Response
{
$this->bus->dispatch(new ChoosePaymentMethod(
$request->attributes->get('token'),
$request->attributes->get('paymentId'),
$request->request->get('method')
));
$this->bus->dispatch($this->choosePaymentMethodCommandProvider->getCommand($request));

return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
}
Expand Down
19 changes: 11 additions & 8 deletions src/Controller/Checkout/ChooseShippingMethodAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sylius\ShopApiPlugin\Command\Cart\ChooseShippingMethod;
use Sylius\ShopApiPlugin\CommandProvider\CommandProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
Expand All @@ -19,19 +19,22 @@ final class ChooseShippingMethodAction
/** @var MessageBusInterface */
private $bus;

public function __construct(ViewHandlerInterface $viewHandler, MessageBusInterface $bus)
{
/** @var CommandProviderInterface */
private $chooseShippingMethodCommandProvider;

public function __construct(
ViewHandlerInterface $viewHandler,
MessageBusInterface $bus,
CommandProviderInterface $chooseShippingMethodCommandProvider
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->chooseShippingMethodCommandProvider = $chooseShippingMethodCommandProvider;
}

public function __invoke(Request $request): Response
{
$this->bus->dispatch(new ChooseShippingMethod(
$request->attributes->get('token'),
$request->attributes->get('shippingId'),
$request->request->get('method')
));
$this->bus->dispatch($this->chooseShippingMethodCommandProvider->getCommand($request));

return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
}
Expand Down
28 changes: 18 additions & 10 deletions src/Controller/Checkout/CompleteOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sylius\ShopApiPlugin\Command\Cart\AssignCustomerToCart;
use Sylius\ShopApiPlugin\Command\Cart\CompleteOrder;
use Sylius\ShopApiPlugin\CommandProvider\CommandProviderInterface;
use Sylius\ShopApiPlugin\Exception\WrongUserException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -22,23 +21,32 @@ final class CompleteOrderAction
/** @var MessageBusInterface */
private $bus;

public function __construct(ViewHandlerInterface $viewHandler, MessageBusInterface $bus)
{
/** @var CommandProviderInterface */
private $assignCustomerToCartCommandProvider;

/** @var CommandProviderInterface */
private $completeOrderCommandProvider;

public function __construct(
ViewHandlerInterface $viewHandler,
MessageBusInterface $bus,
CommandProviderInterface $assignCustomerToCartCommandProvider,
CommandProviderInterface $completeOrderCommandProvider
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->assignCustomerToCartCommandProvider = $assignCustomerToCartCommandProvider;
$this->completeOrderCommandProvider = $completeOrderCommandProvider;
}

public function __invoke(Request $request): Response
{
try {
$orderToken = $request->attributes->get('token');
$email = $request->request->get('email');

if (null !== $email) {
$this->bus->dispatch(new AssignCustomerToCart($orderToken, $email));
if (null !== $request->request->get('email')) {
$this->bus->dispatch($this->assignCustomerToCartCommandProvider->getCommand($request));
}

$this->bus->dispatch(new CompleteOrder($orderToken, $request->request->get('notes')));
$this->bus->dispatch($this->completeOrderCommandProvider->getCommand($request));
} catch (HandlerFailedException $exception) {
$previousException = $exception->getPrevious();

Expand Down
10 changes: 10 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
namespace Sylius\ShopApiPlugin\DependencyInjection;

use Sylius\ShopApiPlugin\Request\Cart\AddCouponRequest;
use Sylius\ShopApiPlugin\Request\Cart\AssignCustomerToCartRequest;
use Sylius\ShopApiPlugin\Request\Cart\ChangeItemQuantityRequest;
use Sylius\ShopApiPlugin\Request\Cart\DropCartRequest;
use Sylius\ShopApiPlugin\Request\Cart\PickupCartRequest;
use Sylius\ShopApiPlugin\Request\Cart\RemoveCouponRequest;
use Sylius\ShopApiPlugin\Request\Cart\RemoveItemFromCartRequest;
use Sylius\ShopApiPlugin\Request\Checkout\AddressOrderRequest;
use Sylius\ShopApiPlugin\Request\Checkout\ChoosePaymentMethodRequest;
use Sylius\ShopApiPlugin\Request\Checkout\ChooseShippingMethodRequest;
use Sylius\ShopApiPlugin\Request\Checkout\CompleteOrderRequest;
use Sylius\ShopApiPlugin\Request\Product\AddProductReviewByCodeRequest;
use Sylius\ShopApiPlugin\Request\Product\AddProductReviewBySlugRequest;
use Sylius\ShopApiPlugin\View;
Expand Down Expand Up @@ -94,7 +99,12 @@ private function buildRequestClassesNode(ArrayNodeDefinition $rootNode): void
->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('address_order')->defaultValue(AddressOrderRequest::class)->end()
->scalarNode('assign_customer_to_cart')->defaultValue(AssignCustomerToCartRequest::class)->end()
->scalarNode('change_item_quantity')->defaultValue(ChangeItemQuantityRequest::class)->end()
->scalarNode('choose_payment_method')->defaultValue(ChoosePaymentMethodRequest::class)->end()
->scalarNode('choose_shipping_method')->defaultValue(ChooseShippingMethodRequest::class)->end()
->scalarNode('complete_order')->defaultValue(CompleteOrderRequest::class)->end()
->scalarNode('drop_cart')->defaultValue(DropCartRequest::class)->end()
->scalarNode('pickup_cart')->defaultValue(PickupCartRequest::class)->end()
->scalarNode('remove_coupon')->defaultValue(RemoveCouponRequest::class)->end()
Expand Down
35 changes: 35 additions & 0 deletions src/Request/Cart/AssignCustomerToCartRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Sylius\ShopApiPlugin\Request\Cart;

use Sylius\ShopApiPlugin\Command\Cart\AssignCustomerToCart;
use Sylius\ShopApiPlugin\Command\CommandInterface;
use Sylius\ShopApiPlugin\Request\RequestInterface;
use Symfony\Component\HttpFoundation\Request;

class AssignCustomerToCartRequest implements RequestInterface
{
/** @var string|null */
protected $token;

/** @var string|null */
protected $email;

private function __construct(Request $request)
{
$this->token = $request->attributes->get('token');
$this->email = $request->request->get('email');
}

public static function fromHttpRequest(Request $request): RequestInterface
{
return new self($request);
}

public function getCommand(): CommandInterface
{
return new AssignCustomerToCart($this->token, $this->email);
}
}
13 changes: 10 additions & 3 deletions src/Request/Checkout/AddressOrderRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace Sylius\ShopApiPlugin\Request\Checkout;

use Sylius\ShopApiPlugin\Command\Cart\AddressOrder;
use Sylius\ShopApiPlugin\Command\CommandInterface;
use Sylius\ShopApiPlugin\Model\Address;
use Sylius\ShopApiPlugin\Request\RequestInterface;
use Symfony\Component\HttpFoundation\Request;

class AddressOrderRequest
class AddressOrderRequest implements RequestInterface
{
/** @var string|null */
protected $token;
Expand All @@ -19,14 +21,19 @@ class AddressOrderRequest
/** @var array|null */
protected $billingAddress;

public function __construct(Request $request)
private function __construct(Request $request)
{
$this->token = $request->attributes->get('token');
$this->shippingAddress = $request->request->get('shippingAddress');
$this->billingAddress = $request->request->get('billingAddress') ?: $request->request->get('shippingAddress');
}

public function getCommand(): AddressOrder
public static function fromHttpRequest(Request $request): RequestInterface
{
return new self($request);
}

public function getCommand(): CommandInterface
{
return new AddressOrder(
$this->token,
Expand Down
39 changes: 39 additions & 0 deletions src/Request/Checkout/ChoosePaymentMethodRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Sylius\ShopApiPlugin\Request\Checkout;

use Sylius\ShopApiPlugin\Command\Cart\ChoosePaymentMethod;
use Sylius\ShopApiPlugin\Command\CommandInterface;
use Sylius\ShopApiPlugin\Request\RequestInterface;
use Symfony\Component\HttpFoundation\Request;

class ChoosePaymentMethodRequest implements RequestInterface
{
/** @var string|null */
protected $token;

/** @var string|null */
protected $paymentId;

/** @var string|null */
protected $method;

private function __construct(Request $request)
{
$this->token = $request->attributes->get('token');
$this->paymentId = $request->attributes->get('paymentId');
$this->method = $request->request->get('method');
}

public static function fromHttpRequest(Request $request): RequestInterface
{
return new self($request);
}

public function getCommand(): CommandInterface
{
return new ChoosePaymentMethod($this->token, $this->paymentId, $this->method);
}
}
39 changes: 39 additions & 0 deletions src/Request/Checkout/ChooseShippingMethodRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Sylius\ShopApiPlugin\Request\Checkout;

use Sylius\ShopApiPlugin\Command\Cart\ChooseShippingMethod;
use Sylius\ShopApiPlugin\Command\CommandInterface;
use Sylius\ShopApiPlugin\Request\RequestInterface;
use Symfony\Component\HttpFoundation\Request;

class ChooseShippingMethodRequest implements RequestInterface
{
/** @var string|null */
protected $token;

/** @var string|null */
protected $shippingId;

/** @var string|null */
protected $method;

private function __construct(Request $request)
{
$this->token = $request->attributes->get('token');
$this->shippingId = $request->attributes->get('shippingId');
$this->method = $request->request->get('method');
}

public static function fromHttpRequest(Request $request): RequestInterface
{
return new self($request);
}

public function getCommand(): CommandInterface
{
return new ChooseShippingMethod($this->token, $this->shippingId, $this->method);
}
}
Loading