Skip to content

Commit 7c17dbb

Browse files
authored
ENGCOM-4393: [Cart Operations] Remove item mutation #373
2 parents a5330a7 + ef8d42f commit 7c17dbb

31 files changed

+879
-287
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
use Magento\Customer\Api\AccountManagementInterface;
1212
use Magento\Customer\Api\CustomerRepositoryInterface;
1313
use Magento\Customer\Model\AuthenticationInterface;
14+
use Magento\Framework\Exception\LocalizedException;
1415
use Magento\Framework\Exception\NoSuchEntityException;
1516
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
1617
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
18+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1719
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1820

1921
/**
@@ -58,6 +60,7 @@ public function __construct(
5860
* @param int|null $customerType
5961
* @return void
6062
* @throws GraphQlAuthorizationException
63+
* @throws GraphQlInputException
6164
* @throws GraphQlNoSuchEntityException
6265
* @throws GraphQlAuthenticationException
6366
*/
@@ -74,13 +77,20 @@ public function execute(?int $customerId, ?int $customerType): void
7477
__('Customer with id "%customer_id" does not exist.', ['customer_id' => $customerId]),
7578
$e
7679
);
80+
} catch (LocalizedException $e) {
81+
throw new GraphQlInputException(__($e->getMessage()));
7782
}
7883

7984
if (true === $this->authentication->isLocked($customerId)) {
8085
throw new GraphQlAuthenticationException(__('The account is locked.'));
8186
}
8287

