From b3280d0b0b740d389928618584650bba19309731 Mon Sep 17 00:00:00 2001 From: Vasilii Date: Sat, 16 Mar 2019 11:45:47 +0200 Subject: [PATCH 1/3] magento/graphql-ce:#468 - Resolve coupling between objects in `\Magento\QuoteGraphQl\Model\Cart\SetBillingAddressOnCart` --- .../CreateQuoteAddressByCustomerAddress.php | 78 +++++++++++++++++++ .../Model/Cart/SetBillingAddressOnCart.php | 50 ++++-------- 2 files changed, 95 insertions(+), 33 deletions(-) create mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/CreateQuoteAddressByCustomerAddress.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/CreateQuoteAddressByCustomerAddress.php b/app/code/Magento/QuoteGraphQl/Model/Cart/CreateQuoteAddressByCustomerAddress.php new file mode 100644 index 0000000000000..2e2588ea81068 --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/CreateQuoteAddressByCustomerAddress.php @@ -0,0 +1,78 @@ +quoteAddressFactory = $quoteAddressFactory; + $this->getCustomer = $getCustomer; + $this->getCustomerAddress = $getCustomerAddress; + } + + /** + * @param ContextInterface $context + * @param int|string|null $customerAddressId + * @param array|null $customerAddress + * @return Address + * @throws GraphQlAuthenticationException + * @throws GraphQlAuthorizationException + * @throws GraphQlInputException + * @throws GraphQlNoSuchEntityException + */ + public function execute( + ContextInterface $context, + $customerAddressId, + $customerAddress + ): Address { + if (null === $customerAddressId) { + return $this->quoteAddressFactory->createBasedOnInputData($customerAddress); + } + + $customer = $this->getCustomer->execute($context); + $customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$customer->getId()); + + return $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress); + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php index 31524ea023222..65afbecb2c3f5 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php @@ -7,8 +7,8 @@ namespace Magento\QuoteGraphQl\Model\Cart; -use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress; -use Magento\CustomerGraphQl\Model\Customer\GetCustomer; +use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException; +use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; @@ -20,41 +20,25 @@ class SetBillingAddressOnCart { /** - * @var QuoteAddressFactory - */ - private $quoteAddressFactory; - - /** - * @var GetCustomer - */ - private $getCustomer; - - /** - * @var GetCustomerAddress + * @var AssignBillingAddressToCart */ - private $getCustomerAddress; + private $assignBillingAddressToCart; /** - * @var AssignBillingAddressToCart + * @var CreateQuoteAddressByCustomerAddress */ - private $assignBillingAddressToCart; + private $createQuoteAddressByCustomerAddress; /** - * @param QuoteAddressFactory $quoteAddressFactory - * @param GetCustomer $getCustomer - * @param GetCustomerAddress $getCustomerAddress * @param AssignBillingAddressToCart $assignBillingAddressToCart + * @param CreateQuoteAddressByCustomerAddress $createQuoteAddressByCustomerAddress */ public function __construct( - QuoteAddressFactory $quoteAddressFactory, - GetCustomer $getCustomer, - GetCustomerAddress $getCustomerAddress, - AssignBillingAddressToCart $assignBillingAddressToCart + AssignBillingAddressToCart $assignBillingAddressToCart, + CreateQuoteAddressByCustomerAddress $createQuoteAddressByCustomerAddress ) { - $this->quoteAddressFactory = $quoteAddressFactory; - $this->getCustomer = $getCustomer; - $this->getCustomerAddress = $getCustomerAddress; $this->assignBillingAddressToCart = $assignBillingAddressToCart; + $this->createQuoteAddressByCustomerAddress = $createQuoteAddressByCustomerAddress; } /** @@ -66,6 +50,8 @@ public function __construct( * @return void * @throws GraphQlInputException * @throws GraphQlNoSuchEntityException + * @throws GraphQlAuthorizationException + * @throws GraphQlAuthenticationException */ public function execute(ContextInterface $context, CartInterface $cart, array $billingAddressInput): void { @@ -93,13 +79,11 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b ); } - if (null === $customerAddressId) { - $billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput); - } else { - $customer = $this->getCustomer->execute($context); - $customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$customer->getId()); - $billingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress); - } + $billingAddress = $this->createQuoteAddressByCustomerAddress->execute( + $context, + $customerAddressId, + $addressInput + ); $this->assignBillingAddressToCart->execute($cart, $billingAddress, $useForShipping); } From 799f37e98cae41b394a8983b5cf96b7b98cde2d2 Mon Sep 17 00:00:00 2001 From: Vasilii Date: Thu, 21 Mar 2019 09:44:31 +0200 Subject: [PATCH 2/3] magento/graphql-ce:#468 - Fix codestyle issue # Fixed issue "@throws annotation tags are added only when exception is thrown explicitly in the function" --- .../Cart/CreateQuoteAddressByCustomerAddress.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/CreateQuoteAddressByCustomerAddress.php b/app/code/Magento/QuoteGraphQl/Model/Cart/CreateQuoteAddressByCustomerAddress.php index 2e2588ea81068..ba46f5621ee82 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/CreateQuoteAddressByCustomerAddress.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/CreateQuoteAddressByCustomerAddress.php @@ -9,12 +9,8 @@ use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress; use Magento\CustomerGraphQl\Model\Customer\GetCustomer; -use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException; -use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; -use Magento\Framework\GraphQl\Exception\GraphQlInputException; -use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; -use Magento\Quote\Model\Quote\Address; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; +use Magento\Quote\Model\Quote\Address; /** * Creates a quote address based on given context, customer address ID and customer address @@ -55,11 +51,8 @@ public function __construct( * @param ContextInterface $context * @param int|string|null $customerAddressId * @param array|null $customerAddress + * * @return Address - * @throws GraphQlAuthenticationException - * @throws GraphQlAuthorizationException - * @throws GraphQlInputException - * @throws GraphQlNoSuchEntityException */ public function execute( ContextInterface $context, From 5b0348830b27ab4c9fa1e86b8743c02c4f868b4c Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Tue, 2 Apr 2019 21:29:36 -0500 Subject: [PATCH 3/3] GraphQL-468: Resolve coupling between objects in `\Magento\QuoteGraphQl\Model\Cart\SetBillingAddressOnCart` --- .../CreateQuoteAddressByCustomerAddress.php | 71 ------------------- .../Model/Cart/QuoteAddressFactory.php | 25 +++++-- .../Model/Cart/SetBillingAddressOnCart.php | 43 +++++++---- .../Model/Cart/SetShippingAddressesOnCart.php | 15 ++-- 4 files changed, 52 insertions(+), 102 deletions(-) delete mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/CreateQuoteAddressByCustomerAddress.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/CreateQuoteAddressByCustomerAddress.php b/app/code/Magento/QuoteGraphQl/Model/Cart/CreateQuoteAddressByCustomerAddress.php deleted file mode 100644 index ba46f5621ee82..0000000000000 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/CreateQuoteAddressByCustomerAddress.php +++ /dev/null @@ -1,71 +0,0 @@ -quoteAddressFactory = $quoteAddressFactory; - $this->getCustomer = $getCustomer; - $this->getCustomerAddress = $getCustomerAddress; - } - - /** - * @param ContextInterface $context - * @param int|string|null $customerAddressId - * @param array|null $customerAddress - * - * @return Address - */ - public function execute( - ContextInterface $context, - $customerAddressId, - $customerAddress - ): Address { - if (null === $customerAddressId) { - return $this->quoteAddressFactory->createBasedOnInputData($customerAddress); - } - - $customer = $this->getCustomer->execute($context); - $customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$customer->getId()); - - return $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress); - } -} diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php index 76bdc74611131..582055bc6e13d 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php @@ -7,9 +7,11 @@ namespace Magento\QuoteGraphQl\Model\Cart; -use Magento\Customer\Api\Data\AddressInterface as CustomerAddress; +use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress; use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; +use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Quote\Model\Quote\Address as QuoteAddress; use Magento\Quote\Model\Quote\AddressFactory as BaseQuoteAddressFactory; @@ -23,13 +25,21 @@ class QuoteAddressFactory */ private $quoteAddressFactory; + /** + * @var GetCustomerAddress + */ + private $getCustomerAddress; + /** * @param BaseQuoteAddressFactory $quoteAddressFactory + * @param GetCustomerAddress $getCustomerAddress */ public function __construct( - BaseQuoteAddressFactory $quoteAddressFactory + BaseQuoteAddressFactory $quoteAddressFactory, + GetCustomerAddress $getCustomerAddress ) { $this->quoteAddressFactory = $quoteAddressFactory; + $this->getCustomerAddress = $getCustomerAddress; } /** @@ -48,14 +58,19 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress } /** - * Create QuoteAddress based on CustomerAddress + * Create Quote Address based on Customer Address * - * @param CustomerAddress $customerAddress + * @param int $customerAddressId + * @param int $customerId * @return QuoteAddress * @throws GraphQlInputException + * @throws GraphQlAuthorizationException + * @throws GraphQlNoSuchEntityException */ - public function createBasedOnCustomerAddress(CustomerAddress $customerAddress): QuoteAddress + public function createBasedOnCustomerAddress(int $customerAddressId, int $customerId): QuoteAddress { + $customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, $customerId); + $quoteAddress = $this->quoteAddressFactory->create(); try { $quoteAddress->importCustomerAddressData($customerAddress); diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php index 65afbecb2c3f5..c2bac13c07067 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php @@ -7,6 +7,7 @@ namespace Magento\QuoteGraphQl\Model\Cart; +use Magento\CustomerGraphQl\Model\Customer\GetCustomer; use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException; use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; @@ -20,25 +21,33 @@ class SetBillingAddressOnCart { /** - * @var AssignBillingAddressToCart + * @var QuoteAddressFactory */ - private $assignBillingAddressToCart; + private $quoteAddressFactory; /** - * @var CreateQuoteAddressByCustomerAddress + * @var GetCustomer */ - private $createQuoteAddressByCustomerAddress; + private $getCustomer; + + /** + * @var AssignBillingAddressToCart + */ + private $assignBillingAddressToCart; /** + * @param QuoteAddressFactory $quoteAddressFactory + * @param GetCustomer $getCustomer * @param AssignBillingAddressToCart $assignBillingAddressToCart - * @param CreateQuoteAddressByCustomerAddress $createQuoteAddressByCustomerAddress */ public function __construct( - AssignBillingAddressToCart $assignBillingAddressToCart, - CreateQuoteAddressByCustomerAddress $createQuoteAddressByCustomerAddress + QuoteAddressFactory $quoteAddressFactory, + GetCustomer $getCustomer, + AssignBillingAddressToCart $assignBillingAddressToCart ) { + $this->quoteAddressFactory = $quoteAddressFactory; + $this->getCustomer = $getCustomer; $this->assignBillingAddressToCart = $assignBillingAddressToCart; - $this->createQuoteAddressByCustomerAddress = $createQuoteAddressByCustomerAddress; } /** @@ -49,9 +58,9 @@ public function __construct( * @param array $billingAddressInput * @return void * @throws GraphQlInputException - * @throws GraphQlNoSuchEntityException - * @throws GraphQlAuthorizationException * @throws GraphQlAuthenticationException + * @throws GraphQlAuthorizationException + * @throws GraphQlNoSuchEntityException */ public function execute(ContextInterface $context, CartInterface $cart, array $billingAddressInput): void { @@ -79,11 +88,15 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b ); } - $billingAddress = $this->createQuoteAddressByCustomerAddress->execute( - $context, - $customerAddressId, - $addressInput - ); + if (null === $customerAddressId) { + $billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput); + } else { + $customer = $this->getCustomer->execute($context); + $billingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress( + (int)$customerAddressId, + (int)$customer->getId() + ); + } $this->assignBillingAddressToCart->execute($cart, $billingAddress, $useForShipping); } diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php index bbca7721754cb..6b0e2a311bf44 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php @@ -7,7 +7,6 @@ namespace Magento\QuoteGraphQl\Model\Cart; -use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress; use Magento\CustomerGraphQl\Model\Customer\GetCustomer; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; @@ -28,11 +27,6 @@ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface */ private $getCustomer; - /** - * @var GetCustomerAddress - */ - private $getCustomerAddress; - /** * @var AssignShippingAddressToCart */ @@ -41,18 +35,15 @@ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface /** * @param QuoteAddressFactory $quoteAddressFactory * @param GetCustomer $getCustomer - * @param GetCustomerAddress $getCustomerAddress * @param AssignShippingAddressToCart $assignShippingAddressToCart */ public function __construct( QuoteAddressFactory $quoteAddressFactory, GetCustomer $getCustomer, - GetCustomerAddress $getCustomerAddress, AssignShippingAddressToCart $assignShippingAddressToCart ) { $this->quoteAddressFactory = $quoteAddressFactory; $this->getCustomer = $getCustomer; - $this->getCustomerAddress = $getCustomerAddress; $this->assignShippingAddressToCart = $assignShippingAddressToCart; } @@ -86,8 +77,10 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s $shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput); } else { $customer = $this->getCustomer->execute($context); - $customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$customer->getId()); - $shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress); + $shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress( + (int)$customerAddressId, + (int)$customer->getId() + ); } $this->assignShippingAddressToCart->execute($cart, $shippingAddress);