From 89638111af1c607ef16a6e8e433f242ad7fb0262 Mon Sep 17 00:00:00 2001 From: Lyzun Oleksandr Date: Mon, 3 Sep 2018 16:28:08 +0200 Subject: [PATCH] Code refactoring --- .../Customer/CustomerDataProvider.php | 132 +++++++++++++++++- .../Model/Resolver/CustomerUpdate.php | 51 +------ 2 files changed, 132 insertions(+), 51 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Resolver/Customer/CustomerDataProvider.php b/app/code/Magento/CustomerGraphQl/Model/Resolver/Customer/CustomerDataProvider.php index 1a942a2ab149a..6b3c02ea98d41 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Resolver/Customer/CustomerDataProvider.php +++ b/app/code/Magento/CustomerGraphQl/Model/Resolver/Customer/CustomerDataProvider.php @@ -13,6 +13,11 @@ use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\Webapi\ServiceOutputProcessor; use Magento\Customer\Api\Data\CustomerInterface; +use Magento\Newsletter\Model\SubscriberFactory; +use Magento\Customer\Model\CustomerRegistry; +use Magento\Framework\Encryption\EncryptorInterface as Encryptor; +use Magento\Store\Api\StoreResolverInterface; +use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; /** * Customer field data provider, used for GraphQL request processing. @@ -29,11 +34,31 @@ class CustomerDataProvider */ private $serviceOutputProcessor; + /** + * @var StoreResolverInterface + */ + private $storeResolver; + + /** + * @var \Magento\Newsletter\Model\SubscriberFactory + */ + protected $subscriberFactory; + + /** + * @var CustomerRegistry + */ + protected $customerRegistry; + /** * @var SerializerInterface */ private $jsonSerializer; + /** + * @var Encryptor + */ + protected $encryptor; + /** * @param CustomerRepositoryInterface $customerRepository * @param ServiceOutputProcessor $serviceOutputProcessor @@ -42,11 +67,32 @@ class CustomerDataProvider public function __construct( CustomerRepositoryInterface $customerRepository, ServiceOutputProcessor $serviceOutputProcessor, - SerializerInterface $jsonSerializer + SerializerInterface $jsonSerializer, + SubscriberFactory $subscriberFactory, + CustomerRegistry $customerRegistry, + Encryptor $encryptor, + StoreResolverInterface $storeResolver ) { $this->customerRepository = $customerRepository; $this->serviceOutputProcessor = $serviceOutputProcessor; $this->jsonSerializer = $jsonSerializer; + $this->subscriberFactory = $subscriberFactory; + $this->customerRegistry = $customerRegistry; + $this->encryptor = $encryptor; + $this->storeResolver = $storeResolver; + } + + /** + * Load customer object + * + * @param int $customerId + * @return CustomerInterface + * @throws LocalizedException + * @throws NoSuchEntityException + */ + public function loadCustomerById(int $customerId): CustomerInterface + { + return $this->customerRepository->getById($customerId); } /** @@ -56,7 +102,7 @@ public function __construct( * @return array * @throws NoSuchEntityException|LocalizedException */ - public function getCustomerById(int $customerId) : array + public function getCustomerById(int $customerId): array { try { $customerObject = $this->customerRepository->getById($customerId); @@ -73,7 +119,7 @@ public function getCustomerById(int $customerId) : array * @param CustomerInterface $customerObject * @return array */ - private function processCustomer(CustomerInterface $customerObject) : array + private function processCustomer(CustomerInterface $customerObject): array { $customer = $this->serviceOutputProcessor->process( $customerObject, @@ -110,4 +156,84 @@ private function processCustomer(CustomerInterface $customerObject) : array return $customer; } + + /** + * Check if customer is subscribed to Newsletter + * + * @param int $customerId + * @return bool + */ + public function isSubscribed(int $customerId): bool + { + $checkSubscriber = $this->subscriberFactory->create()->loadByCustomerId($customerId); + return $checkSubscriber->isSubscribed(); + } + + /** + * Manage customer subscription. Subscribe OR unsubscribe if required + * + * @param int $customerId + * @param $newSubscriptionStatus + * @return bool + */ + public function manageSubscription(int $customerId, bool $newSubscriptionStatus): bool + { + $checkSubscriber = $this->subscriberFactory->create()->loadByCustomerId($customerId); + $isSubscribed = $this->isSubscribed($customerId); + + if ($newSubscriptionStatus === true && !$isSubscribed) { + $this->subscriberFactory->create()->subscribeCustomerById($customerId); + } elseif ($newSubscriptionStatus === false && $checkSubscriber->isSubscribed()) { + $this->subscriberFactory->create()->unsubscribeCustomerById($customerId); + } + return true; + } + + /** + * @param int $customerId + * @param array $customerData + * @return CustomerInterface + * @throws LocalizedException + * @throws NoSuchEntityException + * @throws \Magento\Framework\Exception\InputException + * @throws \Magento\Framework\Exception\State\InputMismatchException + */ + public function updateAccountInformation(int $customerId, array $customerData): CustomerInterface + { + + $customer = $this->loadCustomerById($customerId); + + if (isset($customerData['email']) + && $customer->getEmail() !== $customerData['email'] + && isset($customerData['password'])) { + if ($this->isPasswordCorrect($customerData['password'], $customerId)) { + $customer->setEmail($customerData['email']); + } else { + throw new GraphQlAuthorizationException(__('Invalid current user password.')); + } + } + + if (isset($customerData['firstname'])) { + $customer->setFirstname($customerData['firstname']); + } + if (isset($customerData['lastname'])) { + $customer->setLastname($customerData['lastname']); + } + + $customer->setStoreId($this->storeResolver->getCurrentStoreId()); + $this->customerRepository->save($customer); + + return $customer; + } + + private function isPasswordCorrect(string $password, int $customerId) + { + + $customerSecure = $this->customerRegistry->retrieveSecureData($customerId); + $hash = $customerSecure->getPasswordHash(); + if (!$this->encryptor->validateHash($password, $hash)) { + return false; + } + return true; + } } diff --git a/app/code/Magento/CustomerGraphQl/Model/Resolver/CustomerUpdate.php b/app/code/Magento/CustomerGraphQl/Model/Resolver/CustomerUpdate.php index 5bce9dfa1d581..51f62a7a8cafd 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Resolver/CustomerUpdate.php +++ b/app/code/Magento/CustomerGraphQl/Model/Resolver/CustomerUpdate.php @@ -16,9 +16,6 @@ use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; use Magento\Framework\GraphQl\Query\ResolverInterface; -use Magento\Store\Api\StoreResolverInterface; -use Magento\Framework\Encryption\EncryptorInterface as Encryptor; -use Magento\Customer\Model\CustomerRegistry; /** * Customers field resolver, used for GraphQL request processing. @@ -35,11 +32,6 @@ class CustomerUpdate implements ResolverInterface */ private $valueFactory; - /** - * @var StoreResolverInterface - */ - private $storeResolver; - /** * @var \Magento\Newsletter\Model\SubscriberFactory */ @@ -50,11 +42,6 @@ class CustomerUpdate implements ResolverInterface */ protected $customerRegistry; - /** - * @var Encryptor - */ - protected $encryptor; - /** * @param CustomerDataProvider $customerResolver * @param ValueFactory $valueFactory @@ -62,19 +49,11 @@ class CustomerUpdate implements ResolverInterface public function __construct( CustomerDataProvider $customerResolver, ValueFactory $valueFactory, - \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, - \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory, - StoreResolverInterface $storeResolver, - Encryptor $encryptor, - CustomerRegistry $customerRegistry + \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory ) { $this->customerResolver = $customerResolver; $this->valueFactory = $valueFactory; - $this->customerRepository = $customerRepository; $this->subscriberFactory = $subscriberFactory; - $this->storeResolver = $storeResolver; - $this->encryptor = $encryptor; - $this->customerRegistry = $customerRegistry; } /** @@ -98,34 +77,10 @@ public function resolve( ); } - $customer = $this->customerRepository->getById($context->getUserId()); - - if (isset($args['email']) && $customer->getEmail() !== $args['email']) { - $customerSecure = $this->customerRegistry->retrieveSecureData($context->getUserId()); - $hash = $customerSecure->getPasswordHash(); - if (!$this->encryptor->validateHash($args['password'], $hash)) { - throw new GraphQlAuthorizationException(__('Invalid login or password.')); - } - $customer->setEmail($args['email']); - } - - if (isset($args['firstname'])) { - $customer->setFirstname($args['firstname']); - } - if (isset($args['lastname'])) { - $customer->setLastname($args['lastname']); - } - - $customer->setStoreId($this->storeResolver->getCurrentStoreId()); - $this->customerRepository->save($customer); + $this->customerResolver->updateAccountInformation($context->getUserId(), $args); if (isset($args['is_subscribed'])) { - $checkSubscriber = $this->subscriberFactory->create()->loadByCustomerId($context->getUserId()); - if ($args['is_subscribed'] === true && !$checkSubscriber->isSubscribed()) { - $this->subscriberFactory->create()->subscribeCustomerById($context->getUserId()); - } elseif ($args['is_subscribed'] === false && $checkSubscriber->isSubscribed()) { - $this->subscriberFactory->create()->unsubscribeCustomerById($context->getUserId()); - } + $this->customerResolver->manageSubscription($context->getUserId(), $args['is_subscribed']); } $data = $args;