Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
2 changes: 1 addition & 1 deletion etc/travis/suites/common/before_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
57 changes: 57 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Provider/CustomerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\Provider;

use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\User\Canonicalizer\CanonicalizerInterface;

final class CustomerProvider implements CustomerProviderInterface
{
/** @var CanonicalizerInterface */
private $canonicalizer;

/** @var FactoryInterface */
private $customerFactory;

/** @var CustomerRepositoryInterface */
private $customerRepository;

public function __construct(
CanonicalizerInterface $canonicalizer,
FactoryInterface $customerFactory,
CustomerRepositoryInterface $customerRepository
) {
$this->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;
}
}
21 changes: 21 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Provider/CustomerProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\Provider;

use Sylius\Component\Core\Model\CustomerInterface;

interface CustomerProviderInterface
{
public function provide(string $email): CustomerInterface;
}
8 changes: 8 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Resources/config/app/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
1 change: 1 addition & 0 deletions src/Sylius/Bundle/ApiBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<import resource="services/data_providers.xml" />
<import resource="services/event_subscribers.xml"/>
<import resource="services/filters.xml"/>
<import resource="services/providers.xml"/>
</imports>

<services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
>
<services>
<service id="Sylius\Bundle\ApiBundle\CommandHandler\RegisterShopUserHandler">
<argument type="service" id="sylius.canonicalizer" />
<argument type="service" id="sylius.factory.shop_user" />
<argument type="service" id="sylius.factory.customer" />
<argument type="service" id="sylius.repository.customer" />
<argument type="service" id="sylius.manager.shop_user" />
<argument type="service" id="Sylius\Bundle\ApiBundle\Provider\CustomerProviderInterface" />
<tag name="messenger.message_handler" />
</service>
</services>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--

This file is part of the Sylius package.

(c) Paweł Jędrzejewski

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

-->

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"
>
<services>
<service id="Sylius\Bundle\ApiBundle\Provider\CustomerProviderInterface" class="Sylius\Bundle\ApiBundle\Provider\CustomerProvider">
<argument type="service" id="sylius.canonicalizer" />
<argument type="service" id="sylius.factory.customer" />
<argument type="service" id="sylius.repository.customer" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -16,88 +16,43 @@
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
{
$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('[email protected]')->willReturn('[email protected]');
$customerRepository->findOneBy(['emailCanonical' => '[email protected]'])->willReturn(null);

$shopUserFactory->createNew()->willReturn($shopUser);
$customerFactory->createNew()->willReturn($customer);
$customerProvider->provide('[email protected]')->willReturn($customer);

$customer->getUser()->willReturn(null);

$shopUser->setPlainPassword('iamrobot')->shouldBeCalled();

$customer->setFirstName('Will')->shouldBeCalled();
$customer->setLastName('Smith')->shouldBeCalled();
$customer->setEmail('[email protected]')->shouldBeCalled();
$customer->setPhoneNumber('+13104322400')->shouldBeCalled();
$customer->setUser($shopUser)->shouldBeCalled();

$shopUserManager->persist($shopUser)->shouldBeCalled();

$this(new RegisterShopUser('Will', 'Smith', '[email protected]', '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('[email protected]')->willReturn('[email protected]');
$customerRepository->findOneBy(['emailCanonical' => '[email protected]'])->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('[email protected]')->shouldBeCalled();
$customer->setPhoneNumber('+13104322400')->shouldBeCalled();
$customer->setUser($shopUser)->shouldBeCalled();

Expand All @@ -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('[email protected]')->willReturn('[email protected]');
$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', '[email protected]', 'iamrobot', '+13104322400')]);
$this
->shouldThrow(\DomainException::class)
->during('__invoke', [new RegisterShopUser('Will', 'Smith', '[email protected]', 'iamrobot', '+13104322400')])
;
}
}
Loading