Skip to content

Commit

Permalink
GraphQL-55: [Mutations] My Account > Change account information
Browse files Browse the repository at this point in the history
  • Loading branch information
Valeriy Nayda committed Oct 9, 2018
1 parent 20e3393 commit 661988b
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

Expand Down Expand Up @@ -44,8 +43,8 @@ class ChangePassword implements ResolverInterface

/**
* @param CheckCustomerAccount $checkCustomerAccount
* @param CheckCustomerAccount $checkCustomerPassword
* @param CheckCustomerPassword $accountManagement
* @param CheckCustomerPassword $checkCustomerPassword
* @param AccountManagementInterface $accountManagement
* @param CustomerDataProvider $customerDataProvider
*/
public function __construct(
Expand Down Expand Up @@ -78,7 +77,6 @@ public function resolve(
throw new GraphQlInputException(__('"newPassword" value should be specified'));
}

/** @var ContextInterface $context */
$currentUserId = $context->getUserId();
$currentUserType = $context->getUserType();
$currentUserId = (int)$currentUserId;
Expand Down
2 changes: 0 additions & 2 deletions app/code/Magento/CustomerGraphQl/Model/Resolver/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\CustomerGraphQl\Model\Customer\CustomerDataProvider;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\ResolverInterface;

/**
Expand Down Expand Up @@ -51,7 +50,6 @@ public function resolve(
array $value = null,
array $args = null
) {
/** @var ContextInterface $context */
$currentUserId = $context->getUserId();
$currentUserType = $context->getUserType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Newsletter\Model\SubscriberFactory;

Expand Down Expand Up @@ -51,7 +50,6 @@ public function resolve(
array $value = null,
array $args = null
) {
/** @var ContextInterface $context */
$currentUserId = $context->getUserId();
$currentUserType = $context->getUserType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\CustomerGraphQl\Model\Customer\CustomerDataProvider;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\ResolverInterface;

/**
Expand Down Expand Up @@ -74,7 +73,6 @@ public function resolve(
throw new GraphQlInputException(__('"input" value should be specified'));
}

/** @var ContextInterface $context */
$currentUserId = $context->getUserId();
$currentUserType = $context->getUserType();

Expand Down

This file was deleted.

61 changes: 2 additions & 59 deletions app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,47 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\Message\AbstractMessage;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Magento\Quote\Model\Quote;
use Magento\QuoteGraphQl\Model\Authorization\IsCartMutationAllowedForCurrentUser;

/**
* Add products to cart
*/
class AddProductsToCart
{
/**
* @var MaskedQuoteIdToQuoteIdInterface
*/
private $maskedQuoteIdToQuoteId;

/**
* @var CartRepositoryInterface
*/
private $cartRepository;

/**
* @var IsCartMutationAllowedForCurrentUser
*/
private $isCartMutationAllowedForCurrentUser;

/**
* @var AddSimpleProductToCart
*/
private $addProductToCart;

/**
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
* @param CartRepositoryInterface $cartRepository
* @param IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
* @param AddSimpleProductToCart $addProductToCart
*/
public function __construct(
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
CartRepositoryInterface $cartRepository,
IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser,
AddSimpleProductToCart $addProductToCart
) {
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
$this->cartRepository = $cartRepository;
$this->isCartMutationAllowedForCurrentUser = $isCartMutationAllowedForCurrentUser;
$this->addProductToCart = $addProductToCart;
}

/**
* Add products to cart
*
* @param string $cartHash
* @param Quote $cart
* @param array $cartItems
* @return Quote
* @throws GraphQlInputException
*/
public function execute(string $cartHash, array $cartItems): Quote
public function execute(Quote $cart, array $cartItems): void
{
$cart = $this->getCart($cartHash);

foreach ($cartItems as $cartItemData) {
$this->addProductToCart->execute($cart, $cartItemData);
}
Expand All @@ -83,39 +59,6 @@ public function execute(string $cartHash, array $cartItems): Quote
}

$this->cartRepository->save($cart);
return $cart;
}

/**
* Get cart
*
* @param string $cartHash
* @return Quote
* @throws GraphQlNoSuchEntityException
* @throws GraphQlAuthorizationException
*/
private function getCart(string $cartHash): Quote
{
try {
$cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash);
$cart = $this->cartRepository->get($cartId);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
);
}

if (false === $this->isCartMutationAllowedForCurrentUser->execute($cartId)) {
throw new GraphQlAuthorizationException(
__(
'The current user cannot perform operations on cart "%masked_cart_id"',
['masked_cart_id' => $cartHash]
)
);
}

/** @var Quote $cart */
return $cart;
}

/**
Expand Down
89 changes: 89 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Magento\Quote\Model\Quote;

/**
* Get cart
*/
class GetCartForUser
{
/**
* @var MaskedQuoteIdToQuoteIdInterface
*/
private $maskedQuoteIdToQuoteId;

/**
* @var CartRepositoryInterface
*/
private $cartRepository;

/**
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
CartRepositoryInterface $cartRepository
) {
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
$this->cartRepository = $cartRepository;
}

/**
* Get cart for user
*
* @param string $cartHash
* @param int|null $userId
* @return Quote
* @throws GraphQlAuthenticationException
* @throws GraphQlNoSuchEntityException
*/
public function execute(string $cartHash, ?int $userId): Quote
{
try {
$cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
);
}

try {
/** @var Quote $cart */
$cart = $this->cartRepository->get($cartId);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
);
}

$customerId = (int)$cart->getCustomerId();

/* Guest cart, allow operations */
if (!$customerId) {
return $cart;
}

if ($customerId !== $userId) {
throw new GraphQlAuthenticationException(
__(
'The current user cannot perform operations on cart "%masked_cart_id"',
['masked_cart_id' => $cartHash]
)
);
}
return $cart;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Magento\Framework\Stdlib\ArrayManager;
use Magento\QuoteGraphQl\Model\Cart\AddProductsToCart;
use Magento\QuoteGraphQl\Model\Cart\ExtractDataFromCart;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;

/**
* Add simple products to cart GraphQl resolver
Expand All @@ -26,6 +27,11 @@ class AddSimpleProductsToCart implements ResolverInterface
*/
private $arrayManager;

/**
* @var GetCartForUser
*/
private $getCartForUser;

/**
* @var AddProductsToCart
*/
Expand All @@ -38,15 +44,18 @@ class AddSimpleProductsToCart implements ResolverInterface

/**
* @param ArrayManager $arrayManager
* @param GetCartForUser $getCartForUser
* @param AddProductsToCart $addProductsToCart
* @param ExtractDataFromCart $extractDataFromCart
*/
public function __construct(
ArrayManager $arrayManager,
GetCartForUser $getCartForUser,
AddProductsToCart $addProductsToCart,
ExtractDataFromCart $extractDataFromCart
) {
$this->arrayManager = $arrayManager;
$this->getCartForUser = $getCartForUser;
$this->addProductsToCart = $addProductsToCart;
$this->extractDataFromCart = $extractDataFromCart;
}
Expand All @@ -67,7 +76,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
throw new GraphQlInputException(__('Missing key "cartItems" in cart data'));
}

$cart = $this->addProductsToCart->execute((string)$cartHash, $cartItems);
$currentUserId = $context->getUserId();
$cart = $this->getCartForUser->execute((string)$cartHash, $currentUserId);

$this->addProductsToCart->execute($cart, $cartItems);
$cartData = $this->extractDataFromCart->execute($cart);

return [
Expand Down
Loading

0 comments on commit 661988b

Please sign in to comment.