Skip to content

Commit b0ab652

Browse files
author
Valeriy Nayda
committed
GraphQL-57: Manage Address Book
-- Refactoring
1 parent ec4c985 commit b0ab652

19 files changed

+657
-576
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Framework\GraphQl\Exception\GraphQlInputException;
11+
12+
/**
13+
* Customer address create data validator
14+
*/
15+
class CustomerAddressCreateDataValidator
16+
{
17+
/**
18+
* @var GetAllowedAddressAttributes
19+
*/
20+
private $getAllowedAddressAttributes;
21+
22+
/**
23+
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
24+
*/
25+
public function __construct(GetAllowedAddressAttributes $getAllowedAddressAttributes)
26+
{
27+
$this->getAllowedAddressAttributes = $getAllowedAddressAttributes;
28+
}
29+
30+
/**
31+
* Validate customer address create data
32+
*
33+
* @param array $addressData
34+
* @return void
35+
* @throws GraphQlInputException
36+
*/
37+
public function validate(array $addressData): void
38+
{
39+
$attributes = $this->getAllowedAddressAttributes->execute();
40+
$errorInput = [];
41+
42+
foreach ($attributes as $attributeName => $attributeInfo) {
43+
if ($attributeInfo->getIsRequired()
44+
&& (!isset($addressData[$attributeName]) || empty($addressData[$attributeName]))
45+
) {
46+
$errorInput[] = $attributeName;
47+
}
48+
}
49+
50+
if ($errorInput) {
51+
throw new GraphQlInputException(
52+
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
53+
);
54+
}
55+
}
56+
}

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

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

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

