Skip to content

Commit 661988b

Browse files
author
Valeriy Nayda
committed
GraphQL-55: [Mutations] My Account > Change account information
1 parent 20e3393 commit 661988b

File tree

14 files changed

+146
-270
lines changed

14 files changed

+146
-270
lines changed

app/code/Magento/CustomerGraphQl/Model/Resolver/ChangePassword.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
1414
use Magento\Framework\GraphQl\Config\Element\Field;
1515
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
16-
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1716
use Magento\Framework\GraphQl\Query\ResolverInterface;
1817
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1918

@@ -44,8 +43,8 @@ class ChangePassword implements ResolverInterface
4443

4544
/**
4645
* @param CheckCustomerAccount $checkCustomerAccount
47-
* @param CheckCustomerAccount $checkCustomerPassword
48-
* @param CheckCustomerPassword $accountManagement
46+
* @param CheckCustomerPassword $checkCustomerPassword
47+
* @param AccountManagementInterface $accountManagement
4948
* @param CustomerDataProvider $customerDataProvider
5049
*/
5150
public function __construct(
@@ -78,7 +77,6 @@ public function resolve(
7877
throw new GraphQlInputException(__('"newPassword" value should be specified'));
7978
}
8079

81-
/** @var ContextInterface $context */
8280
$currentUserId = $context->getUserId();
8381
$currentUserType = $context->getUserType();
8482
$currentUserId = (int)$currentUserId;

app/code/Magento/CustomerGraphQl/Model/Resolver/Customer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1212
use Magento\CustomerGraphQl\Model\Customer\CustomerDataProvider;
1313
use Magento\Framework\GraphQl\Config\Element\Field;
14-
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1514
use Magento\Framework\GraphQl\Query\ResolverInterface;
1615

1716
/**
@@ -51,7 +50,6 @@ public function resolve(
5150
array $value = null,
5251
array $args = null
5352
) {
54-
/** @var ContextInterface $context */
5553
$currentUserId = $context->getUserId();
5654
$currentUserType = $context->getUserType();
5755

app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
1111
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1212
use Magento\Framework\GraphQl\Config\Element\Field;
13-
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1413
use Magento\Framework\GraphQl\Query\ResolverInterface;
1514
use Magento\Newsletter\Model\SubscriberFactory;
1615

@@ -51,7 +50,6 @@ public function resolve(
5150
array $value = null,
5251
array $args = null
5352
) {
54-
/** @var ContextInterface $context */
5553
$currentUserId = $context->getUserId();
5654
$currentUserType = $context->getUserType();
5755

app/code/Magento/CustomerGraphQl/Model/Resolver/UpdateCustomer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1515
use Magento\CustomerGraphQl\Model\Customer\CustomerDataProvider;
1616
use Magento\Framework\GraphQl\Config\Element\Field;
17-
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1817
use Magento\Framework\GraphQl\Query\ResolverInterface;
1918

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

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

app/code/Magento/QuoteGraphQl/Model/Authorization/IsCartMutationAllowedForCurrentUser.php

Lines changed: 0 additions & 67 deletions
This file was deleted.

app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,71 +7,47 @@
77

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10-
use Magento\Framework\Exception\NoSuchEntityException;
11-
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1210
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13-
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1411
use Magento\Framework\Message\AbstractMessage;
1512
use Magento\Quote\Api\CartRepositoryInterface;
16-
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
1713
use Magento\Quote\Model\Quote;
18-
use Magento\QuoteGraphQl\Model\Authorization\IsCartMutationAllowedForCurrentUser;
1914

2015
/**
2116
* Add products to cart
2217
*/
2318
class AddProductsToCart
2419
{
25-
/**
26-
* @var MaskedQuoteIdToQuoteIdInterface
27-
*/
28-
private $maskedQuoteIdToQuoteId;
29-
3020
/**
3121
* @var CartRepositoryInterface
3222
*/
3323
private $cartRepository;
3424

35-
/**
36-
* @var IsCartMutationAllowedForCurrentUser
37-
*/
38-
private $isCartMutationAllowedForCurrentUser;
39-
4025
/**
4126
* @var AddSimpleProductToCart
4227
*/
4328
private $addProductToCart;
4429

4530
/**
46-
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
4731
* @param CartRepositoryInterface $cartRepository
48-
* @param IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
4932
* @param AddSimpleProductToCart $addProductToCart
5033
*/
5134
public function __construct(
52-
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
5335
CartRepositoryInterface $cartRepository,
54-
IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser,
5536
AddSimpleProductToCart $addProductToCart
5637
) {
57-
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
5838
$this->cartRepository = $cartRepository;
59-
$this->isCartMutationAllowedForCurrentUser = $isCartMutationAllowedForCurrentUser;
6039
$this->addProductToCart = $addProductToCart;
6140
}
6241

6342
/**
6443
* Add products to cart
6544
*
66-
* @param string $cartHash
45+
* @param Quote $cart
6746
* @param array $cartItems
68-
* @return Quote
6947
* @throws GraphQlInputException
7048
*/
71-
public function execute(string $cartHash, array $cartItems): Quote
49+
public function execute(Quote $cart, array $cartItems): void
7250
{
73-
$cart = $this->getCart($cartHash);
74-
7551
foreach ($cartItems as $cartItemData) {
7652
$this->addProductToCart->execute($cart, $cartItemData);
7753
}
@@ -83,39 +59,6 @@ public function execute(string $cartHash, array $cartItems): Quote
8359
}
8460

8561
$this->cartRepository->save($cart);
86-
return $cart;
87-
}
88-
89-
/**
90-
* Get cart
91-
*
92-
* @param string $cartHash
93-
* @return Quote
94-
* @throws GraphQlNoSuchEntityException
95-
* @throws GraphQlAuthorizationException
96-
*/
97-
private function getCart(string $cartHash): Quote
98-
{
99-
try {
100-
$cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash);
101-
$cart = $this->cartRepository->get($cartId);
102-
} catch (NoSuchEntityException $e) {
103-
throw new GraphQlNoSuchEntityException(
104-
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
105-
);
106-
}
107-
108-
if (false === $this->isCartMutationAllowedForCurrentUser->execute($cartId)) {
109-
throw new GraphQlAuthorizationException(
110-
__(
111-
'The current user cannot perform operations on cart "%masked_cart_id"',
112-
['masked_cart_id' => $cartHash]
113-
)
114-
);
115-
}
116-
117-
/** @var Quote $cart */
118-
return $cart;
11962
}
12063

12164
/**
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
13+
use Magento\Quote\Api\CartRepositoryInterface;
14+
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
15+
use Magento\Quote\Model\Quote;
16+
17+
/**
18+
* Get cart
19+
*/
20+
class GetCartForUser
21+
{
22+
/**
23+
* @var MaskedQuoteIdToQuoteIdInterface
24+
*/
25+
private $maskedQuoteIdToQuoteId;
26+
27+
/**
28+
* @var CartRepositoryInterface
29+
*/
30+
private $cartRepository;
31+
32+
/**
33+
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
34+
* @param CartRepositoryInterface $cartRepository
35+
*/
36+
public function __construct(
37+
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
38+
CartRepositoryInterface $cartRepository
39+
) {
40+
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
41+
$this->cartRepository = $cartRepository;
42+
}
43+
44+
/**
45+
* Get cart for user
46+
*
47+
* @param string $cartHash
48+
* @param int|null $userId
49+
* @return Quote
50+
* @throws GraphQlAuthenticationException
51+
* @throws GraphQlNoSuchEntityException
52+
*/
53+
public function execute(string $cartHash, ?int $userId): Quote
54+
{
55+
try {
56+
$cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash);
57+
} catch (NoSuchEntityException $exception) {
58+
throw new GraphQlNoSuchEntityException(
59+
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
60+
);
61+
}
62+
63+
try {
64+
/** @var Quote $cart */
65+
$cart = $this->cartRepository->get($cartId);
66+
} catch (NoSuchEntityException $e) {
67+
throw new GraphQlNoSuchEntityException(
68+
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
69+
);
70+
}
71+
72+
$customerId = (int)$cart->getCustomerId();
73+
74+
/* Guest cart, allow operations */
75+
if (!$customerId) {
76+
return $cart;
77+
}
78+
79+
if ($customerId !== $userId) {
80+
throw new GraphQlAuthenticationException(
81+
__(
82+
'The current user cannot perform operations on cart "%masked_cart_id"',
83+
['masked_cart_id' => $cartHash]
84+
)
85+
);
86+
}
87+
return $cart;
88+
}
89+
}

