Skip to content

Commit b4dc296

Browse files
authored
LYNX-123: Add custom_attributes to CartAddressInput type
1 parent b3d7b83 commit b4dc296

File tree

14 files changed

+495
-53
lines changed

14 files changed

+495
-53
lines changed

app/code/Magento/Customer/Test/Fixture/CustomerAttribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class CustomerAttribute implements RevertibleDataFixtureInterface
3636
'is_unique' => '0',
3737
'frontend_class' => null,
3838
'used_in_forms' => [],
39-
'sort_order' => null,
39+
'sort_order' => 0,
4040
'attribute_set_id' => null,
4141
'attribute_group_id' => null,
4242
'input_filter' => null,

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

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
namespace Magento\CustomerGraphQl\Model\Customer\Address;
99

1010
use Magento\Customer\Api\AddressMetadataInterface;
11+
use Magento\Customer\Api\AddressRepositoryInterface;
1112
use Magento\Customer\Api\Data\AddressInterface;
1213
use Magento\Customer\Api\Data\CustomerInterface;
13-
use Magento\EavGraphQl\Model\GetAttributeValueComposite;
14-
use Magento\Framework\Api\CustomAttributesDataInterface;
15-
use Magento\Customer\Api\AddressRepositoryInterface;
16-
use Magento\Customer\Model\ResourceModel\Customer as CustomerResourceModel;
1714
use Magento\Customer\Model\CustomerFactory;
18-
use Magento\Framework\Webapi\ServiceOutputProcessor;
15+
use Magento\Customer\Model\ResourceModel\Customer as CustomerResourceModel;
16+
use Magento\EavGraphQl\Model\Output\Value\GetAttributeValueInterface;
17+
use Magento\Framework\Api\CustomAttributesDataInterface;
1918
use Magento\Framework\Serialize\SerializerInterface;
19+
use Magento\Framework\Webapi\ServiceOutputProcessor;
2020

2121
/**
2222
* Transform single customer address data from object to in array format
@@ -44,29 +44,29 @@ class ExtractCustomerAddressData
4444
private $customerFactory;
4545

4646
/**
47-
* @var GetAttributeValueComposite
47+
* @var GetAttributeValueInterface
4848
*/
49-
private GetAttributeValueComposite $getAttributeValueComposite;
49+
private GetAttributeValueInterface $getAttributeValue;
5050

5151
/**
5252
* @param ServiceOutputProcessor $serviceOutputProcessor
5353
* @param SerializerInterface $jsonSerializer
5454
* @param CustomerResourceModel $customerResourceModel
5555
* @param CustomerFactory $customerFactory
56-
* @param GetAttributeValueComposite $getAttributeValueComposite
56+
* @param GetAttributeValueInterface $getAttributeValue
5757
*/
5858
public function __construct(
5959
ServiceOutputProcessor $serviceOutputProcessor,
6060
SerializerInterface $jsonSerializer,
6161
CustomerResourceModel $customerResourceModel,
6262
CustomerFactory $customerFactory,
63-
GetAttributeValueComposite $getAttributeValueComposite
63+
GetAttributeValueInterface $getAttributeValue
6464
) {
6565
$this->serviceOutputProcessor = $serviceOutputProcessor;
6666
$this->jsonSerializer = $jsonSerializer;
6767
$this->customerResourceModel = $customerResourceModel;
6868
$this->customerFactory = $customerFactory;
69-
$this->getAttributeValueComposite = $getAttributeValueComposite;
69+
$this->getAttributeValue = $getAttributeValue;
7070
}
7171

