123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\CustomerGraphQl\Model\Customer;
- use Magento\Customer\Api\CustomerRepositoryInterface;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
- use Magento\Framework\Serialize\SerializerInterface;
- use Magento\Framework\Webapi\ServiceOutputProcessor;
- use Magento\Customer\Api\Data\CustomerInterface;
- /**
- * Customer field data provider, used for GraphQL request processing.
- */
- class CustomerDataProvider
- {
- /**
- * @var CustomerRepositoryInterface
- */
- private $customerRepository;
- /**
- * @var ServiceOutputProcessor
- */
- private $serviceOutputProcessor;
- /**
- * @var SerializerInterface
- */
- private $serializer;
- /**
- * @param CustomerRepositoryInterface $customerRepository
- * @param ServiceOutputProcessor $serviceOutputProcessor
- * @param SerializerInterface $serializer
- */
- public function __construct(
- CustomerRepositoryInterface $customerRepository,
- ServiceOutputProcessor $serviceOutputProcessor,
- SerializerInterface $serializer
- ) {
- $this->customerRepository = $customerRepository;
- $this->serviceOutputProcessor = $serviceOutputProcessor;
- $this->serializer = $serializer;
- }
- /**
- * Get customer data by Id or empty array
- *
- * @param int $customerId
- * @return array
- * @throws NoSuchEntityException|LocalizedException
- */
- public function getCustomerById(int $customerId): array
- {
- try {
- $customer = $this->customerRepository->getById($customerId);
- } catch (NoSuchEntityException $e) {
- throw new GraphQlNoSuchEntityException(
- __('Customer id "%customer_id" does not exist.', ['customer_id' => $customerId]),
- $e
- );
- }
- return $this->processCustomer($customer);
- }
- /**
- * Curate default shipping and default billing keys
- *
- * @param array $arrayAddress
- * @return array
- */
- private function curateAddressData(array $arrayAddress) : array
- {
- foreach ($arrayAddress as $key => $address) {
- if (!isset($address['default_shipping'])) {
- $arrayAddress[$key]['default_shipping'] = false;
- }
- if (!isset($address['default_billing'])) {
- $arrayAddress[$key]['default_billing'] = false;
- }
- }
- return $arrayAddress;
- }
- /**
- * Transform single customer data from object to in array format
- *
- * @param CustomerInterface $customer
- * @return array
- */
- private function processCustomer(CustomerInterface $customer): array
- {
- $customerData = $this->serviceOutputProcessor->process(
- $customer,
- CustomerRepositoryInterface::class,
- 'get'
- );
- $customerData['addresses'] = $this->curateAddressData($customerData['addresses']);
- if (isset($customerData['extension_attributes'])) {
- $customerData = array_merge($customerData, $customerData['extension_attributes']);
- }
- $customAttributes = [];
- if (isset($customerData['custom_attributes'])) {
- foreach ($customerData['custom_attributes'] as $attribute) {
- $isArray = false;
- if (is_array($attribute['value'])) {
- $isArray = true;
- foreach ($attribute['value'] as $attributeValue) {
- if (is_array($attributeValue)) {
- $customAttributes[$attribute['attribute_code']] = $this->serializer->serialize(
- $attribute['value']
- );
- continue;
- }
- $customAttributes[$attribute['attribute_code']] = implode(',', $attribute['value']);
- continue;
- }
- }
- if ($isArray) {
- continue;
- }
- $customAttributes[$attribute['attribute_code']] = $attribute['value'];
- }
- }
- $customerData = array_merge($customerData, $customAttributes);
- return $customerData;
- }
- }
|