[Customer] Extract authorized user assignment to separate method

This commit is contained in:
Łukasz Chruściel 2022-05-13 11:18:20 +02:00
parent cb854760c3
commit 9db5eb52d4
No known key found for this signature in database
GPG key ID: 049A1D51AA3B039C
14 changed files with 72 additions and 68 deletions

View file

@ -3,7 +3,11 @@
## Main update
1. Service `sylius.twig.extension.taxes` has been deprecated. Use methods `getTaxExcludedTotal` and `getTaxIncludedTotal`
from `Sylius\Component\Core\Model\Order` instead.
from `Sylius\Component\Core\Model\Order` instead.
2. Both `getCreatedByGuest` and `setCreatedByGuest` methods were deprecated on `\Sylius\Component\Core\Model\Order`.
Please use `isCreatedByGuest` instead of the first one. The latter is a part of the `setCustomerWithAuthorization` logic
and should be used only this way.
### Asset management changes

View file

@ -50,8 +50,7 @@ final class BlameCartHandler implements MessageHandlerInterface
throw new \InvalidArgumentException('There is an assigned customer to this cart');
}
$cart->setCustomer($user->getCustomer());
$cart->setCreatedByGuest(false);
$cart->setCustomerWithAuthorization($user->getCustomer());
$this->orderProcessor->process($cart);
}

View file

@ -73,9 +73,8 @@ final class PickupCartHandler implements MessageHandlerInterface
$cart->setTokenValue($pickupCart->tokenValue ?? $this->generator->generateUriSafeString(10));
if ($customer !== null) {
$cart->setCustomer($customer);
$cart->setCustomerWithAuthorization($customer);
$cart->setBillingAddress($this->getDefaultAddress($customer));
$cart->setCreatedByGuest(false);
}
$this->orderManager->persist($cart);

View file

