Skip to content

Commit 4d8d6b9

Browse files
committed
Merge remote-tracking branch '38219/refactor-quote-address-validation' into blth-del10131-commdashpr
2 parents c8ba4ab + 05a46b9 commit 4d8d6b9

File tree

3 files changed

+22
-31
lines changed

3 files changed

+22
-31
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
22
/**
3-
*
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
65
*/
76
namespace Magento\Customer\Api;
87

@@ -28,6 +27,7 @@ public function save(\Magento\Customer\Api\Data\AddressInterface $address);
2827
* @param int $addressId
2928
* @return \Magento\Customer\Api\Data\AddressInterface
3029
* @throws \Magento\Framework\Exception\LocalizedException
30+
* @throws \Magento\Framework\Exception\NoSuchEntityException
3131
*/
3232
public function getById($addressId);
3333

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Customer\Model\ResourceModel;
77

@@ -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: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\Quote\Model;
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)