create customer provider in coreBundle

This commit is contained in:
Adam Kasperczak 2022-09-13 08:46:18 +02:00 committed by Grzegorz Sadowski
parent ca6ff4dba9
commit ecb3a3c9b5
No known key found for this signature in database
GPG key ID: EF87FF4E6E3BD364
7 changed files with 140 additions and 31 deletions

View file

@ -1,8 +1,8 @@
# UPGRADE FROM `v1.11.X` TO `v1.12.0`
1. The `Sylius\Bundle\ApiBundle\DataProvider\CartShippingMethodsSubresourceDataProvider` has been removed and replaced by `Sylius\Bundle\ApiBundle\DataProvider\ShippingMethodsCollectionDataProvider`.
1. The `Sylius\Bundle\ApiBundle\Provider\CustomerProviderInterface` has been removed and replaced by `Sylius\Bundle\CoreBundle\Resolver\CustomerResolverInterface`.
src/Sylius/Bundle/CoreBundle/spec/Provider/CustomerProviderSpec.php
1. The `Sylius\Bundle\ApiBundle\Provider\CustomerProviderInterface` has been refactored and moved to `CoreBundle` use `Sylius\Bundle\CoreBundle\Resolver\CustomerResolverInterface` instead.
1. The `Sylius\Bundle\ApiBundle\Serializer\ShippingMethodNormalizer` logic and constructor has been changed due to refactor above.

View file

@ -0,0 +1,42 @@
<?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\CoreBundle\Provider;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
use Sylius\Component\User\Canonicalizer\CanonicalizerInterface;
final class CustomerProvider implements CustomerProviderInterface
{
public function __construct(
private CustomerRepositoryInterface $customerRepository,
private CanonicalizerInterface $canonicalizer
) {
}
public function provide(string $email): CustomerInterface
{
$emailCanonical = $this->canonicalizer->canonicalize($email);
/** @var CustomerInterface|null $customer */
$customer = $this->customerRepository->findOneBy(['emailCanonical' => $emailCanonical]);
if ($customer === null) {
throw new \RuntimeException(sprintf('Customer with email "%s" does not exist.', $email));
}
return $customer;
}
}

View file

@ -0,0 +1,22 @@
<?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\CoreBundle\Provider;
use Sylius\Component\Core\Model\CustomerInterface;
interface CustomerProviderInterface
{
/** @throws \RuntimeException */
public function provide(string $email): CustomerInterface;
}

View file

@ -13,33 +13,28 @@ declare(strict_types=1);
namespace Sylius\Bundle\CoreBundle\Resolver;
use Sylius\Bundle\CoreBundle\Provider\CustomerProviderInterface;
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 CustomerResolver implements CustomerResolverInterface
{
public function __construct(
private CanonicalizerInterface $canonicalizer,
private FactoryInterface $customerFactory,
private CustomerRepositoryInterface $customerRepository,
private CustomerProviderInterface $customerProvider,
) {
}
public function resolve(string $email): CustomerInterface
{
$emailCanonical = $this->canonicalizer->canonicalize($email);
/** @var CustomerInterface|null $customer */
$customer = $this->customerRepository->findOneBy(['emailCanonical' => $emailCanonical]);
if ($customer === null) {
try {
return $this->customerProvider->provide($email);
} catch (\RuntimeException) {
/** @var CustomerInterface $customer */
$customer = $this->customerFactory->createNew();
$customer->setEmail($email);
}
return $customer;
return $customer;
}
}
}

View file

@ -50,6 +50,11 @@
<service id="sylius.provider.channel_based_default_zone_provider" class="Sylius\Bundle\CoreBundle\Provider\ChannelBasedDefaultTaxZoneProvider" />
<service id="Sylius\Bundle\CoreBundle\Provider\CustomerProviderInterface" class="Sylius\Bundle\CoreBundle\Provider\CustomerProvider" >
<argument type="service" id="sylius.repository.customer" />
<argument type="service" id="sylius.canonicalizer" />
</service>
<service id="sylius.integer_distributor" class="Sylius\Component\Core\Distributor\IntegerDistributor" />
<service id="Sylius\Component\Core\Distributor\IntegerDistributorInterface" alias="sylius.integer_distributor" />
@ -296,9 +301,8 @@
</service>
<service id="Sylius\Bundle\CoreBundle\Resolver\CustomerResolverInterface" class="Sylius\Bundle\CoreBundle\Resolver\CustomerResolver">
<argument type="service" id="sylius.canonicalizer" />
<argument type="service" id="sylius.factory.customer" />
<argument type="service" id="sylius.repository.customer" />
<argument type="service" id="Sylius\Bundle\CoreBundle\Provider\CustomerProviderInterface" />
</service>
</services>
</container>