83-
$confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId);
88+
try {
89+
$confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId);
90+
} catch (LocalizedException $e) {
91+
throw new GraphQlInputException(__($e->getMessage()));
92+
}
93+
8494
if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) {
8595
throw new GraphQlAuthenticationException(__("This account isn't confirmed. Verify and try again."));
8696
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\LocalizedException;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
14+
use Magento\Quote\Api\Data\CartInterface;
15+
use Magento\Quote\Api\BillingAddressManagementInterface;
16+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
17+
18+
/**
19+
* Set billing address for a specified shopping cart
20+
*/
21+
class AssignBillingAddressToCart
22+
{
23+
/**
24+
* @var BillingAddressManagementInterface
25+
*/
26+
private $billingAddressManagement;
27+
28+
/**
29+
* @param BillingAddressManagementInterface $billingAddressManagement
30+
*/
31+
public function __construct(
32+
BillingAddressManagementInterface $billingAddressManagement
33+
) {
34+
$this->billingAddressManagement = $billingAddressManagement;
35+
}
36+
37+
/**
38+
* Assign billing address to cart
39+
*
40+
* @param CartInterface $cart
41+
* @param QuoteAddress $billingAddress
42+
* @param bool $useForShipping
43+
* @throws GraphQlInputException
44+
* @throws GraphQlNoSuchEntityException
45+
*/
46+
public function execute(
47+
CartInterface $cart,
48+
QuoteAddress $billingAddress,
49+
bool $useForShipping
50+
): void {
51+
try {
52+
$this->billingAddressManagement->assign($cart->getId(), $billingAddress, $useForShipping);
53+
} catch (NoSuchEntityException $e) {
54+
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
55+
} catch (LocalizedException $e) {
56+
throw new GraphQlInputException(__($e->getMessage()));
57+
}
58+
}
59+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\LocalizedException;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
14+
use Magento\Quote\Api\Data\CartInterface;
15+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
16+
use Magento\Quote\Model\ShippingAddressManagementInterface;
17+
18+
/**
19+
* Assign shipping address to cart
20+
*/
21+
class AssignShippingAddressToCart
22+
{
23+
/**
24+
* @var ShippingAddressManagementInterface
25+
*/
26+
private $shippingAddressManagement;
27+
28+
/**
29+
* @param ShippingAddressManagementInterface $shippingAddressManagement
30+
*/
31+
public function __construct(
32+
ShippingAddressManagementInterface $shippingAddressManagement
33+
) {
34+
$this->shippingAddressManagement = $shippingAddressManagement;
35+
}
36+
37+
/**
38+
* Assign shipping address to cart
39+
*
40+
* @param CartInterface $cart
41+
* @param QuoteAddress $shippingAddress
42+
* @throws GraphQlInputException
43+
* @throws GraphQlNoSuchEntityException
44+
*/
45+
public function execute(
46+
CartInterface $cart,
47+
QuoteAddress $shippingAddress
48+
): void {
49+
try {
50+
$this->shippingAddressManagement->assign($cart->getId(), $shippingAddress);
51+
} catch (NoSuchEntityException $e) {
52+
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
53+
} catch (LocalizedException $e) {
54+
throw new GraphQlInputException(__($e->getMessage()));
55+
}
56+
}
57+
}

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

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

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Exception\LocalizedException;
1313
use Magento\Framework\Exception\NoSuchEntityException;
1414
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
15+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1516
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1617

1718
/**
@@ -33,14 +34,14 @@ public function __construct(AddressRepositoryInterface $addressRepository)
3334
}
3435

3536
/**
36-
* Get customer address. Throws exception if customer is not owner of address
37+
* Get customer address
3738
*
3839
* @param int $addressId
3940
* @param int $customerId
4041
* @return AddressInterface
41-
* @throws GraphQlAuthorizationException
42+
* @throws GraphQlInputException
4243
* @throws GraphQlNoSuchEntityException
43-
* @throws LocalizedException
44+
* @throws GraphQlAuthorizationException
4445
*/
4546
public function execute(int $addressId, int $customerId): AddressInterface
4647
{
@@ -50,6 +51,8 @@ public function execute(int $addressId, int $customerId): AddressInterface
5051
throw new GraphQlNoSuchEntityException(
5152
__('Could not find a address with ID "%address_id"', ['address_id' => $addressId])
5253
);
54+
} catch (LocalizedException $e) {
55+
throw new GraphQlInputException(__($e->getMessage()));
5356
}
5457

5558
if ((int)$customerAddress->getCustomerId() !== $customerId) {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\Customer\Api\Data\AddressInterface as CustomerAddress;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
14+
use Magento\Quote\Model\Quote\AddressFactory as BaseQuoteAddressFactory;
15+
16+
/**
17+
* Create QuoteAddress
18+
*/
19+
class QuoteAddressFactory
20+
{
21+
/**
22+
* @var BaseQuoteAddressFactory
23+
*/
24+
private $quoteAddressFactory;
25+
26+
/**
27+
* @param BaseQuoteAddressFactory $quoteAddressFactory
28+
*/
29+
public function __construct(
30+
BaseQuoteAddressFactory $quoteAddressFactory
31+
) {
32+
$this->quoteAddressFactory = $quoteAddressFactory;
33+
}
34+
35+
/**
36+
* Create QuoteAddress based on input data
37+
*
38+
* @param array $addressInput
39+
* @return QuoteAddress
40+
*/
41+
public function createBasedOnInputData(array $addressInput): QuoteAddress
42+
{
43+
$addressInput['country_id'] = $addressInput['country_code'] ?? '';
44+
45+
$quoteAddress = $this->quoteAddressFactory->create();
46+
$quoteAddress->addData($addressInput);
47+
return $quoteAddress;
48+
}
49+
50+
/**
51+
* Create QuoteAddress based on CustomerAddress
52+
*
53+
* @param CustomerAddress $customerAddress
54+
* @return QuoteAddress
55+
* @throws GraphQlInputException
56+
*/
57+
public function createBasedOnCustomerAddress(CustomerAddress $customerAddress): QuoteAddress
58+
{
59+
$quoteAddress = $this->quoteAddressFactory->create();
60+
try {
61+
$quoteAddress->importCustomerAddressData($customerAddress);
62+
} catch (LocalizedException $e) {
63+
throw new GraphQlInputException(__($e->getMessage()));
64+
}
65+
return $quoteAddress;
66+
}
67+
}

0 commit comments

Comments
 (0)