refactor #13830 [API] Use the Calendar component in command handlers (coldic3)

This PR was merged into the 1.12-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | 
| License         | MIT

Commits
-------

b87a91618c [API] Use the Calendar component in command handlers
e945d76e29 [Maintenance][API] Update UPGRADE-API-1.12.md
This commit is contained in:
Grzegorz Sadowski 2022-04-08 08:23:59 +02:00 committed by GitHub
commit 642142b434
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 10 deletions

View file

@ -71,3 +71,28 @@ Here is how the response looks like:
Wrong parameters otherwise cause empty array `[]` in response and correct parameters return `paymentMethods` available for your `payment`. Wrong parameters otherwise cause empty array `[]` in response and correct parameters return `paymentMethods` available for your `payment`.
1. The 2nd parameter `MetadataInterface` has been removed from `src/Sylius/Bundle/ApiBundle/CommandHandler/Account/ResetPasswordHandler` and replaced by `Sylius\Component\User\Security\PasswordUpdaterInterface` (previously 3rd parameter). From now on a token TTL value must be used instead as the 3rd parameter. 1. The 2nd parameter `MetadataInterface` has been removed from `src/Sylius/Bundle/ApiBundle/CommandHandler/Account/ResetPasswordHandler` and replaced by `Sylius\Component\User\Security\PasswordUpdaterInterface` (previously 3rd parameter). From now on a token TTL value must be used instead as the 3rd parameter.
1. Constructor of `Sylius\Bundle\ApiBundle\CommandHandler\Account\RequestResetPasswordTokenHandler` has been extended with `Sylius\Calendar\Provider\DateTimeProviderInterface` argument:
```diff
public function __construct(
private UserRepositoryInterface $userRepository,
private MessageBusInterface $commandBus,
- private GeneratorInterface $generator
+ private GeneratorInterface $generator,
+ private DateTimeProviderInterface $calendar
) {
}
```
1. Constructor of `\Sylius\Bundle\ApiBundle\CommandHandler\Account\VerifyCustomerAccountHandler` has been extended with `Sylius\Calendar\Provider\DateTimeProviderInterface` argument:
```diff
- public function __construct(private RepositoryInterface $shopUserRepository)
- {
+ public function __construct(
+ private RepositoryInterface $shopUserRepository,
+ private DateTimeProviderInterface $calendar
+ ) {
}
```

View file

