Skip to content

Commit

Permalink
LYNX-111: Customer address custom_attributesV2 field
Browse files Browse the repository at this point in the history
  • Loading branch information
svera authored Apr 27, 2023
1 parent e18edcc commit 6a027c6
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

namespace Magento\CustomerGraphQl\Model\Customer\Address;

use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\EavGraphQl\Model\GetAttributeValueComposite;
use Magento\Framework\Api\CustomAttributesDataInterface;
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Model\ResourceModel\Customer as CustomerResourceModel;
Expand Down Expand Up @@ -41,22 +43,30 @@ class ExtractCustomerAddressData
*/
private $customerFactory;

/**
* @var GetAttributeValueComposite
*/
private GetAttributeValueComposite $getAttributeValueComposite;

/**
* @param ServiceOutputProcessor $serviceOutputProcessor
* @param SerializerInterface $jsonSerializer
* @param CustomerResourceModel $customerResourceModel
* @param CustomerFactory $customerFactory
* @param GetAttributeValueComposite $getAttributeValueComposite
*/
public function __construct(
ServiceOutputProcessor $serviceOutputProcessor,
SerializerInterface $jsonSerializer,
CustomerResourceModel $customerResourceModel,
CustomerFactory $customerFactory
CustomerFactory $customerFactory,
GetAttributeValueComposite $getAttributeValueComposite
) {
$this->serviceOutputProcessor = $serviceOutputProcessor;
$this->jsonSerializer = $jsonSerializer;
$this->customerResourceModel = $customerResourceModel;
$this->customerFactory = $customerFactory;
$this->getAttributeValueComposite = $getAttributeValueComposite;
}

/**
Expand Down Expand Up @@ -124,6 +134,19 @@ public function execute(AddressInterface $address): array
}
$customAttributes[$attribute['attribute_code']] = $attribute['value'];
}

$customAttributesV2 = array_map(
function (array $customAttribute) {
return $this->getAttributeValueComposite->execute(
AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
$customAttribute
);
},
$addressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES]
);
$customAttributes['custom_attributesV2'] = $customAttributesV2;
} else {
$customAttributes['custom_attributesV2'] = [];
}
$addressData = array_merge($addressData, $customAttributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Eav\Model\AttributeRepository;
use Magento\EavGraphQl\Model\GetAttributeValueComposite;
use Magento\EavGraphQl\Model\Uid;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Webapi\ServiceOutputProcessor;

/**
Expand All @@ -27,36 +24,20 @@ class ExtractCustomerData
*/
private $serviceOutputProcessor;

/**
* @var AttributeRepository
*/
private AttributeRepository $attributeRepository;

/**
* @var Uid
*/
private Uid $uid;

/**
* @var GetAttributeValueComposite
*/
private GetAttributeValueComposite $getAttributeValueComposite;

/**
* @param ServiceOutputProcessor $serviceOutputProcessor
* @param AttributeRepository $attributeRepository
* @param Uid $uid
* @param GetAttributeValueComposite $getAttributeValueComposite
*/
public function __construct(
ServiceOutputProcessor $serviceOutputProcessor,
AttributeRepository $attributeRepository,
Uid $uid,
GetAttributeValueComposite $getAttributeValueComposite
) {
$this->serviceOutputProcessor = $serviceOutputProcessor;
$this->attributeRepository = $attributeRepository;
$this->uid = $uid;
$this->getAttributeValueComposite = $getAttributeValueComposite;
}

Expand Down Expand Up @@ -100,10 +81,15 @@ public function execute(CustomerInterface $customer): array
if (isset($customerData['custom_attributes'])) {
$customerData['custom_attributes'] = array_map(
function (array $customAttribute) {
return $this->getAttributeValueComposite->execute($customAttribute);
return $this->getAttributeValueComposite->execute(
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
$customAttribute
);
},
$customerData['custom_attributes']
);
} else {
$customerData['custom_attributes'] = [];
}
//Fields are deprecated and should not be exposed on storefront.
$customerData['group_id'] = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\CustomerGraphQl\Model\Customer;