1010
use Magento\Customer\Api\Data\AddressInterface;
1111
use Magento\Customer\Api\Data\CustomerInterface;
@@ -19,7 +19,7 @@
1919
/**
2020
* Customer Address field data provider, used for GraphQL request processing.
2121
*/
22-
class AddressDataProvider
22+
class CustomerAddressDataProvider
2323
{
2424
/**
2525
* @var ServiceOutputProcessor
@@ -72,10 +72,10 @@ private function curateAddressDefaultValues(array $address, AddressInterface $ad
7272
$this->customerResourceModel->load($customerModel, $addressObject->getCustomerId());
7373
$address[CustomerInterface::DEFAULT_BILLING] =
7474
($customerModel->getDefaultBillingAddress()
75-
&& $addressObject->getId() == $customerModel->getDefaultBillingAddress()->getId()) ? true : false;
75+
&& $addressObject->getId() == $customerModel->getDefaultBillingAddress()->getId());
7676
$address[CustomerInterface::DEFAULT_SHIPPING] =
7777
($customerModel->getDefaultShippingAddress()
78-
&& $addressObject->getId() == $customerModel->getDefaultShippingAddress()->getId()) ? true : false;
78+
&& $addressObject->getId() == $customerModel->getDefaultShippingAddress()->getId());
7979
return $address;
8080
}
8181

@@ -85,7 +85,7 @@ private function curateAddressDefaultValues(array $address, AddressInterface $ad
8585
* @param AddressInterface $addressObject
8686
* @return array
8787
*/
88-
public function processCustomerAddress(AddressInterface $addressObject) : array
88+
public function getAddressData(AddressInterface $addressObject): array
8989
{
9090
$address = $this->serviceOutputProcessor->process(
9191
$addressObject,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Framework\GraphQl\Exception\GraphQlInputException;
11+
12+
/**
13+
* Customer address update data validator. Patch update is allowed
14+
*/
15+
class CustomerAddressUpdateDataValidator
16+
{
17+
/**
18+
* @var GetAllowedAddressAttributes
19+
*/
20+
private $getAllowedAddressAttributes;
21+
22+
/**
23+
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
24+
*/
25+
public function __construct(GetAllowedAddressAttributes $getAllowedAddressAttributes)
26+
{
27+
$this->getAllowedAddressAttributes = $getAllowedAddressAttributes;
28+
}
29+
30+
/**
31+
* Validate customer address update data
32+
*
33+
* @param array $addressData
34+
* @return void
35+
* @throws GraphQlInputException
36+
*/
37+
public function validate(array $addressData): void
38+
{
39+
$attributes = $this->getAllowedAddressAttributes->execute();
40+
$errorInput = [];
41+
42+
foreach ($attributes as $attributeName => $attributeInfo) {
43+
if ($attributeInfo->getIsRequired()
44+
&& (isset($addressData[$attributeName]) && empty($addressData[$attributeName]))
45+
) {
46+
$errorInput[] = $attributeName;
47+
}
48+
}
49+
50+
if ($errorInput) {
51+
throw new GraphQlInputException(
52+
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
53+
);
54+
}
55+
}
56+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\AddressMetadataManagementInterface;
11+
use Magento\Eav\Model\Config;
12+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
13+
14+
/**
15+
* Get allowed address attributes
16+
*/
17+
class GetAllowedAddressAttributes
18+
{
19+
/**
20+
* @var Config
21+
*/
22+
private $eavConfig;
23+
24+
/**
25+
* @param Config $eavConfig
26+
*/
27+
public function __construct(Config $eavConfig)
28+
{
29+
$this->eavConfig = $eavConfig;
30+
}
31+
32+
/**
33+
* Get allowed address attributes
34+
*
35+
* @return AbstractAttribute[]
36+
*/
37+
public function execute(): array
38+
{
39+
$attributes = $this->eavConfig->getEntityAttributes(
40+
AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS
41+
);
42+
foreach ($attributes as $attributeCode => $attribute) {
43+
if (false === $attribute->getIsVisibleOnFront()) {
44+
unset($attributes[$attributeCode]);
45+
}
46+
}
47+
return $attributes;
48+
}
49+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\NoSuchEntityException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
15+
16+
/**
17+
* Get customer address for user
18+
*/
19+
class GetCustomerAddressForUser
20+
{
21+
/**
22+
* @var AddressRepositoryInterface
23+
*/
24+
private $addressRepository;
25+
26+
/**
27+
* @param AddressRepositoryInterface $addressRepository
28+
*/
29+
public function __construct(AddressRepositoryInterface $addressRepository)
30+
{
31+
$this->addressRepository = $addressRepository;
32+
}
33+
34+
/**
35+
* Get customer address for user
36+
*
37+
* @param int $addressId
38+
* @param int $userId
39+
* @return AddressInterface
40+
* @throws GraphQlAuthorizationException
41+
* @throws GraphQlNoSuchEntityException
42+
*/
43+
public function execute(int $addressId, int $userId): AddressInterface
44+
{
45+
try {
46+
/** @var AddressInterface $address */
47+
$address = $this->addressRepository->getById($addressId);
48+
} catch (NoSuchEntityException $e) {
49+
throw new GraphQlNoSuchEntityException(
50+
__('Address id %1 does not exist.', [$addressId])
51+
);
52+
}
53+
54+
if ($address->getCustomerId() != $userId) {
55+
throw new GraphQlAuthorizationException(
56+
__('Current customer does not have permission to address id %1', [$addressId])
57+
);
58+
}
59+
return $address;
60+
}
61+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private function processCustomer(CustomerInterface $customer): array
102102
CustomerRepositoryInterface::class,
103103
'get'
104104
);
105-
$customerData['addresses'] = $this->curateAddressData($customer['addresses']);
105+
$customerData['addresses'] = $this->curateAddressData($customerData['addresses']);
106106
if (isset($customerData['extension_attributes'])) {
107107
$customerData = array_merge($customerData, $customerData['extension_attributes']);
108108
}

app/code/Magento/CustomerGraphQl/Model/Customer/UpdateAccountInformation.php renamed to app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerData.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
use Magento\Store\Model\StoreManagerInterface;
1616

1717
/**
18-
* Update account information
18+
* Update customer data
1919
*/
20-
class UpdateAccountInformation
20+
class UpdateCustomerData
2121
{
2222
/**
2323
* @var CustomerRepositoryInterface

0 commit comments

Comments
 (0)