View file

@ -0,0 +1,54 @@
<?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 spec\Sylius\Bundle\CoreBundle\Provider;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\CoreBundle\Provider\CustomerProvider;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
use Sylius\Component\User\Canonicalizer\CanonicalizerInterface;
final class CustomerProviderSpec extends ObjectBehavior
{
function let(CustomerRepositoryInterface $customerRepository, CanonicalizerInterface $canonicalizer): void
{
$this->beConstructedWith($customerRepository, $canonicalizer);
}
function it_implements_customer_interface(): void
{
$this->shouldImplement(CustomerProvider::class);
}
function it_provides_customer(
CustomerRepositoryInterface $customerRepository,
CanonicalizerInterface $canonicalizer,
CustomerInterface $customer,
): void {
$canonicalizer->canonicalize('Adam@syLius.com')->willReturn('adam@sylius.com');
$customerRepository->findOneBy(['emailCanonical' => 'adam@sylius.com'])->willReturn($customer);
$this->provide('Adam@syLius.com')->shouldReturn($customer);
}
function it_throws_exception_if_customer_is_not_found(
CanonicalizerInterface $canonicalizer,
CustomerRepositoryInterface $customerRepository,
): void {
$canonicalizer->canonicalize('Adam@syLius.com')->willReturn('adam@sylius.com');
$customerRepository->findOneBy(['emailCanonical' => 'adam@sylius.com'])->willReturn(null);
$this->shouldThrow(\RuntimeException::class)->during('provide', ['Adam@syLius.com']);
}
}

View file

@ -14,20 +14,18 @@ declare(strict_types=1);
namespace spec\Sylius\Bundle\CoreBundle\Resolver;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\CoreBundle\Provider\CustomerProviderInterface;
use Sylius\Bundle\CoreBundle\Resolver\CustomerResolverInterface;
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 CustomerResolverSpec extends ObjectBehavior
{
function let(
CanonicalizerInterface $canonicalizer,
FactoryInterface $customerFactory,
CustomerRepositoryInterface $customerRepository,
CustomerProviderInterface $customerProvider,
): void {
$this->beConstructedWith($canonicalizer, $customerFactory, $customerRepository);
$this->beConstructedWith($customerFactory, $customerProvider);
}
function it_is_a_customer_provider(): void
@ -36,14 +34,11 @@ final class CustomerResolverSpec extends ObjectBehavior
}
function it_creates_a_customer_if_there_is_no_existing_one_with_given_email(
CanonicalizerInterface $canonicalizer,
FactoryInterface $customerFactory,
CustomerRepositoryInterface $customerRepository,
CustomerProviderInterface $customerProvider,
CustomerInterface $customer,
): void {
$canonicalizer->canonicalize('WILL.SMITH@example.com')->willReturn('will.smith@example.com');
$customerRepository->findOneBy(['emailCanonical' => 'will.smith@example.com'])->willReturn(null);
$customerProvider->provide('WILL.SMITH@example.com')->willThrow(\RuntimeException::class);
$customerFactory->createNew()->willReturn($customer);
$customer->setEmail('WILL.SMITH@example.com')->shouldBeCalled();
@ -51,14 +46,11 @@ final class CustomerResolverSpec extends ObjectBehavior
}
function it_does_not_create_a_customer_if_customer_with_given_email_already_exists(
CanonicalizerInterface $canonicalizer,
FactoryInterface $customerFactory,
CustomerRepositoryInterface $customerRepository,
CustomerProviderInterface $customerProvider,
CustomerInterface $customer,
): void {
$canonicalizer->canonicalize('WILL.SMITH@example.com')->willReturn('will.smith@example.com');
$customerRepository->findOneBy(['emailCanonical' => 'will.smith@example.com'])->willReturn($customer);
$customerProvider->provide('WILL.SMITH@example.com')->willReturn($customer);
$customerFactory->createNew()->shouldNotBeCalled();
$this->resolve('WILL.SMITH@example.com')->shouldReturn($customer);