From 0d505feb486a9d1922e0c7d9393a3433b9099cd4 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Wed, 19 Jun 2019 15:40:54 -0500 Subject: [PATCH] magento/graphql-ce#741: Add extension point to set custom parameters to Query Context object --- .../Model/Context/AddUserInfoToContext.php | 30 +----- .../Model/Context/GetCustomer.php | 94 ------------------- .../Model/Customer/SaveCustomer.php | 2 - .../Model/Resolver/IsSubscribed.php | 9 -- .../Magento/GraphQl/Controller/GraphQl.php | 22 +++-- .../GraphQl/Model/Query/ContextFactory.php | 6 +- app/code/Magento/GraphQl/etc/di.xml | 1 + .../Model/Cart/GetCartForUser.php | 1 + .../Model/Cart/QuoteAddressFactory.php | 2 +- .../SetShippingAddressesOnCartInterface.php | 1 - 10 files changed, 24 insertions(+), 144 deletions(-) delete mode 100644 app/code/Magento/CustomerGraphQl/Model/Context/GetCustomer.php diff --git a/app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php b/app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php index a871d7f62ba0..c0e46b2bb727 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php +++ b/app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php @@ -21,21 +21,13 @@ class AddUserInfoToContext implements ContextParametersProcessorInterface */ private $userContext; - /** - * @var GetCustomer - */ - private $getCustomer; - /** * @param UserContextInterface $userContext - * @param GetCustomer $getCustomer */ public function __construct( - UserContextInterface $userContext, - GetCustomer $getCustomer + UserContextInterface $userContext ) { $this->userContext = $userContext; - $this->getCustomer = $getCustomer; } /** @@ -53,28 +45,8 @@ public function execute(ContextParametersInterface $contextParameters): ContextP $currentUserType = (int)$currentUserType; } - if (false === $this->isUserGuest($currentUserId, $currentUserType)) { - $customer = $this->getCustomer->execute($currentUserId); - $contextParameters->addExtensionAttribute('customer', $customer); - } - $contextParameters->setUserId($currentUserId); $contextParameters->setUserType($currentUserType); return $contextParameters; } - - /** - * Checking if current customer is guest - * - * @param int|null $customerId - * @param int|null $customerType - * @return bool - */ - private function isUserGuest(?int $customerId, ?int $customerType): bool - { - if (null === $customerId || null === $customerType) { - return true; - } - return 0 === (int)$customerId || (int)$customerType === UserContextInterface::USER_TYPE_GUEST; - } } diff --git a/app/code/Magento/CustomerGraphQl/Model/Context/GetCustomer.php b/app/code/Magento/CustomerGraphQl/Model/Context/GetCustomer.php deleted file mode 100644 index f680f1516889..000000000000 --- a/app/code/Magento/CustomerGraphQl/Model/Context/GetCustomer.php +++ /dev/null @@ -1,94 +0,0 @@ -authentication = $authentication; - $this->customerRepository = $customerRepository; - $this->accountManagement = $accountManagement; - } - - /** - * Get customer - * - * @param int $customerId - * @return void - * @throws GraphQlAuthenticationException - * @throws GraphQlAuthorizationException - * @throws GraphQlInputException - * @throws GraphQlNoSuchEntityException - */ - public function execute(int $customerId): CustomerInterface - { - try { - $customer = $this->customerRepository->getById($customerId); - } catch (NoSuchEntityException $e) { - throw new GraphQlNoSuchEntityException( - __('Customer with id "%customer_id" does not exist.', ['customer_id' => $customerId]), - $e - ); - } catch (LocalizedException $e) { - throw new GraphQlInputException(__($e->getMessage())); - } - - if (true === $this->authentication->isLocked($customerId)) { - throw new GraphQlAuthenticationException(__('The account is locked.')); - } - - try { - $confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId); - } catch (LocalizedException $e) { - throw new GraphQlInputException(__($e->getMessage())); - } - - if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) { - throw new GraphQlAuthenticationException(__("This account isn't confirmed. Verify and try again.")); - } - return $customer; - } -} diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/SaveCustomer.php b/app/code/Magento/CustomerGraphQl/Model/Customer/SaveCustomer.php index 1605c63b62f4..56ab70d12b1c 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/SaveCustomer.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/SaveCustomer.php @@ -11,7 +11,6 @@ use Magento\Framework\Exception\AlreadyExistsException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException; -use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Customer\Api\Data\CustomerInterface; @@ -39,7 +38,6 @@ public function __construct( * * @param CustomerInterface $customer * @throws GraphQlAlreadyExistsException - * @throws GraphQlAuthenticationException * @throws GraphQlInputException */ public function execute(CustomerInterface $customer): void diff --git a/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php b/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php index f956949616c9..4e49891b5870 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php +++ b/app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php @@ -7,7 +7,6 @@ namespace Magento\CustomerGraphQl\Model\Resolver; -use Magento\CustomerGraphQl\Model\Customer\GetCustomer; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Framework\GraphQl\Config\Element\Field; @@ -19,25 +18,17 @@ */ class IsSubscribed implements ResolverInterface { - /** - * @var GetCustomer - */ - private $getCustomer; - /** * @var SubscriberFactory */ private $subscriberFactory; /** - * @param GetCustomer $getCustomer * @param SubscriberFactory $subscriberFactory */ public function __construct( - GetCustomer $getCustomer, SubscriberFactory $subscriberFactory ) { - $this->getCustomer = $getCustomer; $this->subscriberFactory = $subscriberFactory; } diff --git a/app/code/Magento/GraphQl/Controller/GraphQl.php b/app/code/Magento/GraphQl/Controller/GraphQl.php index c0d3817ee682..2d72fde91b03 100644 --- a/app/code/Magento/GraphQl/Controller/GraphQl.php +++ b/app/code/Magento/GraphQl/Controller/GraphQl.php @@ -13,6 +13,7 @@ use Magento\Framework\App\ResponseInterface; use Magento\Framework\GraphQl\Exception\ExceptionFormatter; use Magento\Framework\GraphQl\Query\QueryProcessor; +use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Schema\SchemaGeneratorInterface; use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\Webapi\Response; @@ -57,9 +58,10 @@ class GraphQl implements FrontControllerInterface private $graphQlError; /** - * @var ContextFactoryInterface + * @var ContextInterface + * @deprecated $contextFactory is used for creating Context object */ - private $contextFactory; + private $resolverContext; /** * @var HttpRequestProcessor @@ -81,17 +83,23 @@ class GraphQl implements FrontControllerInterface */ private $httpResponse; + /** + * @var ContextFactoryInterface + */ + private $contextFactory; + /** * @param Response $response * @param SchemaGeneratorInterface $schemaGenerator * @param SerializerInterface $jsonSerializer * @param QueryProcessor $queryProcessor * @param ExceptionFormatter $graphQlError - * @param ContextFactoryInterface $contextFactory + * @param ContextInterface $resolverContext Deprecated. $contextFactory is used for creating Context object. * @param HttpRequestProcessor $requestProcessor * @param QueryFields $queryFields * @param JsonFactory|null $jsonFactory * @param HttpResponse|null $httpResponse + * @param ContextFactoryInterface $contextFactory * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -100,22 +108,24 @@ public function __construct( SerializerInterface $jsonSerializer, QueryProcessor $queryProcessor, ExceptionFormatter $graphQlError, - ContextFactoryInterface $contextFactory, + ContextInterface $resolverContext, HttpRequestProcessor $requestProcessor, QueryFields $queryFields, JsonFactory $jsonFactory = null, - HttpResponse $httpResponse = null + HttpResponse $httpResponse = null, + ContextFactoryInterface $contextFactory = null ) { $this->response = $response; $this->schemaGenerator = $schemaGenerator; $this->jsonSerializer = $jsonSerializer; $this->queryProcessor = $queryProcessor; $this->graphQlError = $graphQlError; - $this->contextFactory = $contextFactory; + $this->resolverContext = $resolverContext; $this->requestProcessor = $requestProcessor; $this->queryFields = $queryFields; $this->jsonFactory = $jsonFactory ?: ObjectManager::getInstance()->get(JsonFactory::class); $this->httpResponse = $httpResponse ?: ObjectManager::getInstance()->get(HttpResponse::class); + $this->contextFactory = $contextFactory ?: ObjectManager::getInstance()->get(ContextFactoryInterface::class); } /** diff --git a/app/code/Magento/GraphQl/Model/Query/ContextFactory.php b/app/code/Magento/GraphQl/Model/Query/ContextFactory.php index 0dd11018983f..bfcd0d548757 100644 --- a/app/code/Magento/GraphQl/Model/Query/ContextFactory.php +++ b/app/code/Magento/GraphQl/Model/Query/ContextFactory.php @@ -19,7 +19,7 @@ class ContextFactory implements ContextFactoryInterface /** * @var ExtensionAttributesFactory */ - protected $extensionAttributesFactory; + private $extensionAttributesFactory; /** * @var ObjectManagerInterface @@ -64,7 +64,9 @@ public function create(): ContextInterface $extensionAttributes = $this->extensionAttributesFactory->create( ContextInterface::class, - $contextParameters->getExtensionAttributesData() + [ + 'data' => $contextParameters->getExtensionAttributesData(), + ] ); $context = $this->objectManager->create( diff --git a/app/code/Magento/GraphQl/etc/di.xml b/app/code/Magento/GraphQl/etc/di.xml index d76101e5556c..b356f33c4f4b 100644 --- a/app/code/Magento/GraphQl/etc/di.xml +++ b/app/code/Magento/GraphQl/etc/di.xml @@ -7,6 +7,7 @@ --> + diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php b/app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php index 3506ffc8c879..12987509b025 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php @@ -58,6 +58,7 @@ public function __construct( * @return Quote * @throws GraphQlAuthorizationException * @throws GraphQlNoSuchEntityException + * @throws NoSuchEntityException */ public function execute(string $cartHash, ?int $customerId): Quote { diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php index 13d6a1d3dce7..a66c0ddb1a53 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php @@ -80,8 +80,8 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress * @param int $customerAddressId * @param int $customerId * @return QuoteAddress - * @throws GraphQlInputException * @throws GraphQlAuthorizationException + * @throws GraphQlInputException * @throws GraphQlNoSuchEntityException */ public function createBasedOnCustomerAddress(int $customerAddressId, int $customerId): QuoteAddress diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCartInterface.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCartInterface.php index eb0f3522102c..de299e34d9c0 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCartInterface.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCartInterface.php @@ -31,7 +31,6 @@ interface SetShippingAddressesOnCartInterface * @return void * @throws GraphQlInputException * @throws GraphQlAuthorizationException - * @throws GraphQlAuthenticationException * @throws GraphQlNoSuchEntityException */ public function execute(ContextInterface $context, CartInterface $cart, array $shippingAddressesInput): void;