-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3892 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
- Loading branch information
Showing
54 changed files
with
1,041 additions
and
789 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?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\Customer\Api\Data\AddressInterfaceFactory; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\Api\DataObjectHelper; | ||
|
||
/** | ||
* Create customer address | ||
*/ | ||
class CreateCustomerAddress | ||
{ | ||
/** | ||
* @var GetAllowedAddressAttributes | ||
*/ | ||
private $getAllowedAddressAttributes; | ||
|
||
/** | ||
* @var AddressInterfaceFactory | ||
*/ | ||
private $addressFactory; | ||
|
||
/** | ||
* @var AddressRepositoryInterface | ||
*/ | ||
private $addressRepository; | ||
|
||
/** | ||
* @var DataObjectHelper | ||
*/ | ||
private $dataObjectHelper; | ||
|
||
/** | ||
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes | ||
* @param AddressInterfaceFactory $addressFactory | ||
* @param AddressRepositoryInterface $addressRepository | ||
* @param DataObjectHelper $dataObjectHelper | ||
*/ | ||
public function __construct( | ||
GetAllowedAddressAttributes $getAllowedAddressAttributes, | ||
AddressInterfaceFactory $addressFactory, | ||
AddressRepositoryInterface $addressRepository, | ||
DataObjectHelper $dataObjectHelper | ||
) { | ||
$this->getAllowedAddressAttributes = $getAllowedAddressAttributes; | ||
$this->addressFactory = $addressFactory; | ||
$this->addressRepository = $addressRepository; | ||
$this->dataObjectHelper = $dataObjectHelper; | ||
} | ||
|
||
/** | ||
* Create customer address | ||
* | ||
* @param int $customerId | ||
* @param array $data | ||
* @return AddressInterface | ||
* @throws GraphQlInputException | ||
*/ | ||
public function execute(int $customerId, array $data): AddressInterface | ||
{ | ||
$this->validateData($data); | ||
|
||
/** @var AddressInterface $address */ | ||
$address = $this->addressFactory->create(); | ||
$this->dataObjectHelper->populateWithArray($address, $data, AddressInterface::class); | ||
|
||
if (isset($data['region']['region_id'])) { | ||
$address->setRegionId($address->getRegion()->getRegionId()); | ||
} | ||
$address->setCustomerId($customerId); | ||
|
||
try { | ||
$this->addressRepository->save($address); | ||
} catch (LocalizedException $e) { | ||
throw new GraphQlInputException(__($e->getMessage()), $e); | ||
} | ||
return $address; | ||
} | ||
|
||
/** | ||
* Validate customer address create data | ||
* | ||
* @param array $addressData | ||
* @return void | ||
* @throws GraphQlInputException | ||
*/ | ||
public function validateData(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)]) | ||
); | ||
} | ||
} | ||
} |
56 changes: 0 additions & 56 deletions
56
...ode/Magento/CustomerGraphQl/Model/Customer/Address/CustomerAddressCreateDataValidator.php
This file was deleted.
Oops, something went wrong.
56 changes: 0 additions & 56 deletions
56
...ode/Magento/CustomerGraphQl/Model/Customer/Address/CustomerAddressUpdateDataValidator.php
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
app/code/Magento/CustomerGraphQl/Model/Customer/Address/DeleteCustomerAddress.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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\LocalizedException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
|
||
/** | ||
* Delete customer address | ||
*/ | ||
class DeleteCustomerAddress | ||
{ | ||
/** | ||
* @var AddressRepositoryInterface | ||
*/ | ||
private $addressRepository; | ||
|
||
/** | ||
* @param AddressRepositoryInterface $addressRepository | ||
*/ | ||
public function __construct( | ||
AddressRepositoryInterface $addressRepository | ||
) { | ||
$this->addressRepository = $addressRepository; | ||
} | ||
|
||
/** | ||
* Delete customer address | ||
* | ||
* @param AddressInterface $address | ||
* @return void | ||
* @throws GraphQlInputException | ||
*/ | ||
public function execute(AddressInterface $address): void | ||
{ | ||
if ($address->isDefaultBilling()) { | ||
throw new GraphQlInputException( | ||
__('Customer Address %1 is set as default billing address and can not be deleted', [$address->getId()]) | ||
); | ||
} | ||
if ($address->isDefaultShipping()) { | ||
throw new GraphQlInputException( | ||
__('Customer Address %1 is set as default shipping address and can not be deleted', [$address->getId()]) | ||
); | ||
} | ||
|
||
try { | ||
$this->addressRepository->delete($address); | ||
} catch (LocalizedException $e) { | ||
throw new GraphQlInputException(__($e->getMessage()), $e); | ||
} | ||
} | ||
} |
Oops, something went wrong.