Skip to content

Commit

Permalink
Add missing command requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Jan 21, 2019
1 parent 6f81a7f commit b4018f8
Show file tree
Hide file tree
Showing 26 changed files with 263 additions and 51 deletions.
1 change: 0 additions & 1 deletion src/Controller/Cart/ChangeItemQuantityAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Sylius\ShopApiPlugin\Command\ChangeItemQuantity;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Sylius\ShopApiPlugin\Request\ChangeItemQuantityRequest;
use Sylius\ShopApiPlugin\ViewRepository\Cart\CartViewRepositoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down
1 change: 0 additions & 1 deletion src/Controller/Cart/DropCartAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Sylius\ShopApiPlugin\Command\DropCart;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Sylius\ShopApiPlugin\Request\DropCartRequest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface;
Expand Down
1 change: 0 additions & 1 deletion src/Controller/Cart/PickupAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Sylius\ShopApiPlugin\Command\PickupCart;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Sylius\ShopApiPlugin\Request\PickupCartRequest;
use Sylius\ShopApiPlugin\ViewRepository\Cart\CartViewRepositoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down
3 changes: 0 additions & 3 deletions src/Controller/Cart/PutItemToCartAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
use Sylius\ShopApiPlugin\Normalizer\RequestCartTokenNormalizerInterface;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Sylius\ShopApiPlugin\Request\CommandRequestInterface;
use Sylius\ShopApiPlugin\Request\PutOptionBasedConfigurableItemToCartRequest;
use Sylius\ShopApiPlugin\Request\PutSimpleItemToCartRequest;
use Sylius\ShopApiPlugin\Request\PutVariantBasedConfigurableItemToCartRequest;
use Sylius\ShopApiPlugin\ViewRepository\Cart\CartViewRepositoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down
1 change: 0 additions & 1 deletion src/Controller/Cart/RemoveCouponAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Sylius\ShopApiPlugin\Command\RemoveCoupon;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Sylius\ShopApiPlugin\Request\RemoveCouponRequest;
use Sylius\ShopApiPlugin\ViewRepository\Cart\CartViewRepositoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down
1 change: 0 additions & 1 deletion src/Controller/Cart/RemoveItemFromCartAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Sylius\ShopApiPlugin\Command\RemoveItemFromCart;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Sylius\ShopApiPlugin\Request\RemoveItemFromCartRequest;
use Sylius\ShopApiPlugin\ViewRepository\Cart\CartViewRepositoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down
21 changes: 13 additions & 8 deletions src/Controller/Checkout/AddressAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use FOS\RestBundle\View\ViewHandlerInterface;
use League\Tactician\CommandBus;
use Sylius\ShopApiPlugin\Command\AddressOrder;
use Sylius\ShopApiPlugin\Model\Address;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -20,19 +20,24 @@ final class AddressAction
/** @var CommandBus */
private $bus;