7272
/**
@@ -110,44 +110,11 @@ public function execute(AddressInterface $address): array
110110
$addressData[CustomAttributesDataInterface::EXTENSION_ATTRIBUTES_KEY]
111111
);
112112
}
113-
$customAttributes = [];
114-
if (isset($addressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])) {
115-
foreach ($addressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES] as $attribute) {
116-
$isArray = false;
117-
if (is_array($attribute['value'])) {
118-
// @ignoreCoverageStart
119-
$isArray = true;
120-
foreach ($attribute['value'] as $attributeValue) {
121-
if (is_array($attributeValue)) {
122-
$customAttributes[$attribute['attribute_code']] = $this->jsonSerializer->serialize(
123-
$attribute['value']
124-
);
125-
continue;
126-
}
127-
$customAttributes[$attribute['attribute_code']] = implode(',', $attribute['value']);
128-
continue;
129-
}
130-
// @ignoreCoverageEnd
131-
}
132-
if ($isArray) {
133-
continue;
134-
}
135-
$customAttributes[$attribute['attribute_code']] = $attribute['value'];
136-
}
137113

138-
$customAttributesV2 = array_map(
139-
function (array $customAttribute) {
140-
return $this->getAttributeValueComposite->execute(
141-
AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
142-
$customAttribute
143-
);
144-
},
145-
$addressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES]
146-
);
147-
$customAttributes['custom_attributesV2'] = $customAttributesV2;
148-
} else {
149-
$customAttributes['custom_attributesV2'] = [];
150-
}
114+
$customAttributes = isset($addressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])
115+
? $this->formatCustomAttributes($addressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])
116+
: ['custom_attributesV2' => []];
117+
151118
$addressData = array_merge($addressData, $customAttributes);
152119

153120
$addressData['customer_id'] = null;
@@ -158,4 +125,49 @@ function (array $customAttribute) {
158125

159126
return $addressData;
160127
}
128+
129+
/**
130+
* Retrieve formatted custom attributes
131+
*
132+
* @param array $attributes
133+
* @return array
134+
*/
135+
private function formatCustomAttributes(array $attributes)
136+
{
137+
foreach ($attributes as $attribute) {
138+
$isArray = false;
139+
if (is_array($attribute['value'])) {
140+
// @ignoreCoverageStart
141+
$isArray = true;
142+
foreach ($attribute['value'] as $attributeValue) {
143+
if (is_array($attributeValue)) {
144+
$customAttributes[$attribute['attribute_code']] = $this->jsonSerializer->serialize(
145+
$attribute['value']
146+
);
147+
continue;
148+
}
149+
$customAttributes[$attribute['attribute_code']] = implode(',', $attribute['value']);
150+
continue;
151+
}
152+
// @ignoreCoverageEnd
153+
}
154+
if ($isArray) {
155+
continue;
156+
}
157+
$customAttributes[$attribute['attribute_code']] = $attribute['value'];
158+
}
159+
160+
$customAttributes['custom_attributesV2'] = array_map(
161+
function (array $customAttribute) {
162+
return $this->getAttributeValue->execute(
163+
AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
164+
$customAttribute['attribute_code'],
165+
$customAttribute['value']
166+
);
167+
},
168+
$attributes
169+
);
170+
171+
return $customAttributes;
172+
}
161173
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\EavGraphQl\Model\Output\Value;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Exception\RuntimeException;
12+
13+
/**
14+
* Format attribute values provider for GraphQL output
15+
*/
16+
class GetAttributeValueComposite implements GetAttributeValueInterface
17+
{
18+
/**
19+
* @var GetAttributeValueInterface[]
20+
*/
21+
private array $providers;
22+
23+
/**
24+
* @param array $providers
25+
*/
26+
public function __construct(array $providers = [])
27+
{
28+
$this->providers = $providers;
29+
}
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
public function execute(string $entity, string $code, string $value): ?array
35+
{
36+
foreach ($this->providers as $provider) {
37+
if (!$provider instanceof GetAttributeValueInterface) {
38+
throw new RuntimeException(
39+
__('Configured attribute data providers should implement GetAttributeValueInterface')
40+
);
41+
}
42+
43+
try {
44+
return $provider->execute($entity, $code, $value);
45+
} catch (LocalizedException $e) {
46+
continue;
47+
}
48+
}
49+
return null;
50+
}
51+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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\EavGraphQl\Model\Output\Value;
9+
10+
/**
11+
* Interface for getting custom attributes.
12+
*/
13+
interface GetAttributeValueInterface
14+
{
15+
/**
16+
* Retrieve all attributes filtered by attribute code
17+
*
18+
* @param string $entity
19+
* @param string $code
20+
* @param string $value
21+
* @return array|null
22+
*/
23+
public function execute(string $entity, string $code, string $value): ?array;
24+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\EavGraphQl\Model\Output\Value;
9+
10+
use Magento\Eav\Model\AttributeRepository;
11+
use Magento\EavGraphQl\Model\Output\Value\Options\GetAttributeSelectedOptionInterface;
12+
use Magento\EavGraphQl\Model\Uid;
13+
14+
/**
15+
* Custom attribute value provider for customer
16+
*/
17+
class GetCustomAttributes implements GetAttributeValueInterface
18+
{
19+
/**
20+
* @var Uid
21+
*/
22+
private Uid $uid;
23+
24+
/**
25+
* @var AttributeRepository
26+
*/
27+
private AttributeRepository $attributeRepository;
28+
29+
/**
30+
* @var GetAttributeSelectedOptionInterface
31+
*/
32+
private GetAttributeSelectedOptionInterface $getAttributeSelectedOption;
33+
34+
/**
35+
* @var array
36+
*/
37+
private array $frontendInputs;
38+
39+
/**
40+
* @param Uid $uid
41+
* @param AttributeRepository $attributeRepository
42+
* @param GetAttributeSelectedOptionInterface $getAttributeSelectedOption
43+
* @param array $frontendInputs
44+
*/
45+
public function __construct(
46+
Uid $uid,
47+
AttributeRepository $attributeRepository,
48+
GetAttributeSelectedOptionInterface $getAttributeSelectedOption,
49+
array $frontendInputs = []
50+
) {
51+
$this->uid = $uid;
52+
$this->attributeRepository = $attributeRepository;
53+
$this->frontendInputs = $frontendInputs;
54+
$this->getAttributeSelectedOption = $getAttributeSelectedOption;
55+
}
56+
57+
/**
58+
* @inheritDoc
59+
*/
60+
public function execute(string $entity, string $code, string $value): ?array
61+
{
62+
$attr = $this->attributeRepository->get($entity, $code);
63+
64+
$result = [
65+
'uid' => $this->uid->encode($entity, $code),
66+
'code' => $code
67+
];
68+
69+
if (in_array($attr->getFrontendInput(), $this->frontendInputs)) {
70+
$result['selected_options'] = $this->getAttributeSelectedOption->execute($entity, $code, $value);
71+
} else {
72+
$result['value'] = $value;
73+
}
74+
return $result;
75+
}
76+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\EavGraphQl\Model\Output\Value\Options;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Exception\RuntimeException;
12+
13+
/**
14+
* Format selected options values provider for GraphQL output
15+
*/
16+
class GetAttributeSelectedOptionComposite implements GetAttributeSelectedOptionInterface
17+
{
18+
/**
19+
* @var GetAttributeSelectedOptionInterface[]
20+
*/
21+
private array $providers;
22+
23+
/**
24+
* @param array $providers
25+
*/
26+
public function __construct(array $providers = [])
27+
{
28+
$this->providers = $providers;
29+
}
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
public function execute(string $entity, string $code, string $value): ?array
35+
{
36+
foreach ($this->providers as $provider) {
37+
if (!$provider instanceof GetAttributeSelectedOptionInterface) {
38+
throw new RuntimeException(
39+
__('Configured attribute selected option data providers should implement
40+
GetAttributeSelectedOptionInterface')
41+
);
42+
}
43+
44+
try {
45+
return $provider->execute($entity, $code, $value);
46+
} catch (LocalizedException $e) {
47+
continue;
48+
}
49+
}
50+
return null;
51+
}
52+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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\EavGraphQl\Model\Output\Value\Options;
9+
10+
/**
11+
* Interface for getting custom attributes seelcted options.
12+
*/
13+
interface GetAttributeSelectedOptionInterface
14+
{
15+
/**
16+
* Retrieve all selected options of an attribute filtered by attribute code
17+
*
18+
* @param string $entity
19+
* @param string $code
20+
* @param string $value
21+
* @return array|null
22+
*/
23+
public function execute(string $entity, string $code, string $value): ?array;
24+
}

0 commit comments

Comments
 (0)