From 501c2abcb358a295972cb63b9b98add2522f700e Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Thu, 16 Apr 2020 11:16:12 +0200 Subject: [PATCH 1/3] Move messenger config to bundles --- .../Bundle/ApiBundle/Resources/config/app/config.yaml | 8 ++++++++ .../Bundle/CoreBundle/Resources/config/app/config.yml | 1 + .../CoreBundle/Resources/config/app}/messenger.yaml | 6 ++---- 3 files changed, 11 insertions(+), 4 deletions(-) rename {config/packages => src/Sylius/Bundle/CoreBundle/Resources/config/app}/messenger.yaml (79%) diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/app/config.yaml b/src/Sylius/Bundle/ApiBundle/Resources/config/app/config.yaml index 60ce0e7a913..b71ff964498 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/app/config.yaml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/app/config.yaml @@ -15,3 +15,11 @@ api_platform: SM\SMException: 422 Sylius\Bundle\ApiBundle\Exception\CannotRemoveCurrentlyLoggedInUser: 422 Sylius\Bundle\ApiBundle\Exception\ShippingMethodCannotBeRemoved: 422 + +framework: + messenger: + buses: + sylius_default.bus: + middleware: + - 'validation' + - 'doctrine_transaction' diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/app/config.yml b/src/Sylius/Bundle/CoreBundle/Resources/config/app/config.yml index 14fc12dcc5a..6a4d6587a2d 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/app/config.yml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/app/config.yml @@ -22,6 +22,7 @@ imports: - { resource: "@SyliusCoreBundle/Resources/config/app/sylius.yml" } - { resource: "@SyliusCoreBundle/Resources/config/app/state_machine.yml" } - { resource: "@SyliusCoreBundle/Resources/config/app/fixtures.yml" } + - { resource: "@SyliusCoreBundle/Resources/config/app/messenger.yaml" } parameters: sylius_core.public_dir: "%kernel.project_dir%/web" diff --git a/config/packages/messenger.yaml b/src/Sylius/Bundle/CoreBundle/Resources/config/app/messenger.yaml similarity index 79% rename from config/packages/messenger.yaml rename to src/Sylius/Bundle/CoreBundle/Resources/config/app/messenger.yaml index 5b7323d9e6e..28ac457dc90 100644 --- a/config/packages/messenger.yaml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/app/messenger.yaml @@ -13,8 +13,6 @@ framework: # Route your messages to the transports # 'App\Message\YourMessage': async + default_bus: sylius_default.bus buses: - messenger.bus.default: - middleware: - - 'validation' - - 'doctrine_transaction' + sylius_default.bus: ~ From e5f9696f45b648e0d5ef44731ebbb7af986ff345 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Thu, 16 Apr 2020 11:54:31 +0200 Subject: [PATCH 2/3] Extract providing customer to separate service --- .../RegisterShopUserHandler.php | 58 ++++---------- .../ApiBundle/Provider/CustomerProvider.php | 57 ++++++++++++++ .../Provider/CustomerProviderInterface.php | 21 ++++++ .../ApiBundle/Resources/config/services.xml | 1 + .../config/services/command_handlers.xml | 4 +- .../Resources/config/services/providers.xml | 25 +++++++ .../RegisterShopUserHandlerSpec.php | 75 ++++--------------- .../spec/Provider/CustomerProviderSpec.php | 66 ++++++++++++++++ 8 files changed, 200 insertions(+), 107 deletions(-) create mode 100644 src/Sylius/Bundle/ApiBundle/Provider/CustomerProvider.php create mode 100644 src/Sylius/Bundle/ApiBundle/Provider/CustomerProviderInterface.php create mode 100644 src/Sylius/Bundle/ApiBundle/Resources/config/services/providers.xml create mode 100644 src/Sylius/Bundle/ApiBundle/spec/Provider/CustomerProviderSpec.php diff --git a/src/Sylius/Bundle/ApiBundle/CommandHandler/RegisterShopUserHandler.php b/src/Sylius/Bundle/ApiBundle/CommandHandler/RegisterShopUserHandler.php index 91a2c360009..03e577e89e1 100644 --- a/src/Sylius/Bundle/ApiBundle/CommandHandler/RegisterShopUserHandler.php +++ b/src/Sylius/Bundle/ApiBundle/CommandHandler/RegisterShopUserHandler.php @@ -15,42 +15,30 @@ use Doctrine\Persistence\ObjectManager; use Sylius\Bundle\ApiBundle\Command\RegisterShopUser; -use Sylius\Component\Core\Model\CustomerInterface; +use Sylius\Bundle\ApiBundle\Provider\CustomerProviderInterface; use Sylius\Component\Core\Model\ShopUserInterface; -use Sylius\Component\Core\Repository\CustomerRepositoryInterface; use Sylius\Component\Resource\Factory\FactoryInterface; -use Sylius\Component\User\Canonicalizer\CanonicalizerInterface; use Symfony\Component\Messenger\Handler\MessageHandlerInterface; -class RegisterShopUserHandler implements MessageHandlerInterface +final class RegisterShopUserHandler implements MessageHandlerInterface { - /** @var CanonicalizerInterface */ - protected $canonicalizer; - - /** @var FactoryInterface */ - protected $shopUserFactory; - /** @var FactoryInterface */ - protected $customerFactory; - - /** @var CustomerRepositoryInterface */ - protected $customerRepository; + private $shopUserFactory; /** @var ObjectManager */ - protected $shopUserManager; + private $shopUserManager; + + /** @var CustomerProviderInterface */ + private $customerProvider; public function __construct( - CanonicalizerInterface $canonicalizer, FactoryInterface $shopUserFactory, - FactoryInterface $customerFactory, - CustomerRepositoryInterface $customerRepository, - ObjectManager $shopUserManager + ObjectManager $shopUserManager, + CustomerProviderInterface $customerProvider ) { - $this->canonicalizer = $canonicalizer; $this->shopUserFactory = $shopUserFactory; - $this->customerFactory = $customerFactory; - $this->customerRepository = $customerRepository; $this->shopUserManager = $shopUserManager; + $this->customerProvider = $customerProvider; } public function __invoke(RegisterShopUser $command): void @@ -59,32 +47,16 @@ public function __invoke(RegisterShopUser $command): void $user = $this->shopUserFactory->createNew(); $user->setPlainPassword($command->password); - $customer = $this->provideCustomer($command->email); + $customer = $this->customerProvider->provide($command->email); + if ($customer->getUser() !== null) { + throw new \DomainException(sprintf('User with email "%s" is already registered.', $command->email)); + } + $customer->setFirstName($command->firstName); $customer->setLastName($command->lastName); - $customer->setEmail($command->email); $customer->setPhoneNumber($command->phoneNumber); $customer->setUser($user); $this->shopUserManager->persist($user); } - - protected function provideCustomer(string $email): CustomerInterface - { - $emailCanonical = $this->canonicalizer->canonicalize($email); - - /** @var CustomerInterface|null $customer */ - $customer = $this->customerRepository->findOneBy(['emailCanonical' => $emailCanonical]); - - if ($customer === null) { - /** @var CustomerInterface $customer */ - $customer = $this->customerFactory->createNew(); - } - - if ($customer->getUser() !== null) { - throw new \DomainException(sprintf('User with email "%s" is already registered.', $emailCanonical)); - } - - return $customer; - } } diff --git a/src/Sylius/Bundle/ApiBundle/Provider/CustomerProvider.php b/src/Sylius/Bundle/ApiBundle/Provider/CustomerProvider.php new file mode 100644 index 00000000000..e890e3d2925 --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/Provider/CustomerProvider.php @@ -0,0 +1,57 @@ +canonicalizer = $canonicalizer; + $this->customerFactory = $customerFactory; + $this->customerRepository = $customerRepository; + } + + public function provide(string $email): CustomerInterface + { + $emailCanonical = $this->canonicalizer->canonicalize($email); + + /** @var CustomerInterface|null $customer */ + $customer = $this->customerRepository->findOneBy(['emailCanonical' => $emailCanonical]); + + if ($customer === null) { + /** @var CustomerInterface $customer */ + $customer = $this->customerFactory->createNew(); + $customer->setEmail($email); + } + + return $customer; + } +} diff --git a/src/Sylius/Bundle/ApiBundle/Provider/CustomerProviderInterface.php b/src/Sylius/Bundle/ApiBundle/Provider/CustomerProviderInterface.php new file mode 100644 index 00000000000..8ec6faa3d69 --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/Provider/CustomerProviderInterface.php @@ -0,0 +1,21 @@ + + diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/services/command_handlers.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/services/command_handlers.xml index d03d8adaac9..7df18ffc729 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/services/command_handlers.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/services/command_handlers.xml @@ -17,11 +17,9 @@ > - - - + diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/services/providers.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/services/providers.xml new file mode 100644 index 00000000000..8e186fa66c7 --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/services/providers.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + diff --git a/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/RegisterShopUserHandlerSpec.php b/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/RegisterShopUserHandlerSpec.php index 1b7765d1050..31de363798e 100644 --- a/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/RegisterShopUserHandlerSpec.php +++ b/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/RegisterShopUserHandlerSpec.php @@ -16,29 +16,20 @@ use Doctrine\Persistence\ObjectManager; use PhpSpec\ObjectBehavior; use Sylius\Bundle\ApiBundle\Command\RegisterShopUser; +use Sylius\Bundle\ApiBundle\Provider\CustomerProviderInterface; use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Component\Core\Model\ShopUserInterface; -use Sylius\Component\Core\Repository\CustomerRepositoryInterface; use Sylius\Component\Resource\Factory\FactoryInterface; -use Sylius\Component\User\Canonicalizer\CanonicalizerInterface; use Symfony\Component\Messenger\Handler\MessageHandlerInterface; final class RegisterShopUserHandlerSpec extends ObjectBehavior { function let( - CanonicalizerInterface $canonicalizer, FactoryInterface $shopUserFactory, - FactoryInterface $customerFactory, - CustomerRepositoryInterface $customerRepository, - ObjectManager $shopUserManager + ObjectManager $shopUserManager, + CustomerProviderInterface $customerProvider ): void { - $this->beConstructedWith( - $canonicalizer, - $shopUserFactory, - $customerFactory, - $customerRepository, - $shopUserManager - ); + $this->beConstructedWith($shopUserFactory, $shopUserManager, $customerProvider); } function it_is_a_message_handler(): void @@ -46,20 +37,15 @@ function it_is_a_message_handler(): void $this->shouldImplement(MessageHandlerInterface::class); } - function it_creates_a_customer_and_user_with_given_data( - CanonicalizerInterface $canonicalizer, + function it_creates_a_shop_user_with_given_data( FactoryInterface $shopUserFactory, - FactoryInterface $customerFactory, - CustomerRepositoryInterface $customerRepository, ObjectManager $shopUserManager, + CustomerProviderInterface $customerProvider, ShopUserInterface $shopUser, CustomerInterface $customer ): void { - $canonicalizer->canonicalize('WILL.SMITH@example.com')->willReturn('will.smith@example.com'); - $customerRepository->findOneBy(['emailCanonical' => 'will.smith@example.com'])->willReturn(null); - $shopUserFactory->createNew()->willReturn($shopUser); - $customerFactory->createNew()->willReturn($customer); + $customerProvider->provide('WILL.SMITH@example.com')->willReturn($customer); $customer->getUser()->willReturn(null); @@ -67,37 +53,6 @@ function it_creates_a_customer_and_user_with_given_data( $customer->setFirstName('Will')->shouldBeCalled(); $customer->setLastName('Smith')->shouldBeCalled(); - $customer->setEmail('WILL.SMITH@example.com')->shouldBeCalled(); - $customer->setPhoneNumber('+13104322400')->shouldBeCalled(); - $customer->setUser($shopUser)->shouldBeCalled(); - - $shopUserManager->persist($shopUser)->shouldBeCalled(); - - $this(new RegisterShopUser('Will', 'Smith', 'WILL.SMITH@example.com', 'iamrobot', '+13104322400')); - } - - function it_creates_only_a_user_if_customer_without_user_already_exists( - CanonicalizerInterface $canonicalizer, - FactoryInterface $shopUserFactory, - FactoryInterface $customerFactory, - CustomerRepositoryInterface $customerRepository, - ObjectManager $shopUserManager, - ShopUserInterface $shopUser, - CustomerInterface $customer - ): void { - $canonicalizer->canonicalize('WILL.SMITH@example.com')->willReturn('will.smith@example.com'); - $customerRepository->findOneBy(['emailCanonical' => 'will.smith@example.com'])->willReturn($customer); - - $shopUserFactory->createNew()->willReturn($shopUser); - $customerFactory->createNew()->shouldNotBeCalled(); - - $customer->getUser()->willReturn(null); - - $shopUser->setPlainPassword('iamrobot')->shouldBeCalled(); - - $customer->setFirstName('Will')->shouldBeCalled(); - $customer->setLastName('Smith')->shouldBeCalled(); - $customer->setEmail('WILL.SMITH@example.com')->shouldBeCalled(); $customer->setPhoneNumber('+13104322400')->shouldBeCalled(); $customer->setUser($shopUser)->shouldBeCalled(); @@ -107,25 +62,23 @@ function it_creates_only_a_user_if_customer_without_user_already_exists( } function it_throws_an_exception_if_customer_with_user_already_exists( - CanonicalizerInterface $canonicalizer, FactoryInterface $shopUserFactory, - FactoryInterface $customerFactory, - CustomerRepositoryInterface $customerRepository, ObjectManager $shopUserManager, + CustomerProviderInterface $customerProvider, ShopUserInterface $shopUser, CustomerInterface $customer, ShopUserInterface $existingShopUser ): void { - $canonicalizer->canonicalize('WILL.SMITH@example.com')->willReturn('will.smith@example.com'); - $customerRepository->findOneBy(['emailCanonical' => 'will.smith@example.com'])->willReturn($customer); + $shopUserFactory->createNew()->willReturn($shopUser); + $customerProvider->provide('WILL.SMITH@example.com')->willReturn($customer); $customer->getUser()->willReturn($existingShopUser); - $shopUserFactory->createNew()->willReturn($shopUser); - $customerFactory->createNew()->shouldNotBeCalled(); - $shopUserManager->persist($shopUser)->shouldNotBeCalled(); - $this->shouldThrow(\DomainException::class)->during('__invoke', [new RegisterShopUser('Will', 'Smith', 'WILL.SMITH@example.com', 'iamrobot', '+13104322400')]); + $this + ->shouldThrow(\DomainException::class) + ->during('__invoke', [new RegisterShopUser('Will', 'Smith', 'WILL.SMITH@example.com', 'iamrobot', '+13104322400')]) + ; } } diff --git a/src/Sylius/Bundle/ApiBundle/spec/Provider/CustomerProviderSpec.php b/src/Sylius/Bundle/ApiBundle/spec/Provider/CustomerProviderSpec.php new file mode 100644 index 00000000000..ac61f87cdbf --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/spec/Provider/CustomerProviderSpec.php @@ -0,0 +1,66 @@ +beConstructedWith($canonicalizer, $customerFactory, $customerRepository); + } + + function it_is_a_customer_provider(): void + { + $this->shouldImplement(CustomerProviderInterface::class); + } + + function it_creates_a_customer_if_there_is_no_existing_one_with_given_email( + CanonicalizerInterface $canonicalizer, + FactoryInterface $customerFactory, + CustomerRepositoryInterface $customerRepository, + CustomerInterface $customer + ): void { + $canonicalizer->canonicalize('WILL.SMITH@example.com')->willReturn('will.smith@example.com'); + $customerRepository->findOneBy(['emailCanonical' => 'will.smith@example.com'])->willReturn(null); + + $customerFactory->createNew()->willReturn($customer); + $customer->setEmail('WILL.SMITH@example.com')->shouldBeCalled(); + + $this->provide('WILL.SMITH@example.com')->shouldReturn($customer); + } + + function it_doesn_not_create_a_customer_if_customer_with_given_email_already_exists( + CanonicalizerInterface $canonicalizer, + FactoryInterface $customerFactory, + CustomerRepositoryInterface $customerRepository, + CustomerInterface $customer + ): void { + $canonicalizer->canonicalize('WILL.SMITH@example.com')->willReturn('will.smith@example.com'); + $customerRepository->findOneBy(['emailCanonical' => 'will.smith@example.com'])->willReturn($customer); + + $customerFactory->createNew()->shouldNotBeCalled(); + + $this->provide('WILL.SMITH@example.com')->shouldReturn($customer); + } +} From a6e2f5377a23a72920c1c4c87cd5d63be58db2b5 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Thu, 16 Apr 2020 11:54:49 +0200 Subject: [PATCH 3/3] [Travis] Fix comment --- etc/travis/suites/common/before_install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/travis/suites/common/before_install.sh b/etc/travis/suites/common/before_install.sh index 6aa18e6724b..15995df5b15 100755 --- a/etc/travis/suites/common/before_install.sh +++ b/etc/travis/suites/common/before_install.sh @@ -5,7 +5,7 @@ source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../../../bash/common.lib.s print_header "Customizing the environment" "Sylius" run_command "git fetch origin $TRAVIS_BRANCH:refs/remotes/origin/$TRAVIS_BRANCH" || exit $? # Make origin/master available for is_suitable steps run_command "phpenv config-rm xdebug.ini" # Disable XDebug -run_command "echo \"memory_limit=6144M\" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini" || exit $? # Increase memory limit to 4GB +run_command "echo \"memory_limit=6144M\" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini" || exit $? # Increase memory limit to 6GB run_command "composer self-update --stable" run_command "mkdir -p \"${SYLIUS_CACHE_DIR}\"" || exit $? # Create Sylius cache directory run_command "mkdir -p web/media/image" || exit $? # Create Sylius images directory