Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nuzil committed Sep 3, 2018
1 parent ef7f579 commit 8963811
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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);
}

/**
Expand All @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
}
51 changes: 3 additions & 48 deletions app/code/Magento/CustomerGraphQl/Model/Resolver/CustomerUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -35,11 +32,6 @@ class CustomerUpdate implements ResolverInterface
*/
private $valueFactory;

/**
* @var StoreResolverInterface
*/
private $storeResolver;

/**
* @var \Magento\Newsletter\Model\SubscriberFactory
*/
Expand All @@ -50,31 +42,18 @@ class CustomerUpdate implements ResolverInterface
*/
protected $customerRegistry;

/**
* @var Encryptor
*/
protected $encryptor;

/**
* @param CustomerDataProvider $customerResolver
* @param ValueFactory $valueFactory
*/
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;
}

/**
Expand All @@ -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;
Expand Down

0 comments on commit 8963811

Please sign in to comment.