Skip to content

Commit a3d0985

Browse files
authored
ENGCOM-4464: GraphQL-458: CustomerGraphQl module refactoring #459
2 parents 8718692 + 35f2e95 commit a3d0985

40 files changed

+776
-728
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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\CustomerGraphQl\Model\Customer\Address;
9+
10+
use Magento\Customer\Api\AddressRepositoryInterface;
11+
use Magento\Customer\Api\Data\AddressInterface;
12+
use Magento\Customer\Api\Data\AddressInterfaceFactory;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
15+
use Magento\Framework\Api\DataObjectHelper;
16+
17+
/**
18+
* Create customer address
19+
*/
20+
class CreateCustomerAddress
21+
{
22+
/**
23+
* @var GetAllowedAddressAttributes
24+
*/
25+
private $getAllowedAddressAttributes;
26+
27+
/**
28+
* @var AddressInterfaceFactory
29+
*/
30+
private $addressFactory;
31+
32+
/**
33+
* @var AddressRepositoryInterface
34+
*/
35+
private $addressRepository;
36+
37+
/**
38+
* @var DataObjectHelper
39+
*/
40+
private $dataObjectHelper;
41+
42+
/**
43+
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
44+
* @param AddressInterfaceFactory $addressFactory
45+
* @param AddressRepositoryInterface $addressRepository
46+
* @param DataObjectHelper $dataObjectHelper
47+
*/
48+
public function __construct(
49+
GetAllowedAddressAttributes $getAllowedAddressAttributes,
50+
AddressInterfaceFactory $addressFactory,
51+
AddressRepositoryInterface $addressRepository,
52+
DataObjectHelper $dataObjectHelper
53+
) {
54+
$this->getAllowedAddressAttributes = $getAllowedAddressAttributes;
55+
$this->addressFactory = $addressFactory;
56+
$this->addressRepository = $addressRepository;
57+
$this->dataObjectHelper = $dataObjectHelper;
58+
}
59+
60+
/**
61+
* Create customer address
62+
*
63+
* @param int $customerId
64+
* @param array $data
65+
* @return AddressInterface
66+
* @throws GraphQlInputException
67+
*/
68+
public function execute(int $customerId, array $data): AddressInterface
69+
{
70+
$this->validateData($data);
71+
72+
/** @var AddressInterface $address */
73+
$address = $this->addressFactory->create();
74+
$this->dataObjectHelper->populateWithArray($address, $data, AddressInterface::class);
75+
76+
if (isset($data['region']['region_id'])) {
77+
$address->setRegionId($address->getRegion()->getRegionId());
78+
}
79+
$address->setCustomerId($customerId);
80+
81+
try {
82+
$this->addressRepository->save($address);
83+
} catch (LocalizedException $e) {
84+
throw new GraphQlInputException(__($e->getMessage()), $e);
85+
}
86+
return $address;
87+
}
88+
89+
/**
90+
* Validate customer address create data
91+
*
92+
* @param array $addressData
93+
* @return void
94+
* @throws GraphQlInputException
95+
*/
96+
public function validateData(array $addressData): void
97+
{
98+
$attributes = $this->getAllowedAddressAttributes->execute();
99+
$errorInput = [];
100+
101+
foreach ($attributes as $attributeName => $attributeInfo) {
102+
if ($attributeInfo->getIsRequired()
103+
&& (!isset($addressData[$attributeName]) || empty($addressData[$attributeName]))
104+
) {
105+
$errorInput[] = $attributeName;
106+
}
107+
}
108+
109+
if ($errorInput) {
110+
throw new GraphQlInputException(
111+
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
112+
);
113+
}
114+
}
115+
}

app/code/Magento/CustomerGraphQl/Model/Customer/Address/CustomerAddressCreateDataValidator.php

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

