Skip to content

Commit

Permalink
[Customer] Refactor customer 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 c6b0d34
Show file tree
Hide file tree
Showing 29 changed files with 342 additions and 117 deletions.
12 changes: 8 additions & 4 deletions spec/CommandProvider/ChannelBasedCommandProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ function it_validates_request(
$channel->getCode()->willReturn('WEB_GB');

$validator
->validate(TestChannelBasedRequest::fromHttpRequestAndChannel(
$httpRequest->getWrappedObject(),
$channel->getWrappedObject()
))
->validate(
TestChannelBasedRequest::fromHttpRequestAndChannel(
$httpRequest->getWrappedObject(),
$channel->getWrappedObject()
),
null,
null
)
->willReturn($constraintViolationList)
;

Expand Down
2 changes: 1 addition & 1 deletion spec/CommandProvider/DefaultCommandProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function it_validates_request(
$httpRequest->attributes = new ParameterBag(['token' => 'sample_cart_token']);

$validator
->validate(TestRequest::fromHttpRequest($httpRequest->getWrappedObject()))
->validate(TestRequest::fromHttpRequest($httpRequest->getWrappedObject()), null, null)
->willReturn($constraintViolationList)
;

Expand Down
4 changes: 3 additions & 1 deletion src/Command/Customer/GenerateResetPasswordToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Sylius\ShopApiPlugin\Command\Customer;

class GenerateResetPasswordToken
use Sylius\ShopApiPlugin\Command\CommandInterface;

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

namespace Sylius\ShopApiPlugin\Command\Customer;

class RegisterCustomer
use Sylius\ShopApiPlugin\Command\CommandInterface;

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

namespace Sylius\ShopApiPlugin\Command\Customer;

class SendResetPasswordToken
use Sylius\ShopApiPlugin\Command\CommandInterface;

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

namespace Sylius\ShopApiPlugin\Command\Customer;

class SendVerificationToken
use Sylius\ShopApiPlugin\Command\CommandInterface;

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

namespace Sylius\ShopApiPlugin\Command\Customer;

class UpdateCustomer
use Sylius\ShopApiPlugin\Command\CommandInterface;

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

namespace Sylius\ShopApiPlugin\Command\Customer;

class VerifyAccount
use Sylius\ShopApiPlugin\Command\CommandInterface;

class VerifyAccount implements CommandInterface
{
/** @var string */
protected $token;
Expand Down
10 changes: 7 additions & 3 deletions src/CommandProvider/ChannelBasedCommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ public function __construct(string $requestClass, ValidatorInterface $validator)
$this->validator = $validator;
}

public function validate(Request $httpRequest, ChannelInterface $channel): ConstraintViolationListInterface
{
return $this->validator->validate($this->transformHttpRequest($httpRequest, $channel));
public function validate(
Request $httpRequest,
ChannelInterface $channel,
array $constraints = null,
array $groups = null
): ConstraintViolationListInterface {
return $this->validator->validate($this->transformHttpRequest($httpRequest, $channel), $constraints, $groups);
}

public function getCommand(Request $httpRequest, ChannelInterface $channel): CommandInterface
Expand Down
7 changes: 6 additions & 1 deletion src/CommandProvider/ChannelBasedCommandProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@

interface ChannelBasedCommandProviderInterface
{
public function validate(Request $httpRequest, ChannelInterface $channel): ConstraintViolationListInterface;
public function validate(
Request $httpRequest,
ChannelInterface $channel,
array $constraints = null,
array $groups = null
): ConstraintViolationListInterface;

public function getCommand(Request $httpRequest, ChannelInterface $channel): CommandInterface;
}
2 changes: 1 addition & 1 deletion src/CommandProvider/CommandProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

interface CommandProviderInterface
{
public function validate(Request $httpRequest): ConstraintViolationListInterface;
public function validate(Request $httpRequest, array $constraints = null, array $groups = null): ConstraintViolationListInterface;

public function getCommand(Request $httpRequest): CommandInterface;
}
6 changes: 4 additions & 2 deletions src/CommandProvider/DefaultCommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Sylius\ShopApiPlugin\Command\CommandInterface;
use Sylius\ShopApiPlugin\Request\RequestInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Webmozart\Assert\Assert;
Expand All @@ -27,9 +29,9 @@ public function __construct(string $requestClass, ValidatorInterface $validator)
$this->validator = $validator;
}

public function validate(Request $httpRequest): ConstraintViolationListInterface
public function validate(Request $httpRequest, array $constraints = null, array $groups = null): ConstraintViolationListInterface
{
return $this->validator->validate($this->transformHttpRequest($httpRequest));
return $this->validator->validate($this->transformHttpRequest($httpRequest), $constraints, $groups);
}

public function getCommand(Request $httpRequest): CommandInterface
Expand Down
24 changes: 11 additions & 13 deletions src/Controller/Customer/RegisterCustomerAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\ShopApiPlugin\CommandProvider\ChannelBasedCommandProviderInterface;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Request\Customer\RegisterCustomerRequest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

final class RegisterCustomerAction
{
Expand All @@ -22,45 +22,43 @@ final class RegisterCustomerAction
/** @var MessageBusInterface */
private $bus;

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

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

/** @var ChannelContextInterface */
private $channelContext;

/** @var ChannelBasedCommandProviderInterface */
private $registerCustomerCommandProvider;

public function __construct(
ViewHandlerInterface $viewHandler,
MessageBusInterface $bus,
ValidatorInterface $validator,
ValidationErrorViewFactoryInterface $validationErrorViewFactory,
ChannelContextInterface $channelContext
ChannelContextInterface $channelContext,
ChannelBasedCommandProviderInterface $registerCustomerCommandProvider
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->validator = $validator;
$this->validationErrorViewFactory = $validationErrorViewFactory;
$this->channelContext = $channelContext;
$this->registerCustomerCommandProvider = $registerCustomerCommandProvider;
}

public function __invoke(Request $request): Response
{
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();

$registerCustomerRequest = new RegisterCustomerRequest($request, $channel->getCode());

$validationResults = $this->validator->validate($registerCustomerRequest);

$validationResults = $this->registerCustomerCommandProvider->validate($request, $channel);
if (0 !== count($validationResults)) {
return $this->viewHandler->handle(View::create(
$this->validationErrorViewFactory->create($validationResults),
Response::HTTP_BAD_REQUEST
));
}

$this->bus->dispatch($registerCustomerRequest->getCommand());
$this->bus->dispatch($this->registerCustomerCommandProvider->getCommand($request, $channel));

return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
}
Expand Down
22 changes: 17 additions & 5 deletions src/Controller/Customer/RequestPasswordResettingAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\ShopApiPlugin\Command\Customer\GenerateResetPasswordToken;
use Sylius\ShopApiPlugin\Command\Customer\SendResetPasswordToken;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\ShopApiPlugin\CommandProvider\ChannelBasedCommandProviderInterface;
use Sylius\ShopApiPlugin\CommandProvider\CommandProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
Expand All @@ -24,22 +25,33 @@ final class RequestPasswordResettingAction
/** @var ChannelContextInterface */
private $channelContext;

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

/** @var ChannelBasedCommandProviderInterface */
private $sendResetPasswordTokenCommandProvider;

public function __construct(
ViewHandlerInterface $viewHandler,
MessageBusInterface $bus,
ChannelContextInterface $channelContext
ChannelContextInterface $channelContext,
CommandProviderInterface $generateResetPasswordTokenCommandProvider,
ChannelBasedCommandProviderInterface $sendResetPasswordTokenCommandProvider
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->channelContext = $channelContext;
$this->generateResetPasswordTokenCommandProvider = $generateResetPasswordTokenCommandProvider;
$this->sendResetPasswordTokenCommandProvider = $sendResetPasswordTokenCommandProvider;
}

public function __invoke(Request $request): Response
{
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();

$this->bus->dispatch(new GenerateResetPasswordToken($request->request->get('email')));
$this->bus->dispatch(new SendResetPasswordToken($request->request->get('email'), $channel->getCode()));
$this->bus->dispatch($this->generateResetPasswordTokenCommandProvider->getCommand($request));
$this->bus->dispatch($this->sendResetPasswordTokenCommandProvider->getCommand($request, $channel));

return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
}
Expand Down
24 changes: 11 additions & 13 deletions src/Controller/Customer/ResendVerificationTokenAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\ShopApiPlugin\CommandProvider\ChannelBasedCommandProviderInterface;
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use Sylius\ShopApiPlugin\Request\Customer\ResendVerificationTokenRequest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

final class ResendVerificationTokenAction
{
Expand All @@ -22,45 +22,43 @@ final class ResendVerificationTokenAction
/** @var MessageBusInterface */
private $bus;

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

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

/** @var ChannelContextInterface */
private $channelContext;

/** @var ChannelBasedCommandProviderInterface */
private $resetVerificationTokenCommandProvider;

public function __construct(
ViewHandlerInterface $viewHandler,
MessageBusInterface $bus,
ValidatorInterface $validator,
ValidationErrorViewFactoryInterface $validationErrorViewFactory,
ChannelContextInterface $channelContext
ChannelContextInterface $channelContext,
ChannelBasedCommandProviderInterface $resetVerificationTokenCommandProvider
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->validator = $validator;
$this->validationErrorViewFactory = $validationErrorViewFactory;
$this->channelContext = $channelContext;
$this->resetVerificationTokenCommandProvider = $resetVerificationTokenCommandProvider;
}

public function __invoke(Request $request): Response
{
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();

$resendVerificationTokenRequest = new ResendVerificationTokenRequest($request, $channel->getCode());

$validationResults = $this->validator->validate($resendVerificationTokenRequest);

$validationResults = $this->resetVerificationTokenCommandProvider->validate($request, $channel);
if (0 !== count($validationResults)) {
return $this->viewHandler->handle(View::create(
$this->validationErrorViewFactory->create($validationResults),
Response::HTTP_BAD_REQUEST
));
}

$this->bus->dispatch($resendVerificationTokenRequest->getCommand());
$this->bus->dispatch($this->resetVerificationTokenCommandProvider->getCommand($request, $channel));

return $this->viewHandler->handle(View::create(null, Response::HTTP_CREATED));
}
Expand Down
Loading

0 comments on commit c6b0d34

Please sign in to comment.