Skip to content

Commit

Permalink
[API] AddressOrderHandler refactored to use customer provider instead…
Browse files Browse the repository at this point in the history
… of logic duplication
  • Loading branch information
lchrusciel committed Aug 17, 2021
1 parent 8c1c394 commit 47f9f66
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 144 deletions.
101 changes: 58 additions & 43 deletions UPGRADE-API-1.11.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
product_image_prefix: 'media/image'
```

1. `Sylius\Bundle\ApiBundle\View\CartShippingMethodInterface` and `Sylius\Bundle\ApiBundle\View\CartShippingMethod` have been removed.
2. `Sylius\Bundle\ApiBundle\View\CartShippingMethodInterface` and `Sylius\Bundle\ApiBundle\View\CartShippingMethod` have been removed.

1. `Sylius\Bundle\ApiBundle\View\Factory\CartShippingMethodFactoryInterface` and `Sylius\Bundle\ApiBundle\View\Factory\CartShippingMethodFactory` have been removed.
3. `Sylius\Bundle\ApiBundle\View\Factory\CartShippingMethodFactoryInterface` and `Sylius\Bundle\ApiBundle\View\Factory\CartShippingMethodFactory` have been removed.

1. The constructor of `Sylius\Bundle\ApiBundle\DataProvider\CartShippingMethodsSubresourceDataProvider` has been changed:
4. The constructor of `Sylius\Bundle\ApiBundle\DataProvider\CartShippingMethodsSubresourceDataProvider` has been changed:

```diff
public function __construct(
Expand All @@ -40,7 +40,22 @@
}
```

1. The response schema for endpoint `GET /api/v2/shop/orders/{tokenValue}/shipments/{shipments}/methods` has been changed from:
5. The constructor of `\Sylius\Bundle\ApiBundle\CommandHandler\Checkout\AddressOrderHandler` has been changed:

```diff
public function __construct(
OrderRepositoryInterface $orderRepository,
- CustomerRepositoryInterface $customerRepository,
- FactoryInterface $customerFactory,
ObjectManager $manager,
StateMachineFactoryInterface $stateMachineFactory,
AddressMapperInterface $addressMapper
AddressMapperInterface $addressMapper,
+ CustomerProviderInterface $customerProvider
) {
```

6. The response schema for endpoint `GET /api/v2/shop/orders/{tokenValue}/shipments/{shipments}/methods` has been changed from:

```
...
Expand Down Expand Up @@ -68,7 +83,7 @@
]
```

1. Constructor of `Core/OrderProcessing/OrderTaxesProcessor.php` has been changed where new service implementing
7. Constructor of `Core/OrderProcessing/OrderTaxesProcessor.php` has been changed where new service implementing
`TaxationAddressResolverInterface` will become mandatory from Sylius version 2.0:

```diff
Expand All @@ -82,7 +97,7 @@
}
```

1. Constructor of `ApiBundle/Serializer/ProductVariantNormalizer.php` has been extended with `SectionProviderInterface`
8. Constructor of `ApiBundle/Serializer/ProductVariantNormalizer.php` has been extended with `SectionProviderInterface`
argument:

```diff
Expand All @@ -96,7 +111,7 @@
}
```

1. Request body of `POST` `api/v2/shop/addresses` endpoint has been changed:
9. Request body of `POST` `api/v2/shop/addresses` endpoint has been changed:

```diff
{
Expand All @@ -114,39 +129,39 @@
}
```

1. The service `Sylius\Bundle\ApiBundle\Converter\ItemIriToIdentifierConverter` has changed its name to `Sylius\Bundle\ApiBundle\Converter\IriToIdentifierConverter`
and its service definition from `Sylius\Bundle\ApiBundle\Converter\ItemIriToIdentifierConverter` to `Sylius\Bundle\ApiBundle\Converter\IriToIdentifierConverterInterface`

1. The service `Sylius\Bundle\ApiBundle\Serializer\CommandFieldItemIriToIdentifierDenormalizer` has changed its name to `Sylius\Bundle\ApiBundle\Serializer\CommandArgumentsDenormalizer`
and also its service definition.

1. Following namespaces has been changed:
* '\Sylius\Bundle\ApiBundle\CommandHandler\AddProductReviewHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Catalog\AddProductReviewHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\BlameCartHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Cart\BlameCartHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\RequestResetPasswordTokenHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Account\RequestResetPasswordTokenHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\ResendVerificationEmailHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Account\ResendVerificationEmailHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\ResetPasswordHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Account\ResetPasswordHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\SendAccountRegistrationEmailHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Account\SendAccountRegistrationEmailHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\SendAccountVerificationEmailHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Account\SendAccountVerificationEmailHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\SendOrderConfirmationHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Checkout\SendOrderConfirmationHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\SendResetPasswordEmailHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Account\SendResetPasswordEmailHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\SendShipmentConfirmationEmailHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Checkout\SendShipmentConfirmationEmailHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\VerifyCustomerAccountHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Account\VerifyCustomerAccountHandler'
* '\Sylius\Bundle\ApiBundle\Command\AddProductReview' => '\Sylius\Bundle\ApiBundle\Command\Catalog\AddProductReview'
* '\Sylius\Bundle\ApiBundle\Command\BlameCart' => '\Sylius\Bundle\ApiBundle\Command\Cart\BlameCart'
* '\Sylius\Bundle\ApiBundle\Command\RequestResetPasswordToken' => '\Sylius\Bundle\ApiBundle\Command\Account\RequestResetPasswordToken'
* '\Sylius\Bundle\ApiBundle\Command\ResendVerificationEmail' => '\Sylius\Bundle\ApiBundle\Command\Account\ResendVerificationEmail'
* '\Sylius\Bundle\ApiBundle\Command\ResetPassword' => '\Sylius\Bundle\ApiBundle\Command\Account\ResetPassword'
* '\Sylius\Bundle\ApiBundle\Command\SendAccountRegistrationEmail' => '\Sylius\Bundle\ApiBundle\Command\Account\SendAccountRegistrationEmail'
* '\Sylius\Bundle\ApiBundle\Command\SendAccountVerificationEmail' => '\Sylius\Bundle\ApiBundle\Command\Account\SendAccountVerificationEmail'
* '\Sylius\Bundle\ApiBundle\Command\SendOrderConfirmation' => '\Sylius\Bundle\ApiBundle\Command\Checkout\SendOrderConfirmation'
* '\Sylius\Bundle\ApiBundle\Command\SendResetPasswordEmail' => '\Sylius\Bundle\ApiBundle\Command\Account\SendResetPasswordEmail'
* '\Sylius\Bundle\ApiBundle\Command\SendShipmentConfirmationEmail' => '\Sylius\Bundle\ApiBundle\Command\Checkout\SendShipmentConfirmationEmail'
* '\Sylius\Bundle\ApiBundle\Command\VerifyCustomerAccount' => '\Sylius\Bundle\ApiBundle\Command\Account\VerifyCustomerAccount'
* `\Sylius\Bundle\ApiBundle\CommandHandler\ChangeShopUserPasswordHandler` => `\Sylius\Bundle\ApiBundle\CommandHandler\Account\ChangeShopUserPasswordHandler`
* `\Sylius\Bundle\ApiBundle\CommandHandler\PickupCartHandler` => `\Sylius\Bundle\ApiBundle\CommandHandler\Cart\PickupCartHandler`
* `\Sylius\Bundle\ApiBundle\CommandHandler\RegisterShopUserHandler` => `\Sylius\Bundle\ApiBundle\CommandHandler\Account\RegisterShopUserHandler`
* `\Sylius\Bundle\ApiBundle\Command\ChangeShopUserPassword` => `\Sylius\Bundle\ApiBundle\Command\Account\ChangeShopUserPassword`
* `\Sylius\Bundle\ApiBundle\Command\RegisterShopUser` => `\Sylius\Bundle\ApiBundle\Command\Account\RegisterShopUser`
* `\Sylius\Bundle\ApiBundle\Doctrine\Filters\ExchangeRateFilter` => `\Sylius\Bundle\ApiBundle\Doctrine\Filter\ExchangeRateFilter`
* `\Sylius\Bundle\ApiBundle\Doctrine\Filters\TranslationOrderNameAndLocaleFilter` => `Sylius\Bundle\ApiBundle\Doctrine\Filter\TranslationOrderNameAndLocaleFilter`
10. The service `Sylius\Bundle\ApiBundle\Converter\ItemIriToIdentifierConverter` has changed its name to `Sylius\Bundle\ApiBundle\Converter\IriToIdentifierConverter`
and its service definition from `Sylius\Bundle\ApiBundle\Converter\ItemIriToIdentifierConverter` to `Sylius\Bundle\ApiBundle\Converter\IriToIdentifierConverterInterface`

11. The service `Sylius\Bundle\ApiBundle\Serializer\CommandFieldItemIriToIdentifierDenormalizer` has changed its name to `Sylius\Bundle\ApiBundle\Serializer\CommandArgumentsDenormalizer`
and also its service definition.

12. Following namespaces has been changed:
* '\Sylius\Bundle\ApiBundle\CommandHandler\AddProductReviewHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Catalog\AddProductReviewHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\BlameCartHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Cart\BlameCartHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\RequestResetPasswordTokenHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Account\RequestResetPasswordTokenHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\ResendVerificationEmailHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Account\ResendVerificationEmailHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\ResetPasswordHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Account\ResetPasswordHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\SendAccountRegistrationEmailHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Account\SendAccountRegistrationEmailHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\SendAccountVerificationEmailHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Account\SendAccountVerificationEmailHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\SendOrderConfirmationHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Checkout\SendOrderConfirmationHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\SendResetPasswordEmailHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Account\SendResetPasswordEmailHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\SendShipmentConfirmationEmailHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Checkout\SendShipmentConfirmationEmailHandler'
* '\Sylius\Bundle\ApiBundle\CommandHandler\VerifyCustomerAccountHandler' => '\Sylius\Bundle\ApiBundle\CommandHandler\Account\VerifyCustomerAccountHandler'
* '\Sylius\Bundle\ApiBundle\Command\AddProductReview' => '\Sylius\Bundle\ApiBundle\Command\Catalog\AddProductReview'
* '\Sylius\Bundle\ApiBundle\Command\BlameCart' => '\Sylius\Bundle\ApiBundle\Command\Cart\BlameCart'
* '\Sylius\Bundle\ApiBundle\Command\RequestResetPasswordToken' => '\Sylius\Bundle\ApiBundle\Command\Account\RequestResetPasswordToken'
* '\Sylius\Bundle\ApiBundle\Command\ResendVerificationEmail' => '\Sylius\Bundle\ApiBundle\Command\Account\ResendVerificationEmail'
* '\Sylius\Bundle\ApiBundle\Command\ResetPassword' => '\Sylius\Bundle\ApiBundle\Command\Account\ResetPassword'
* '\Sylius\Bundle\ApiBundle\Command\SendAccountRegistrationEmail' => '\Sylius\Bundle\ApiBundle\Command\Account\SendAccountRegistrationEmail'
* '\Sylius\Bundle\ApiBundle\Command\SendAccountVerificationEmail' => '\Sylius\Bundle\ApiBundle\Command\Account\SendAccountVerificationEmail'
* '\Sylius\Bundle\ApiBundle\Command\SendOrderConfirmation' => '\Sylius\Bundle\ApiBundle\Command\Checkout\SendOrderConfirmation'
* '\Sylius\Bundle\ApiBundle\Command\SendResetPasswordEmail' => '\Sylius\Bundle\ApiBundle\Command\Account\SendResetPasswordEmail'
* '\Sylius\Bundle\ApiBundle\Command\SendShipmentConfirmationEmail' => '\Sylius\Bundle\ApiBundle\Command\Checkout\SendShipmentConfirmationEmail'
* '\Sylius\Bundle\ApiBundle\Command\VerifyCustomerAccount' => '\Sylius\Bundle\ApiBundle\Command\Account\VerifyCustomerAccount'
* `\Sylius\Bundle\ApiBundle\CommandHandler\ChangeShopUserPasswordHandler` => `\Sylius\Bundle\ApiBundle\CommandHandler\Account\ChangeShopUserPasswordHandler`
* `\Sylius\Bundle\ApiBundle\CommandHandler\PickupCartHandler` => `\Sylius\Bundle\ApiBundle\CommandHandler\Cart\PickupCartHandler`
* `\Sylius\Bundle\ApiBundle\CommandHandler\RegisterShopUserHandler` => `\Sylius\Bundle\ApiBundle\CommandHandler\Account\RegisterShopUserHandler`
* `\Sylius\Bundle\ApiBundle\Command\ChangeShopUserPassword` => `\Sylius\Bundle\ApiBundle\Command\Account\ChangeShopUserPassword`
* `\Sylius\Bundle\ApiBundle\Command\RegisterShopUser` => `\Sylius\Bundle\ApiBundle\Command\Account\RegisterShopUser`
* `\Sylius\Bundle\ApiBundle\Doctrine\Filters\ExchangeRateFilter` => `\Sylius\Bundle\ApiBundle\Doctrine\Filter\ExchangeRateFilter`
* `\Sylius\Bundle\ApiBundle\Doctrine\Filters\TranslationOrderNameAndLocaleFilter` => `Sylius\Bundle\ApiBundle\Doctrine\Filter\TranslationOrderNameAndLocaleFilter`
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
use Sylius\Bundle\ApiBundle\Command\Checkout\AddressOrder;
use Sylius\Bundle\ApiBundle\Mapper\AddressMapperInterface;
use Sylius\Bundle\ApiBundle\Provider\CustomerProviderInterface;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Webmozart\Assert\Assert;

Expand All @@ -33,12 +31,6 @@ final class AddressOrderHandler implements MessageHandlerInterface
/** @var OrderRepositoryInterface */
private $orderRepository;

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

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

/** @var ObjectManager */
private $manager;

Expand All @@ -48,20 +40,21 @@ final class AddressOrderHandler implements MessageHandlerInterface
/** @var AddressMapperInterface */
private $addressMapper;

/** @var CustomerProviderInterface */
private $customerProvider;

public function __construct(
OrderRepositoryInterface $orderRepository,
CustomerRepositoryInterface $customerRepository,
FactoryInterface $customerFactory,
ObjectManager $manager,
StateMachineFactoryInterface $stateMachineFactory,
AddressMapperInterface $addressMapper
AddressMapperInterface $addressMapper,
CustomerProviderInterface $customerProvider
) {
$this->orderRepository = $orderRepository;
$this->customerRepository = $customerRepository;
$this->customerFactory = $customerFactory;
$this->manager = $manager;
$this->stateMachineFactory = $stateMachineFactory;
$this->addressMapper = $addressMapper;
$this->customerProvider = $customerProvider;
}

public function __invoke(AddressOrder $addressOrder): OrderInterface
Expand All @@ -80,7 +73,9 @@ public function __invoke(AddressOrder $addressOrder): OrderInterface
);

if (null === $order->getCustomer()) {
$order->setCustomer($this->provideCustomerByEmail($addressOrder->email));
Assert::notNull($addressOrder->email, sprintf('Visitor should provide an email.'));

$order->setCustomer($this->customerProvider->provide($addressOrder->email));
}

/** @var AddressInterface|null $billingAddress */
Expand Down Expand Up @@ -108,19 +103,4 @@ public function __invoke(AddressOrder $addressOrder): OrderInterface

return $order;
}

private function provideCustomerByEmail(?string $email): CustomerInterface
{
Assert::notNull($email, sprintf('Visitor should provide an email.'));

$customer = $this->customerRepository->findOneBy(['email' => $email]);
if (null === $customer) {
/** @var CustomerInterface $customer */
$customer = $this->customerFactory->createNew();
$customer->setEmail($email);
$this->manager->persist($customer);
}

return $customer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@

<service id="Sylius\Bundle\ApiBundle\CommandHandler\Checkout\AddressOrderHandler">
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="sylius.repository.customer" />
<argument type="service" id="sylius.factory.customer" />
<argument type="service" id="sylius.manager.customer" />
<argument type="service" id="sylius.manager.order" />
<argument type="service" id="sm.factory" />
<argument type="service" id="Sylius\Bundle\ApiBundle\Mapper\AddressMapperInterface" />
<argument type="service" id="Sylius\Bundle\ApiBundle\Provider\CustomerProviderInterface" />
<tag name="messenger.message_handler" bus="sylius.command_bus" />
<tag name="messenger.message_handler" bus="sylius_default.bus" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
use Sylius\Bundle\ApiBundle\Command\Checkout\AddressOrder;
use Sylius\Bundle\ApiBundle\CommandHandler\Checkout\AddressOrderHandler;
use Sylius\Bundle\ApiBundle\Mapper\AddressMapperInterface;
use Sylius\Bundle\ApiBundle\Provider\CustomerProviderInterface;
use Sylius\Component\Core\Model\Address;
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

Expand All @@ -40,15 +39,12 @@ public function it_changes_address_order_without_duplication_in_database(): void
/** @var RepositoryInterface $addressRepository */
$addressRepository = $container->get('sylius.repository.address');

/** @var CustomerRepositoryInterface $customerRepository */
$customerRepository = $container->get('sylius.repository.customer');
/** @var CustomerProviderInterface $customerProvider */
$customerProvider = $container->get('Sylius\Bundle\ApiBundle\Provider\CustomerProviderInterface');

/** @var OrderRepositoryInterface $orderRepository */
$orderRepository = $container->get('sylius.repository.order');

/** @var FactoryInterface $customerFactory */
$customerFactory = $container->get('sylius.factory.customer');

/** @var ObjectManager $manager */
$manager = $container->get('doctrine.orm.default_entity_manager');

Expand Down Expand Up @@ -79,11 +75,10 @@ public function it_changes_address_order_without_duplication_in_database(): void

$addressOrderHandler = new AddressOrderHandler(
$orderRepository,
$customerRepository,
$customerFactory,
$manager,
$stateMachineFactory,
$addressMapper
$addressMapper,
$customerProvider
);

$newBillingAddress = $address = new Address();
Expand Down

0 comments on commit 47f9f66

Please sign in to comment.