@ -15,6 +15,7 @@ namespace Sylius\Bundle\ApiBundle\CommandHandler\Account;
use Sylius\Bundle\ApiBundle\Command\Account\RequestResetPasswordToken; use Sylius\Bundle\ApiBundle\Command\Account\RequestResetPasswordToken;
use Sylius\Bundle\ApiBundle\Command\Account\SendResetPasswordEmail; use Sylius\Bundle\ApiBundle\Command\Account\SendResetPasswordEmail;
use Sylius\Calendar\Provider\DateTimeProviderInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface; use Sylius\Component\User\Repository\UserRepositoryInterface;
use Sylius\Component\User\Security\Generator\GeneratorInterface; use Sylius\Component\User\Security\Generator\GeneratorInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
@ -28,7 +29,8 @@ final class RequestResetPasswordTokenHandler implements MessageHandlerInterface
public function __construct( public function __construct(
private UserRepositoryInterface $userRepository, private UserRepositoryInterface $userRepository,
private MessageBusInterface $commandBus, private MessageBusInterface $commandBus,
private GeneratorInterface $generator private GeneratorInterface $generator,
private DateTimeProviderInterface $calendar
) { ) {
} }
@ -38,7 +40,7 @@ final class RequestResetPasswordTokenHandler implements MessageHandlerInterface
Assert::notNull($user); Assert::notNull($user);
$user->setPasswordResetToken($this->generator->generate()); $user->setPasswordResetToken($this->generator->generate());
$user->setPasswordRequestedAt(new \DateTime()); $user->setPasswordRequestedAt($this->calendar->now());
$this->commandBus->dispatch( $this->commandBus->dispatch(
new SendResetPasswordEmail( new SendResetPasswordEmail(

View file

@ -15,6 +15,7 @@ namespace Sylius\Bundle\ApiBundle\CommandHandler\Account;
use InvalidArgumentException; use InvalidArgumentException;
use Sylius\Bundle\ApiBundle\Command\Account\VerifyCustomerAccount; use Sylius\Bundle\ApiBundle\Command\Account\VerifyCustomerAccount;
use Sylius\Calendar\Provider\DateTimeProviderInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface; use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\User\Model\UserInterface; use Sylius\Component\User\Model\UserInterface;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
@ -23,8 +24,10 @@ use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
/** @experimental */ /** @experimental */
final class VerifyCustomerAccountHandler implements MessageHandlerInterface final class VerifyCustomerAccountHandler implements MessageHandlerInterface
{ {
public function __construct(private RepositoryInterface $shopUserRepository) public function __construct(
{ private RepositoryInterface $shopUserRepository,
private DateTimeProviderInterface $calendar
) {
} }
public function __invoke(VerifyCustomerAccount $command): JsonResponse public function __invoke(VerifyCustomerAccount $command): JsonResponse
@ -37,7 +40,7 @@ final class VerifyCustomerAccountHandler implements MessageHandlerInterface
); );
} }
$user->setVerifiedAt(new \DateTime()); $user->setVerifiedAt($this->calendar->now());
$user->setEmailVerificationToken(null); $user->setEmailVerificationToken(null);
$user->enable(); $user->enable();

View file

@ -146,6 +146,7 @@
<argument type="service" id="sylius.repository.shop_user" /> <argument type="service" id="sylius.repository.shop_user" />
<argument type="service" id="messenger.default_bus" /> <argument type="service" id="messenger.default_bus" />
<argument type="service" id="sylius.shop_user.token_generator.password_reset" /> <argument type="service" id="sylius.shop_user.token_generator.password_reset" />
<argument type="service" id="Sylius\Calendar\Provider\DateTimeProviderInterface" />
<tag name="messenger.message_handler" bus="sylius.command_bus" /> <tag name="messenger.message_handler" bus="sylius.command_bus" />
<tag name="messenger.message_handler" bus="sylius_default.bus" /> <tag name="messenger.message_handler" bus="sylius_default.bus" />
</service> </service>
@ -206,6 +207,7 @@
<service id="Sylius\Bundle\ApiBundle\CommandHandler\Account\VerifyCustomerAccountHandler"> <service id="Sylius\Bundle\ApiBundle\CommandHandler\Account\VerifyCustomerAccountHandler">
<argument type="service" id="sylius.repository.shop_user" /> <argument type="service" id="sylius.repository.shop_user" />
<argument type="service" id="Sylius\Calendar\Provider\DateTimeProviderInterface" />
<tag name="messenger.message_handler" bus="sylius.command_bus" /> <tag name="messenger.message_handler" bus="sylius.command_bus" />
<tag name="messenger.message_handler" bus="sylius_default.bus" /> <tag name="messenger.message_handler" bus="sylius_default.bus" />
</service> </service>

View file

@ -17,6 +17,7 @@ use PhpSpec\ObjectBehavior;
use Prophecy\Argument; use Prophecy\Argument;
use Sylius\Bundle\ApiBundle\Command\Account\RequestResetPasswordToken; use Sylius\Bundle\ApiBundle\Command\Account\RequestResetPasswordToken;
use Sylius\Bundle\ApiBundle\Command\Account\SendResetPasswordEmail; use Sylius\Bundle\ApiBundle\Command\Account\SendResetPasswordEmail;
use Sylius\Calendar\Provider\DateTimeProviderInterface;
use Sylius\Component\Core\Model\ShopUserInterface; use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface; use Sylius\Component\User\Repository\UserRepositoryInterface;
use Sylius\Component\User\Security\Generator\GeneratorInterface; use Sylius\Component\User\Security\Generator\GeneratorInterface;
@ -30,9 +31,10 @@ final class RequestResetPasswordTokenHandlerSpec extends ObjectBehavior
function let( function let(
UserRepositoryInterface $userRepository, UserRepositoryInterface $userRepository,
MessageBusInterface $messageBus, MessageBusInterface $messageBus,
GeneratorInterface $generator GeneratorInterface $generator,
DateTimeProviderInterface $dateTimeProvider
): void { ): void {
$this->beConstructedWith($userRepository, $messageBus, $generator); $this->beConstructedWith($userRepository, $messageBus, $generator, $dateTimeProvider);
} }
function it_is_a_message_handler(): void function it_is_a_message_handler(): void
@ -44,9 +46,11 @@ final class RequestResetPasswordTokenHandlerSpec extends ObjectBehavior
UserRepositoryInterface $userRepository, UserRepositoryInterface $userRepository,
ShopUserInterface $shopUser, ShopUserInterface $shopUser,
GeneratorInterface $generator, GeneratorInterface $generator,
DateTimeProviderInterface $dateTimeProvider,
MessageBusInterface $messageBus MessageBusInterface $messageBus
): void { ): void {
$userRepository->findOneByEmail('test@email.com')->willReturn($shopUser); $userRepository->findOneByEmail('test@email.com')->willReturn($shopUser);
$dateTimeProvider->now()->willReturn(new \DateTime());
$generator->generate()->willReturn('TOKEN'); $generator->generate()->willReturn('TOKEN');
$shopUser->setPasswordResetToken('TOKEN')->shouldBeCalled(); $shopUser->setPasswordResetToken('TOKEN')->shouldBeCalled();

View file

@ -16,15 +16,18 @@ namespace spec\Sylius\Bundle\ApiBundle\CommandHandler\Account;
use PhpSpec\ObjectBehavior; use PhpSpec\ObjectBehavior;
use Prophecy\Argument; use Prophecy\Argument;
use Sylius\Bundle\ApiBundle\Command\Account\VerifyCustomerAccount; use Sylius\Bundle\ApiBundle\Command\Account\VerifyCustomerAccount;
use Sylius\Calendar\Provider\DateTimeProviderInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface; use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\User\Model\UserInterface; use Sylius\Component\User\Model\UserInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
final class VerifyCustomerAccountHandlerSpec extends ObjectBehavior final class VerifyCustomerAccountHandlerSpec extends ObjectBehavior
{ {
function let(RepositoryInterface $shopUserRepository): void function let(
{ RepositoryInterface $shopUserRepository,
$this->beConstructedWith($shopUserRepository); DateTimeProviderInterface $dateTimeProvider
): void {
$this->beConstructedWith($shopUserRepository, $dateTimeProvider);
} }
function it_is_a_message_handler(): void function it_is_a_message_handler(): void
@ -34,9 +37,11 @@ final class VerifyCustomerAccountHandlerSpec extends ObjectBehavior
function it_verifies_shop_user( function it_verifies_shop_user(
RepositoryInterface $shopUserRepository, RepositoryInterface $shopUserRepository,
DateTimeProviderInterface $dateTimeProvider,
UserInterface $user UserInterface $user
): void { ): void {
$shopUserRepository->findOneBy(['emailVerificationToken' => 'ToKeN'])->willReturn($user); $shopUserRepository->findOneBy(['emailVerificationToken' => 'ToKeN'])->willReturn($user);
$dateTimeProvider->now()->willReturn(new \DateTime());
$user->setVerifiedAt(Argument::type(\DateTime::class))->shouldBeCalled(); $user->setVerifiedAt(Argument::type(\DateTime::class))->shouldBeCalled();
$user->setEmailVerificationToken(null)->shouldBeCalled(); $user->setEmailVerificationToken(null)->shouldBeCalled();