Skip to content

Commit a24a503

Browse files
refactor quote address do validate method
1 parent 298cbd4 commit a24a503

File tree

3 files changed

+16
-24
lines changed

3 files changed

+16
-24
lines changed

app/code/Magento/Customer/Api/AddressRepositoryInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function save(\Magento\Customer\Api\Data\AddressInterface $address);
2828
* @param int $addressId
2929
* @return \Magento\Customer\Api\Data\AddressInterface
3030
* @throws \Magento\Framework\Exception\LocalizedException
31+
* @throws \Magento\Framework\Exception\NoSuchEntityException
3132
*/
3233
public function getById($addressId);
3334

app/code/Magento/Customer/Model/ResourceModel/AddressRepository.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ private function updateAddressCollection(CustomerModel $customer, CustomerAddres
164164
* @param int $addressId
165165
* @return \Magento\Customer\Api\Data\AddressInterface
166166
* @throws \Magento\Framework\Exception\LocalizedException
167+
* @throws \Magento\Framework\Exception\NoSuchEntityException
167168
*/
168169
public function getById($addressId)
169170
{

app/code/Magento/Quote/Model/QuoteAddressValidator.php

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Customer\Api\AddressRepositoryInterface;
1111
use Magento\Customer\Api\CustomerRepositoryInterface;
12+
use Magento\Customer\Api\Data\AddressInterface as CustomerAddress;
1213
use Magento\Customer\Model\Session;
1314
use Magento\Framework\Exception\InputException;
1415
use Magento\Framework\Exception\LocalizedException;
@@ -68,39 +69,28 @@ public function __construct(
6869
*/
6970
private function doValidate(AddressInterface $address, ?int $customerId): void
7071
{
71-
//validate customer id
72-
if ($customerId) {
73-
$customer = $this->customerRepository->getById($customerId);
74-
if (!$customer->getId()) {
75-
throw new NoSuchEntityException(
76-
__('Invalid customer id %1', $customerId)
77-
);
78-
}
79-
}
80-
81-
if ($address->getCustomerAddressId()) {
72+
$customerAddressId = $address->getCustomerAddressId();
73+
if ($customerAddressId) {
8274
//Existing address cannot belong to a guest
8375
if (!$customerId) {
8476
throw new NoSuchEntityException(
85-
__('Invalid customer address id %1', $address->getCustomerAddressId())
77+
__('Invalid customer address id %1', $customerAddressId)
8678
);
8779
}
80+
81+
$customer = $this->customerRepository->getById($customerId);
82+
8883
//Validating address ID
89-
try {
90-
$this->addressRepository->getById($address->getCustomerAddressId());
91-
} catch (NoSuchEntityException $e) {
92-
throw new NoSuchEntityException(
93-
__('Invalid address id %1', $address->getId())
94-
);
95-
}
84+
$this->addressRepository->getById($customerAddressId);
85+
9686
//Finding available customer's addresses
97-
$applicableAddressIds = array_map(function ($address) {
98-
/** @var \Magento\Customer\Api\Data\AddressInterface $address */
87+
$applicableAddressIds = array_map(function (CustomerAddress $address) {
9988
return $address->getId();
100-
}, $this->customerRepository->getById($customerId)->getAddresses());
101-
if (!in_array($address->getCustomerAddressId(), $applicableAddressIds)) {
89+
}, $customer->getAddresses());
90+
91+
if (!in_array($customerAddressId, $applicableAddressIds)) {
10292
throw new NoSuchEntityException(
103-
__('Invalid customer address id %1', $address->getCustomerAddressId())
93+
__('Invalid customer address id %1', $customerAddressId)
10494
);
10595
}
10696
}

0 commit comments

Comments
 (0)