public function __construct(ViewHandlerInterface $viewHandler, CommandBus $bus)
{
/** @var CommandRequestParserInterface */
private $commandRequestParser;

public function __construct(
ViewHandlerInterface $viewHandler,
CommandBus $bus,
CommandRequestParserInterface $commandRequestParser
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->commandRequestParser = $commandRequestParser;
}

public function __invoke(Request $request): Response
{
$this->bus->handle(new AddressOrder(
$request->attributes->get('token'),
Address::createFromArray($request->request->get('shippingAddress')),
Address::createFromArray($request->request->get('billingAddress') ?: $request->request->get('shippingAddress'))
));
$commandRequest = $this->commandRequestParser->parse($request, AddressOrder::class);

$this->bus->handle($commandRequest->getCommand());

return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
}
Expand Down
20 changes: 13 additions & 7 deletions src/Controller/Checkout/ChoosePaymentMethodAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use FOS\RestBundle\View\ViewHandlerInterface;
use League\Tactician\CommandBus;
use Sylius\ShopApiPlugin\Command\ChoosePaymentMethod;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -19,19 +20,24 @@ final class ChoosePaymentMethodAction
/** @var CommandBus */
private $bus;

public function __construct(ViewHandlerInterface $viewHandler, CommandBus $bus)
{
/** @var CommandRequestParserInterface */
private $commandRequestParser;

public function __construct(
ViewHandlerInterface $viewHandler,
CommandBus $bus,
CommandRequestParserInterface $commandRequestParser
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->commandRequestParser = $commandRequestParser;
}

public function __invoke(Request $request): Response
{
$this->bus->handle(new ChoosePaymentMethod(
$request->attributes->get('token'),
$request->attributes->get('paymentId'),
$request->request->get('method')
));
$comandRequest = $this->commandRequestParser->parse($request, ChoosePaymentMethod::class);

$this->bus->handle($comandRequest->getCommand());

return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
}
Expand Down
20 changes: 13 additions & 7 deletions src/Controller/Checkout/ChooseShippingMethodAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use FOS\RestBundle\View\ViewHandlerInterface;
use League\Tactician\CommandBus;
use Sylius\ShopApiPlugin\Command\ChooseShippingMethod;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -19,19 +20,24 @@ final class ChooseShippingMethodAction
/** @var CommandBus */
private $bus;

public function __construct(ViewHandlerInterface $viewHandler, CommandBus $bus)
{
/** @var CommandRequestParserInterface */
private $commandRequestParser;

public function __construct(
ViewHandlerInterface $viewHandler,
CommandBus $bus,
CommandRequestParserInterface $commandRequestParser
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->commandRequestParser = $commandRequestParser;
}

public function __invoke(Request $request): Response
{
$this->bus->handle(new ChooseShippingMethod(
$request->attributes->get('token'),
$request->attributes->get('shippingId'),
$request->request->get('method')
));
$commandRequest = $this->commandRequestParser->parse($request, ChooseShippingMethod::class);

$this->bus->handle($commandRequest->getCommand());

return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
}
Expand Down
34 changes: 23 additions & 11 deletions src/Controller/Checkout/CompleteOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use League\Tactician\CommandBus;
use Sylius\ShopApiPlugin\Command\CompleteOrder;
use Sylius\ShopApiPlugin\Exception\WrongUserException;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -24,30 +25,29 @@ final class CompleteOrderAction
/** @var LoggedInShopUserProviderInterface */
private $loggedInUserProvider;

/** @var CommandRequestParserInterface */
private $commandRequestParser;

public function __construct(
ViewHandlerInterface $viewHandler,
CommandBus $bus,
LoggedInShopUserProviderInterface $loggedInUserProvider
LoggedInShopUserProviderInterface $loggedInUserProvider,
CommandRequestParserInterface $commandRequestParser
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->loggedInUserProvider = $loggedInUserProvider;
$this->commandRequestParser = $commandRequestParser;
}

public function __invoke(Request $request): Response
{
if ($this->loggedInUserProvider->isUserLoggedIn()) {
$defaultEmail = $this->loggedInUserProvider->provide()->getEmail();
}
$this->setDefaultEmailOnRequestIfNeeded($request);

$commandRequest = $this->commandRequestParser->parse($request, CompleteOrder::class);

try {
$this->bus->handle(
new CompleteOrder(
$request->attributes->get('token'),
$request->request->get('email', $defaultEmail ?? null),
$request->request->get('notes')
)
);
$this->bus->handle($commandRequest->getCommand());
} catch (WrongUserException $notLoggedInException) {
return $this->viewHandler->handle(
View::create(
Expand All @@ -59,4 +59,16 @@ public function __invoke(Request $request): Response

return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
}

private function setDefaultEmailOnRequestIfNeeded(Request $request): void
{
$defaultEmail = null;
if ($this->loggedInUserProvider->isUserLoggedIn()) {
$defaultEmail = $this->loggedInUserProvider->provide()->getEmail();
}

if (!$request->request->has('email')) {
$request->request->set('email', $defaultEmail);
}
}
}
1 change: 0 additions & 1 deletion src/Controller/Customer/RegisterCustomerAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Sylius\ShopApiPlugin\Command\RegisterCustomer;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Sylius\ShopApiPlugin\Request\RegisterCustomerRequest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface;
Expand Down
12 changes: 9 additions & 3 deletions src/Controller/Customer/RequestPasswordResettingAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use League\Tactician\CommandBus;
use Sylius\ShopApiPlugin\Command\GenerateResetPasswordToken;
use Sylius\ShopApiPlugin\Command\SendResetPasswordToken;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -20,18 +21,23 @@ final class RequestPasswordResettingAction
/** @var CommandBus */
private $bus;

/** @var CommandRequestParserInterface */
private $commandRequestParser;

public function __construct(
ViewHandlerInterface $viewHandler,
CommandBus $bus
CommandBus $bus,
CommandRequestParserInterface $commandRequestParser
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->commandRequestParser = $commandRequestParser;
}

public function __invoke(Request $request): Response
{
$this->bus->handle(new GenerateResetPasswordToken($request->request->get('email')));
$this->bus->handle(new SendResetPasswordToken($request->request->get('email'), $request->attributes->get('channelCode')));
$this->bus->handle($this->commandRequestParser->parse($request, GenerateResetPasswordToken::class)->getCommand());
$this->bus->handle($this->commandRequestParser->parse($request, SendResetPasswordToken::class)->getCommand());

return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
}
Expand Down
1 change: 0 additions & 1 deletion src/Controller/Customer/ResendVerificationTokenAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Sylius\ShopApiPlugin\Command\SendVerificationToken;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Sylius\ShopApiPlugin\Request\ResendVerificationTokenRequest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface;
Expand Down
1 change: 0 additions & 1 deletion src/Controller/Customer/VerifyAccountAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Sylius\ShopApiPlugin\Command\VerifyAccount;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Sylius\ShopApiPlugin\Request\VerifyAccountRequest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface;
Expand Down
1 change: 0 additions & 1 deletion src/Controller/Product/AddReviewByCodeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Sylius\ShopApiPlugin\Command\AddProductReviewByCode;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Sylius\ShopApiPlugin\Request\AddProductReviewByCodeRequest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface;
Expand Down
1 change: 0 additions & 1 deletion src/Controller/Product/AddReviewBySlugAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Sylius\ShopApiPlugin\Command\AddProductReviewBySlug;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Parser\CommandRequestParserInterface;
use Sylius\ShopApiPlugin\Request\AddProductReviewBySlugRequest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface;
Expand Down
1 change: 0 additions & 1 deletion src/DependencyInjection/ShopApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;

final class ShopApiExtension extends Extension
{
Expand Down
33 changes: 33 additions & 0 deletions src/Request/AddressOrderRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Sylius\ShopApiPlugin\Request;

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

class AddressOrderRequest implements CommandRequestInterface
{
/** @var string */
protected $token;

/** @var Address */
protected $shippingAddress;

/** @var Address */
protected $billingAddress;

public function populateData(Request $request): void
{
$this->token = $request->attributes->get('token');
$this->shippingAddress = Address::createFromArray($request->request->get('shippingAddress'));
$this->billingAddress = Address::createFromArray($request->request->get('billingAddress') ?: $request->request->get('shippingAddress'));
}

public function getCommand(): object
{
return new AddressOrder($this->token, $this->shippingAddress, $this->billingAddress);
}
}
32 changes: 32 additions & 0 deletions src/Request/ChoosePaymentMethodRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Sylius\ShopApiPlugin\Request;

use Sylius\ShopApiPlugin\Command\ChoosePaymentMethod;
use Symfony\Component\HttpFoundation\Request;

class ChoosePaymentMethodRequest implements CommandRequestInterface
{
/** @var string */
protected $token;

/** @var int */
protected $paymentId;

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

public function populateData(Request $request): void
{
$this->token = $request->attributes->get('token');
$this->paymentId = $request->attributes->get('paymentId');
$this->method = $request->request->get('method');
}

public function getCommand(): object
{
return new ChoosePaymentMethod($this->token, $this->paymentId, $this->method);
}
}
Loading

0 comments on commit b4018f8

Please sign in to comment.