app/code/Magento/QuoteGraphQl/Model/Resolver/AddSimpleProductsToCart.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Framework\Stdlib\ArrayManager;
1515
use Magento\QuoteGraphQl\Model\Cart\AddProductsToCart;
1616
use Magento\QuoteGraphQl\Model\Cart\ExtractDataFromCart;
17+
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
1718

1819
/**
1920
* Add simple products to cart GraphQl resolver
@@ -26,6 +27,11 @@ class AddSimpleProductsToCart implements ResolverInterface
2627
*/
2728
private $arrayManager;
2829

30+
/**
31+
* @var GetCartForUser
32+
*/
33+
private $getCartForUser;
34+
2935
/**
3036
* @var AddProductsToCart
3137
*/
@@ -38,15 +44,18 @@ class AddSimpleProductsToCart implements ResolverInterface
3844

3945
/**
4046
* @param ArrayManager $arrayManager
47+
* @param GetCartForUser $getCartForUser
4148
* @param AddProductsToCart $addProductsToCart
4249
* @param ExtractDataFromCart $extractDataFromCart
4350
*/
4451
public function __construct(
4552
ArrayManager $arrayManager,
53+
GetCartForUser $getCartForUser,
4654
AddProductsToCart $addProductsToCart,
4755
ExtractDataFromCart $extractDataFromCart
4856
) {
4957
$this->arrayManager = $arrayManager;
58+
$this->getCartForUser = $getCartForUser;
5059
$this->addProductsToCart = $addProductsToCart;
5160
$this->extractDataFromCart = $extractDataFromCart;
5261
}
@@ -67,7 +76,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6776
throw new GraphQlInputException(__('Missing key "cartItems" in cart data'));
6877
}
6978

70-
$cart = $this->addProductsToCart->execute((string)$cartHash, $cartItems);
79+
$currentUserId = $context->getUserId();
80+
$cart = $this->getCartForUser->execute((string)$cartHash, $currentUserId);
81+
82+
$this->addProductsToCart->execute($cart, $cartItems);
7183
$cartData = $this->extractDataFromCart->execute($cart);
7284

7385
return [

0 commit comments

Comments
 (0)