app/code/Magento/CustomerGraphQl/Model/Customer/Address/CustomerAddressUpdateDataValidator.php

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\CustomerGraphQl\Model\Customer\Address;
9+
10+
use Magento\Customer\Api\AddressRepositoryInterface;
11+
use Magento\Customer\Api\Data\AddressInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
15+
/**
16+
* Delete customer address
17+
*/
18+
class DeleteCustomerAddress
19+
{
20+
/**
21+
* @var AddressRepositoryInterface
22+
*/
23+
private $addressRepository;
24+
25+
/**
26+
* @param AddressRepositoryInterface $addressRepository
27+
*/
28+
public function __construct(
29+
AddressRepositoryInterface $addressRepository
30+
) {
31+
$this->addressRepository = $addressRepository;
32+
}
33+
34+
/**
35+
* Delete customer address
36+
*
37+
* @param AddressInterface $address
38+
* @return void
39+
* @throws GraphQlInputException
40+
*/
41+
public function execute(AddressInterface $address): void
42+
{
43+
if ($address->isDefaultBilling()) {
44+
throw new GraphQlInputException(
45+
__('Customer Address %1 is set as default billing address and can not be deleted', [$address->getId()])
46+
);
47+
}
48+
if ($address->isDefaultShipping()) {
49+
throw new GraphQlInputException(
50+
__('Customer Address %1 is set as default shipping address and can not be deleted', [$address->getId()])
51+
);
52+
}
53+
54+
try {
55+
$this->addressRepository->delete($address);
56+
} catch (LocalizedException $e) {
57+
throw new GraphQlInputException(__($e->getMessage()), $e);
58+
}
59+
}
60+
}

app/code/Magento/CustomerGraphQl/Model/Customer/Address/CustomerAddressDataProvider.php renamed to app/code/Magento/CustomerGraphQl/Model/Customer/Address/ExtractCustomerAddressData.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
use Magento\Framework\Serialize\SerializerInterface;
1818

1919
/**
20-
* Customer Address field data provider, used for GraphQL request processing.
20+
* Transform single customer address data from object to in array format
2121
*/
22-
class CustomerAddressDataProvider
22+
class ExtractCustomerAddressData
2323
{
2424
/**
2525
* @var ServiceOutputProcessor
@@ -82,24 +82,27 @@ private function curateAddressDefaultValues(array $address, AddressInterface $ad
8282
/**
8383
* Transform single customer address data from object to in array format
8484
*
85-
* @param AddressInterface $addressObject
85+
* @param AddressInterface $address
8686
* @return array
8787
*/
88-
public function getAddressData(AddressInterface $addressObject): array
88+
public function execute(AddressInterface $address): array
8989
{
90-
$address = $this->serviceOutputProcessor->process(
91-
$addressObject,
90+
$addressData = $this->serviceOutputProcessor->process(
91+
$address,
9292
AddressRepositoryInterface::class,
9393
'getById'
9494
);
95-
$address = $this->curateAddressDefaultValues($address, $addressObject);
95+
$addressData = $this->curateAddressDefaultValues($addressData, $address);
9696

97-
if (isset($address[CustomAttributesDataInterface::EXTENSION_ATTRIBUTES_KEY])) {
98-
$address = array_merge($address, $address[CustomAttributesDataInterface::EXTENSION_ATTRIBUTES_KEY]);
97+
if (isset($addressData[CustomAttributesDataInterface::EXTENSION_ATTRIBUTES_KEY])) {
98+
$addressData = array_merge(
99+
$addressData,
100+
$addressData[CustomAttributesDataInterface::EXTENSION_ATTRIBUTES_KEY]
101+
);
99102
}
100103
$customAttributes = [];
101-
if (isset($address[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])) {
102-
foreach ($address[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES] as $attribute) {
104+
if (isset($addressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])) {
105+
foreach ($addressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES] as $attribute) {
103106
$isArray = false;
104107
if (is_array($attribute['value'])) {
105108
$isArray = true;
@@ -120,8 +123,8 @@ public function getAddressData(AddressInterface $addressObject): array
120123
$customAttributes[$attribute['attribute_code']] = $attribute['value'];
121124
}
122125
}
123-
$address = array_merge($address, $customAttributes);
126+
$addressData = array_merge($addressData, $customAttributes);
124127

125-
return $address;
128+
return $addressData;
126129
}
127130
}

app/code/Magento/QuoteGraphQl/Model/Cart/GetCustomerAddress.php renamed to app/code/Magento/CustomerGraphQl/Model/Customer/Address/GetCustomerAddress.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\QuoteGraphQl\Model\Cart;
8+
namespace Magento\CustomerGraphQl\Model\Customer\Address;
99

1010
use Magento\Customer\Api\AddressRepositoryInterface;
1111
use Magento\Customer\Api\Data\AddressInterface;
@@ -58,7 +58,7 @@ public function execute(int $addressId, int $customerId): AddressInterface
5858
if ((int)$customerAddress->getCustomerId() !== $customerId) {
5959
throw new GraphQlAuthorizationException(
6060
__(
61-
'The current user cannot use address with ID "%address_id"',
61+
'Current customer does not have permission to address with ID "%address_id"',
6262
['address_id' => $addressId]
6363
)
6464
);

0 commit comments

Comments
 (0)