Skip to content

Commit

Permalink
Merge pull request magento#4389 from magento-engcom/graphql-develop-prs
Browse files Browse the repository at this point in the history
[Magento Community Engineering] Community Contributions - GraphQL
  • Loading branch information
naydav authored Jun 21, 2019
2 parents d617351 + e3c35ed commit 5177344
Show file tree
Hide file tree
Showing 65 changed files with 1,648 additions and 363 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@

use Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderInterface;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Framework\GraphQL\DataObjectConverter;

/**
* DataProvider Model for Authorizenet
*/
class AuthorizenetDataProvider implements AdditionalDataProviderInterface
{
private const PATH_ADDITIONAL_DATA = 'input/payment_method/additional_data/authorizenet_acceptjs';
private const PATH_ADDITIONAL_DATA = 'authorizenet_acceptjs';

/**
* @var ArrayManager
Expand All @@ -36,12 +35,12 @@ public function __construct(
/**
* Return additional data
*
* @param array $args
* @param array $data
* @return array
*/
public function getData(array $args): array
public function getData(array $data): array
{
$additionalData = $this->arrayManager->get(static::PATH_ADDITIONAL_DATA, $args) ?? [];
$additionalData = $this->arrayManager->get(static::PATH_ADDITIONAL_DATA, $data) ?? [];
foreach ($additionalData as $key => $value) {
$additionalData[$this->snakeCaseToCamelCase($key)] = $value;
unset($additionalData[$key]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,27 @@ public function execute(ContextParametersInterface $contextParameters): ContextP
if (null !== $currentUserId) {
$currentUserId = (int)$currentUserId;
}
$contextParameters->setUserId($currentUserId);

$currentUserType = $this->userContext->getUserType();
if (null !== $currentUserType) {
$currentUserType = (int)$currentUserType;
}

$contextParameters->setUserId($currentUserId);
$contextParameters->setUserType($currentUserType);

$contextParameters->addExtensionAttribute('is_customer', $this->isCustomer($currentUserId, $currentUserType));
return $contextParameters;
}

/**
* Checking if current user is logged
*
* @param int|null $customerId
* @param int|null $customerType
* @return bool
*/
private function isCustomer(?int $customerId, ?int $customerType): bool
{
return !empty($customerId) && !empty($customerType) && $customerType !== UserContextInterface::USER_TYPE_GUEST;
}
}
23 changes: 1 addition & 22 deletions app/code/Magento/CustomerGraphQl/Model/Customer/GetCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\CustomerGraphQl\Model\Customer;

use Magento\Authorization\Model\UserContextInterface;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
Expand All @@ -18,7 +17,7 @@
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;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Get customer
Expand Down Expand Up @@ -68,11 +67,6 @@ public function __construct(
public function execute(ContextInterface $context): CustomerInterface
{
$currentUserId = $context->getUserId();
$currentUserType = $context->getUserType();

if (true === $this->isUserGuest($currentUserId, $currentUserType)) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

try {
$customer = $this->customerRepository->getById($currentUserId);
Expand Down Expand Up @@ -100,19 +94,4 @@ public function execute(ContextInterface $context): CustomerInterface
}
return $customer;
}

/**
* 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;
}
}
13 changes: 10 additions & 3 deletions app/code/Magento/CustomerGraphQl/Model/Resolver/ChangePassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Change customer password resolver
Expand Down Expand Up @@ -70,6 +72,11 @@ public function resolve(
array $value = null,
array $args = null
) {
/** @var ContextInterface $context */
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

if (!isset($args['currentPassword']) || '' == trim($args['currentPassword'])) {
throw new GraphQlInputException(__('Specify the "currentPassword" value.'));
}
Expand All @@ -78,16 +85,16 @@ public function resolve(
throw new GraphQlInputException(__('Specify the "newPassword" value.'));
}

$customer = $this->getCustomer->execute($context);
$customerId = (int)$customer->getId();

$customerId = $context->getUserId();
$this->checkCustomerPassword->execute($args['currentPassword'], $customerId);

try {
$this->accountManagement->changePasswordById($customerId, $args['currentPassword'], $args['newPassword']);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}

$customer = $this->getCustomer->execute($context);
return $this->extractCustomerData->execute($customer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function resolve(
array $value = null,
array $args = null
) {
if (!isset($args['input']) || !is_array($args['input']) || empty($args['input'])) {
if (empty($args['input']) || !is_array($args['input'])) {
throw new GraphQlInputException(__('"input" value should be specified'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@

use Magento\CustomerGraphQl\Model\Customer\Address\CreateCustomerAddress as CreateCustomerAddressModel;
use Magento\CustomerGraphQl\Model\Customer\Address\ExtractCustomerAddressData;
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Customers address create, used for GraphQL request processing
*/
class CreateCustomerAddress implements ResolverInterface
{
/**
* @var GetCustomer
*/
private $getCustomer;

/**
* @var CreateCustomerAddressModel
*/
Expand All @@ -36,16 +32,13 @@ class CreateCustomerAddress implements ResolverInterface
private $extractCustomerAddressData;

/**
* @param GetCustomer $getCustomer
* @param CreateCustomerAddressModel $createCustomerAddress
* @param ExtractCustomerAddressData $extractCustomerAddressData
*/
public function __construct(
GetCustomer $getCustomer,
CreateCustomerAddressModel $createCustomerAddress,
ExtractCustomerAddressData $extractCustomerAddressData
) {
$this->getCustomer = $getCustomer;
$this->createCustomerAddress = $createCustomerAddress;
$this->extractCustomerAddressData = $extractCustomerAddressData;
}
Expand All @@ -60,13 +53,16 @@ public function resolve(
array $value = null,
array $args = null
) {
if (!isset($args['input']) || !is_array($args['input']) || empty($args['input'])) {
throw new GraphQlInputException(__('"input" value should be specified'));
/** @var ContextInterface $context */
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

$customer = $this->getCustomer->execute($context);
if (empty($args['input']) || !is_array($args['input'])) {
throw new GraphQlInputException(__('"input" value should be specified'));
}

$address = $this->createCustomerAddress->execute((int)$customer->getId(), $args['input']);
$address = $this->createCustomerAddress->execute($context->getUserId(), $args['input']);
return $this->extractCustomerAddressData->execute($address);
}
}
8 changes: 7 additions & 1 deletion app/code/Magento/CustomerGraphQl/Model/Resolver/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\CustomerGraphQl\Model\Customer\ExtractCustomerData;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Customers field resolver, used for GraphQL request processing.
Expand Down Expand Up @@ -50,8 +52,12 @@ public function resolve(
array $value = null,
array $args = null
) {
$customer = $this->getCustomer->execute($context);
/** @var ContextInterface $context */
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

$customer = $this->getCustomer->execute($context);
return $this->extractCustomerData->execute($customer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\Customer\Model\Customer;
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;
Expand All @@ -20,25 +19,17 @@
*/
class CustomerAddresses implements ResolverInterface
{
/**
* @var GetCustomer
*/
private $getCustomer;

/**
* @var ExtractCustomerAddressData
*/
private $extractCustomerAddressData;

/**
* @param GetCustomer $getCustomer
* @param ExtractCustomerAddressData $extractCustomerAddressData
*/
public function __construct(
GetCustomer $getCustomer,
ExtractCustomerAddressData $extractCustomerAddressData
) {
$this->getCustomer = $getCustomer;
$this->extractCustomerAddressData = $extractCustomerAddressData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@

use Magento\CustomerGraphQl\Model\Customer\Address\DeleteCustomerAddress as DeleteCustomerAddressModel;
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Customers address delete, used for GraphQL request processing.
*/
class DeleteCustomerAddress implements ResolverInterface
{
/**
* @var GetCustomer
*/
private $getCustomer;

/**
* @var GetCustomerAddress
*/
Expand All @@ -36,16 +32,13 @@ class DeleteCustomerAddress implements ResolverInterface
private $deleteCustomerAddress;

/**
* @param GetCustomer $getCustomer
* @param GetCustomerAddress $getCustomerAddress
* @param DeleteCustomerAddressModel $deleteCustomerAddress
*/
public function __construct(
GetCustomer $getCustomer,
GetCustomerAddress $getCustomerAddress,
DeleteCustomerAddressModel $deleteCustomerAddress
) {
$this->getCustomer = $getCustomer;
$this->getCustomerAddress = $getCustomerAddress;
$this->deleteCustomerAddress = $deleteCustomerAddress;
}
Expand All @@ -60,13 +53,16 @@ public function resolve(
array $value = null,
array $args = null
) {
if (!isset($args['id']) || empty($args['id'])) {
throw new GraphQlInputException(__('Address "id" value should be specified'));
/** @var ContextInterface $context */
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

$customer = $this->getCustomer->execute($context);
$address = $this->getCustomerAddress->execute((int)$args['id'], (int)$customer->getId());
if (empty($args['id'])) {
throw new GraphQlInputException(__('Address "id" value should be specified'));
}

$address = $this->getCustomerAddress->execute((int)$args['id'], $context->getUserId());
$this->deleteCustomerAddress->execute($address);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public function resolve(
array $value = null,
array $args = null
) {
if (!isset($args['email']) || empty($args['email'])) {
if (empty($args['email'])) {
throw new GraphQlInputException(__('Specify the "email" value.'));
}

if (!isset($args['password']) || empty($args['password'])) {
if (empty($args['password'])) {
throw new GraphQlInputException(__('Specify the "password" value.'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function resolve(
array $value = null,
array $args = null
) {
if (!isset($args['email']) || empty($args['email'])) {
if (empty($args['email'])) {
throw new GraphQlInputException(__('Email must be specified'));
}

Expand Down
Loading

0 comments on commit 5177344

Please sign in to comment.