Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Customer\Address;

use Magento\Framework\GraphQl\Exception\GraphQlInputException;

/**
* Customer address create data validator
*/
class CustomerAddressCreateDataValidator
{
/**
* @var GetAllowedAddressAttributes
*/
private $getAllowedAddressAttributes;

/**
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
*/
public function __construct(GetAllowedAddressAttributes $getAllowedAddressAttributes)
{
$this->getAllowedAddressAttributes = $getAllowedAddressAttributes;
}

/**
* Validate customer address create data
*
* @param array $addressData
* @return void
* @throws GraphQlInputException
*/
public function validate(array $addressData): void
{
$attributes = $this->getAllowedAddressAttributes->execute();
$errorInput = [];

foreach ($attributes as $attributeName => $attributeInfo) {
if ($attributeInfo->getIsRequired()
&& (!isset($addressData[$attributeName]) || empty($addressData[$attributeName]))
) {
$errorInput[] = $attributeName;
}
}

if ($errorInput) {
throw new GraphQlInputException(
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Customer\Address;

use Magento\Customer\Api\Data\AddressInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Api\CustomAttributesDataInterface;
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Model\ResourceModel\Customer as CustomerResourceModel;
use Magento\Customer\Model\CustomerFactory;
use Magento\Framework\Webapi\ServiceOutputProcessor;
use Magento\Framework\Serialize\SerializerInterface;

/**
* Customer Address field data provider, used for GraphQL request processing.
*/
class CustomerAddressDataProvider
{
/**
* @var ServiceOutputProcessor
*/
private $serviceOutputProcessor;

/**
* @var SerializerInterface
*/
private $jsonSerializer;

/**
* @var CustomerResourceModel
*/
private $customerResourceModel;

/**
* @var CustomerFactory
*/
private $customerFactory;

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

/**
* Curate shipping and billing default options
*
* @param array $address
* @param AddressInterface $addressObject
* @return array
*/
private function curateAddressDefaultValues(array $address, AddressInterface $addressObject) : array
{
$customerModel = $this->customerFactory->create();
$this->customerResourceModel->load($customerModel, $addressObject->getCustomerId());
$address[CustomerInterface::DEFAULT_BILLING] =
($customerModel->getDefaultBillingAddress()
&& $addressObject->getId() == $customerModel->getDefaultBillingAddress()->getId());
$address[CustomerInterface::DEFAULT_SHIPPING] =
($customerModel->getDefaultShippingAddress()
&& $addressObject->getId() == $customerModel->getDefaultShippingAddress()->getId());
return $address;
}

/**
* Transform single customer address data from object to in array format
*
* @param AddressInterface $addressObject
* @return array
*/
public function getAddressData(AddressInterface $addressObject): array
{
$address = $this->serviceOutputProcessor->process(
$addressObject,
AddressRepositoryInterface::class,
'getById'
);
$address = $this->curateAddressDefaultValues($address, $addressObject);

if (isset($address[CustomAttributesDataInterface::EXTENSION_ATTRIBUTES_KEY])) {
$address = array_merge($address, $address[CustomAttributesDataInterface::EXTENSION_ATTRIBUTES_KEY]);
}
$customAttributes = [];
if (isset($address[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])) {
foreach ($address[CustomAttributesDataInterface::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->jsonSerializer->serialize(
$attribute['value']
);
continue;
}
$customAttributes[$attribute['attribute_code']] = implode(',', $attribute['value']);
continue;
}
}
if ($isArray) {
continue;
}
$customAttributes[$attribute['attribute_code']] = $attribute['value'];
}
}
$address = array_merge($address, $customAttributes);

return $address;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Customer\Address;

use Magento\Framework\GraphQl\Exception\GraphQlInputException;

/**
* Customer address update data validator. Patch update is allowed
*/
class CustomerAddressUpdateDataValidator
{
/**
* @var GetAllowedAddressAttributes
*/
private $getAllowedAddressAttributes;

/**
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
*/
public function __construct(GetAllowedAddressAttributes $getAllowedAddressAttributes)
{
$this->getAllowedAddressAttributes = $getAllowedAddressAttributes;
}

/**
* Validate customer address update data
*
* @param array $addressData
* @return void
* @throws GraphQlInputException
*/
public function validate(array $addressData): void
{
$attributes = $this->getAllowedAddressAttributes->execute();
$errorInput = [];

foreach ($attributes as $attributeName => $attributeInfo) {
if ($attributeInfo->getIsRequired()
&& (isset($addressData[$attributeName]) && empty($addressData[$attributeName]))
) {
$errorInput[] = $attributeName;
}
}

if ($errorInput) {
throw new GraphQlInputException(
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Customer\Address;

use Magento\Customer\Api\AddressMetadataManagementInterface;
use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;

/**
* Get allowed address attributes
*/
class GetAllowedAddressAttributes
{
/**
* @var Config
*/
private $eavConfig;

/**
* @param Config $eavConfig
*/
public function __construct(Config $eavConfig)
{
$this->eavConfig = $eavConfig;
}

/**
* Get allowed address attributes
*
* @return AbstractAttribute[]
*/
public function execute(): array
{
$attributes = $this->eavConfig->getEntityAttributes(
AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS
);
foreach ($attributes as $attributeCode => $attribute) {
if (false === $attribute->getIsVisibleOnFront()) {
unset($attributes[$attributeCode]);
}
}
return $attributes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Customer\Address;

use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;

/**
* Get customer address for user
*/
class GetCustomerAddressForUser
{
/**
* @var AddressRepositoryInterface
*/
private $addressRepository;

/**
* @param AddressRepositoryInterface $addressRepository
*/
public function __construct(AddressRepositoryInterface $addressRepository)
{
$this->addressRepository = $addressRepository;
}

/**
* Get customer address for user
*
* @param int $addressId
* @param int $userId
* @return AddressInterface
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
*/
public function execute(int $addressId, int $userId): AddressInterface
{
try {
/** @var AddressInterface $address */
$address = $this->addressRepository->getById($addressId);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(
__('Address id %1 does not exist.', [$addressId])
);
}

if ($address->getCustomerId() != $userId) {
throw new GraphQlAuthorizationException(
__('Current customer does not have permission to address id %1', [$addressId])
);
}
return $address;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ public function getCustomerById(int $customerId): array
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
*
Expand All @@ -83,6 +102,7 @@ private function processCustomer(CustomerInterface $customer): array
CustomerRepositoryInterface::class,
'get'
);
$customerData['addresses'] = $this->curateAddressData($customerData['addresses']);
if (isset($customerData['extension_attributes'])) {
$customerData = array_merge($customerData, $customerData['extension_attributes']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
use Magento\Store\Model\StoreManagerInterface;

/**
* Update account information
* Update customer data
*/
class UpdateAccountInformation
class UpdateCustomerData
{
/**
* @var CustomerRepositoryInterface
Expand Down
Loading