use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Eav\Model\AttributeRepository;
use Magento\EavGraphQl\Model\GetAttributeSelectedOptionComposite;
use Magento\EavGraphQl\Model\GetAttributeValueInterface;
Expand Down Expand Up @@ -59,23 +58,26 @@ public function __construct(
/**
* @inheritDoc
*/
public function execute(array $customAttribute): ?array
public function execute(string $entityType, array $customAttribute): ?array
{
$attr = $this->attributeRepository->get(
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
$entityType,
$customAttribute['attribute_code']
);

$result = [
'uid' => $this->uid->encode(
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
$entityType,
$customAttribute['attribute_code']
),
'code' => $customAttribute['attribute_code']
];

if (in_array($attr->getFrontendInput(), $this->frontendInputs)) {
$result['selected_options'] = $this->attributeSelectedOptionComposite->execute($customAttribute);
$result['selected_options'] = $this->attributeSelectedOptionComposite->execute(
$entityType,
$customAttribute
);
} else {
$result['value'] = $customAttribute['value'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\CustomerGraphQl\Model\Customer;

use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Eav\Model\AttributeRepository;
use Magento\EavGraphQl\Model\GetAttributeSelectedOptionInterface;
use Magento\Framework\GraphQl\Query\Uid;
Expand All @@ -33,7 +32,7 @@ class GetCustomSelectedOptionAttributes implements GetAttributeSelectedOptionInt
*/
public function __construct(
Uid $uid,
AttributeRepository $attributeRepository,
AttributeRepository $attributeRepository
) {
$this->uid = $uid;
$this->attributeRepository = $attributeRepository;
Expand All @@ -42,10 +41,10 @@ public function __construct(
/**
* @inheritDoc
*/
public function execute(array $customAttribute): ?array
public function execute(string $entityType, array $customAttribute): ?array
{
$attr = $this->attributeRepository->get(
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
$entityType,
$customAttribute['attribute_code']
);

Expand Down
2 changes: 2 additions & 0 deletions app/code/Magento/CustomerGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
<arguments>
<argument name="providers" xsi:type="array">
<item name="customer" xsi:type="object">Magento\CustomerGraphQl\Model\Customer\GetCustomAttributes</item>
<item name="customer_address" xsi:type="object">Magento\CustomerGraphQl\Model\Customer\GetCustomAttributes</item>
</argument>
</arguments>
</type>
Expand All @@ -157,6 +158,7 @@
<arguments>
<argument name="providers" xsi:type="array">
<item name="customer" xsi:type="object">Magento\CustomerGraphQl\Model\Customer\GetCustomSelectedOptionAttributes</item>
<item name="customer_address" xsi:type="object">Magento\CustomerGraphQl\Model\Customer\GetCustomSelectedOptionAttributes</item>
</argument>
</arguments>
</type>
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/CustomerGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ type CustomerAddress @doc(description: "Contains detailed information about a cu
vat_id: String @doc(description: "The customer's Value-added tax (VAT) number (for corporate customers).")
default_shipping: Boolean @doc(description: "Indicates whether the address is the customer's default shipping address.")
default_billing: Boolean @doc(description: "Indicates whether the address is the customer's default billing address.")
custom_attributes: [CustomerAddressAttribute] @deprecated(reason: "Custom attributes should not be put into a container.")
custom_attributes: [CustomerAddressAttribute] @deprecated(reason: "Use custom_attributesV2 instead.")
custom_attributesV2: [AttributeValueInterface!]! @doc(description: "Customer address' custom attributes.")
extension_attributes: [CustomerAddressAttribute] @doc(description: "Contains any extension attributes for the address.")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,25 @@ public function __construct(array $providers = [])
/**
* Returns right GetAttributeSelectedOptionInterface to use for attribute with $attributeCode
*
* @param string $entityType
* @param array $customAttribute
* @return array|null
* @throws RuntimeException
*/
public function execute(array $customAttribute): ?array
public function execute(string $entityType, array $customAttribute): ?array
{
foreach ($this->providers as $provider) {
if (!$provider instanceof GetAttributeSelectedOptionInterface) {
throw new RuntimeException(
__('Configured attribute selected option data providers should implement
GetAttributeSelectedOptionInterface')
);
}

try {
return $provider->execute($customAttribute);
} catch (LocalizedException $e) {
continue;
}
if (!isset($this->providers[$entityType])) {
throw new RuntimeException(
__(sprintf('"%s" entity type not set in providers', $entityType))
);
}
if (!$this->providers[$entityType] instanceof GetAttributeSelectedOptionInterface) {
throw new RuntimeException(
__('Configured attribute selected option data providers should implement
GetAttributeSelectedOptionInterface')
);
}
return null;

return $this->providers[$entityType]->execute($entityType, $customAttribute);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
use Magento\Framework\Exception\LocalizedException;

/**
* Interface for getting custom attributes seelcted options.
* Interface for getting custom attributes selected options.
*/
interface GetAttributeSelectedOptionInterface
{
/**
* Retrieve all selected options of an attribute filtered by attribute code
*
* @param string $entityType
* @param array $customAttribute
* @return array|null
* @throws LocalizedException
*/
public function execute(array $customAttribute): ?array;
public function execute(string $entityType, array $customAttribute): ?array;
}
27 changes: 13 additions & 14 deletions app/code/Magento/EavGraphQl/Model/GetAttributeValueComposite.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,24 @@ public function __construct(array $providers = [])
/**
* Returns right GetAttributeValueInterface to use for attribute with $attributeCode
*
* @param string $entityType
* @param array $customAttribute
* @return array|null
* @throws RuntimeException
*/
public function execute(array $customAttribute): ?array
public function execute(string $entityType, array $customAttribute): ?array
{
foreach ($this->providers as $provider) {
if (!$provider instanceof GetAttributeValueInterface) {
throw new RuntimeException(
__('Configured attribute data providers should implement GetAttributeValueInterface')
);
}

try {
return $provider->execute($customAttribute);
} catch (LocalizedException $e) {
continue;
}
if (!isset($this->providers[$entityType])) {
throw new RuntimeException(
__(sprintf('"%s" entity type not set in providers', $entityType))
);
}
if (!$this->providers[$entityType] instanceof GetAttributeValueInterface) {
throw new RuntimeException(
__('Configured attribute data providers should implement GetAttributeValueInterface')
);
}
return null;

return $this->providers[$entityType]->execute($entityType, $customAttribute);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ interface GetAttributeValueInterface
/**
* Retrieve all attributes filtered by attribute code
*
* @param string $entityType
* @param array $customAttribute
* @return array|null
* @throws LocalizedException
*/
public function execute(array $customAttribute): ?array;
public function execute(string $entityType, array $customAttribute): ?array;
}
Loading

0 comments on commit 6a027c6

Please sign in to comment.