@ -45,7 +45,7 @@ final class ApiCartBlamerListener
}
$cart = $this->getCart();
if (null === $cart || !$cart->getCreatedByGuest()) {
if (null === $cart || !$cart->isCreatedByGuest()) {
return;
}

View file

@ -57,8 +57,7 @@ final class BlameCartHandlerSpec extends ObjectBehavior
$user->getCustomer()->willReturn($customer);
$cart->setCustomer($customer)->shouldBeCalled();
$cart->setCreatedByGuest(false)->shouldBeCalled();
$cart->setCustomerWithAuthorization($customer)->shouldBeCalled();
$orderProcessor->process($cart)->shouldBeCalled();

View file

@ -90,8 +90,7 @@ final class PickupCartHandlerSpec extends ObjectBehavior
$channel->getLocales()->willReturn(new ArrayCollection([$locale->getWrappedObject()]));
$cartFactory->createNew()->willReturn($cart);
$cart->setCustomer($customer)->shouldBeCalled();
$cart->setCreatedByGuest(false)->shouldBeCalled();
$cart->setCustomerWithAuthorization($customer)->shouldBeCalled();
$cart->setBillingAddress($address)->shouldBeCalled();
$cart->setChannel($channel)->shouldBeCalled();
$cart->setCurrencyCode('USD')->shouldBeCalled();
@ -136,8 +135,7 @@ final class PickupCartHandlerSpec extends ObjectBehavior
$channel->getLocales()->willReturn(new ArrayCollection([$locale->getWrappedObject()]));
$cartFactory->createNew()->willReturn($cart);
$cart->setCustomer($customer)->shouldBeCalled();
$cart->setCreatedByGuest(false)->shouldBeCalled();
$cart->setCustomerWithAuthorization($customer)->shouldBeCalled();
$cart->setBillingAddress(null)->shouldBeCalled();
$cart->setChannel($channel)->shouldBeCalled();
$cart->setCurrencyCode('USD')->shouldBeCalled();

View file

@ -75,7 +75,7 @@ final class ApiCartBlamerListenerSpec extends ObjectBehavior
): void {
$sectionResolver->getSection()->willReturn($shopApiOrdersSubSectionSection);
$cartContext->getCart()->willReturn($cart);
$cart->getCreatedByGuest()->willReturn(true);
$cart->isCreatedByGuest()->willReturn(true);
$token->getUser()->willReturn($user);
$user->getCustomer()->willReturn($customer);
@ -104,7 +104,7 @@ final class ApiCartBlamerListenerSpec extends ObjectBehavior
): void {
$sectionResolver->getSection()->willReturn($shopApiOrdersSubSectionSection);
$cartContext->getCart()->willReturn($cart);
$cart->getCreatedByGuest()->willReturn(false);
$cart->isCreatedByGuest()->willReturn(false);
$cart->setCustomer(Argument::any())->shouldNotBeCalled();

View file

@ -65,8 +65,7 @@ final class ShopCartBlamerListener
return;
}
$cart->setCustomer($user->getCustomer());
$cart->setCreatedByGuest(false);
$cart->setCustomerWithAuthorization($user->getCustomer());
}
/**

View file

@ -88,8 +88,7 @@ final class ShopCartBlamerListenerSpec extends ObjectBehavior
$userEvent->getUser()->willReturn($user);
$user->getCustomer()->willReturn($customer);
$cart->setCustomer($customer)->shouldBeCalled();
$cart->setCreatedByGuest(false)->shouldBeCalled();
$cart->setCustomerWithAuthorization($customer)->shouldBeCalled();
$this->onImplicitLogin($userEvent);
}
@ -110,8 +109,7 @@ final class ShopCartBlamerListenerSpec extends ObjectBehavior
$token->getUser()->willReturn($user);
$user->getCustomer()->willReturn($customer);
$cart->setCustomer($customer)->shouldBeCalled();
$cart->setCreatedByGuest(false)->shouldBeCalled();
$cart->setCustomerWithAuthorization($customer)->shouldBeCalled();
$this->onInteractiveLogin(new InteractiveLoginEvent($request->getWrappedObject(), $token->getWrappedObject()));
}
@ -129,8 +127,7 @@ final class ShopCartBlamerListenerSpec extends ObjectBehavior
$cartContext->getCart()->willReturn($cart);
$cart->getCustomer()->willReturn($customer);
$cart->setCustomer(Argument::any())->shouldNotBeCalled();
$cart->setCreatedByGuest(false)->shouldNotBeCalled();
$cart->setCustomerWithAuthorization(Argument::any())->shouldNotBeCalled();
$this->onInteractiveLogin(new InteractiveLoginEvent($request->getWrappedObject(), $token->getWrappedObject()));
}
@ -147,8 +144,7 @@ final class ShopCartBlamerListenerSpec extends ObjectBehavior
$cartContext->getCart()->willReturn($cart);
$token->getUser()->willReturn('anon.');
$cart->setCustomer(Argument::any())->shouldNotBeCalled();
$cart->setCreatedByGuest(false)->shouldNotBeCalled();
$cart->setCustomerWithAuthorization(Argument::any())->shouldNotBeCalled();
$this->onInteractiveLogin(new InteractiveLoginEvent($request->getWrappedObject(), $token->getWrappedObject()));
}

View file

@ -90,11 +90,7 @@ final class ShopBasedCartContext implements CartContextInterface
private function setCustomerAndAddressOnCart(OrderInterface $cart, CustomerInterface $customer): void
{
$cart->setCustomer($customer);
if ($this->createdByGuestFlagResolver !== null) {
$cart->setCreatedByGuest($this->createdByGuestFlagResolver->resolveFlag());
}
$this->setCustomer($cart, $customer);
$defaultAddress = $customer->getDefaultAddress();
if (null !== $defaultAddress) {
@ -103,4 +99,15 @@ final class ShopBasedCartContext implements CartContextInterface
$cart->setBillingAddress($clonedAddress);
}
}
private function setCustomer(OrderInterface $cart, CustomerInterface $customer): void
{
if ($this->createdByGuestFlagResolver !== null && !$this->createdByGuestFlagResolver->resolveFlag()) {
$cart->setCustomerWithAuthorization($customer);
return;
}
$cart->setCustomer($customer);
}
}

View file

@ -115,6 +115,12 @@ class Order extends BaseOrder implements OrderInterface
$this->customer = $customer;
}
public function setCustomerWithAuthorization(?BaseCustomerInterface $customer): void
{
$this->setCustomer($customer);
$this->createdByGuest = false;
}
public function getChannel(): ?BaseChannelInterface
{
return $this->channel;
@ -472,13 +478,22 @@ class Order extends BaseOrder implements OrderInterface
return $total;
}
public function getCreatedByGuest(): bool
public function isCreatedByGuest(): bool
{
return $this->createdByGuest;
}
public function getCreatedByGuest(): bool
{
@trigger_error('This method is deprecated since Sylius 1.12 and it will be removed in Sylius 2.0. Please use `isCreatedByGuest` instead.');
return $this->isCreatedByGuest();
}
public function setCreatedByGuest(bool $createdByGuest): void
{
@trigger_error('This method is deprecated since Sylius 1.12 and it will be removed in Sylius 2.0. This flag should be changed only through `setCustomerWithAuthorization` method.');
$this->createdByGuest = $createdByGuest;
}
}

View file

@ -17,6 +17,7 @@ use Doctrine\Common\Collections\Collection;
use Sylius\Component\Channel\Model\ChannelAwareInterface;
use Sylius\Component\Channel\Model\ChannelInterface as BaseChannelInterface;
use Sylius\Component\Customer\Model\CustomerAwareInterface;
use Sylius\Component\Customer\Model\CustomerInterface as BaseCustomerInterface;
use Sylius\Component\Order\Model\OrderInterface as BaseOrderInterface;
use Sylius\Component\Payment\Model\PaymentsSubjectInterface;
use Sylius\Component\Promotion\Model\CountablePromotionSubjectInterface;
@ -117,6 +118,10 @@ interface OrderInterface extends
public function setCustomerIp(?string $customerIp): void;
public function setCustomerWithAuthorization(?BaseCustomerInterface $customer): void;
public function isCreatedByGuest(): bool;
public function getCreatedByGuest(): bool;
public function setCreatedByGuest(bool $createdByGuest): void;

View file

@ -189,7 +189,7 @@ final class ShopBasedCartContextSpec extends ObjectBehavior
$this->getCart()->shouldReturn($secondCart);
}
function it_sets_created_by_guest_flag_as_false_if_order_is_created_by_logged_in_customer(
function it_creates_order_for_authorized_user(
CartContextInterface $cartContext,
ShopperContextInterface $shopperContext,
CreatedByGuestFlagResolverInterface $createdByGuestFlagResolver,
@ -202,8 +202,6 @@ final class ShopBasedCartContextSpec extends ObjectBehavior
$createdByGuestFlagResolver->resolveFlag()->willReturn(false);
$cart->setCreatedByGuest(false)->shouldBeCalled();
$cartContext->getCart()->shouldBeCalledTimes(1)->willReturn($cart);
$shopperContext->getChannel()->willReturn($channel);
@ -217,40 +215,7 @@ final class ShopBasedCartContextSpec extends ObjectBehavior
$cart->setChannel($channel)->shouldBeCalled();
$cart->setCurrencyCode('PLN')->shouldBeCalled();
$cart->setLocaleCode('pl')->shouldBeCalled();
$cart->setCustomer($customer)->shouldBeCalled();
$this->getCart()->shouldReturn($cart);
}
function it_sets_created_by_guest_flag_as_true_if_order_is_created_by_anonymous_user(
CartContextInterface $cartContext,
ShopperContextInterface $shopperContext,
CreatedByGuestFlagResolverInterface $createdByGuestFlagResolver,
OrderInterface $cart,
ChannelInterface $channel,
CurrencyInterface $currency,
CustomerInterface $customer
): void {
$this->beConstructedWith($cartContext, $shopperContext, $createdByGuestFlagResolver);
$createdByGuestFlagResolver->resolveFlag()->willReturn(true);
$cart->setCreatedByGuest(true)->shouldBeCalled();
$cartContext->getCart()->shouldBeCalledTimes(1)->willReturn($cart);
$shopperContext->getChannel()->willReturn($channel);
$shopperContext->getLocaleCode()->willReturn('pl');
$shopperContext->getCustomer()->willReturn($customer);
$customer->getDefaultAddress()->willReturn(null);
$channel->getBaseCurrency()->willReturn($currency);
$currency->getCode()->willReturn('PLN');
$cart->setChannel($channel)->shouldBeCalled();
$cart->setCurrencyCode('PLN')->shouldBeCalled();
$cart->setLocaleCode('pl')->shouldBeCalled();
$cart->setCustomer($customer)->shouldBeCalled();
$cart->setCustomerWithAuthorization($customer)->shouldBeCalled();
$this->getCart()->shouldReturn($cart);
}

View file

@ -49,12 +49,30 @@ final class OrderSpec extends ObjectBehavior
$this->getCustomer()->shouldReturn(null);
}
function its_allows_defining_customer(CustomerInterface $customer): void
function it_allows_defining_customer(CustomerInterface $customer): void
{
$this->setCustomer($customer);
$this->getCustomer()->shouldReturn($customer);
}
function it_allows_defining_authorized_customer(CustomerInterface $customer): void
{
$this->setCustomerWithAuthorization($customer);
$this->getCustomer()->shouldReturn($customer);
$this->isCreatedByGuest()->shouldReturn(false);
}
function its_created_by_guest_customer_by_default(): void
{
$this->isCreatedByGuest()->shouldReturn(true);
}
function it_allows_to_mutate_create_by_guest_field(): void
{
$this->setCreatedByGuest(false);
$this->isCreatedByGuest()->shouldReturn(false);
}
function its_customer_can_be_nullable(): void
{
$this->setCustomer(null);