Skip to content

Commit 556666d

Browse files
committed
AC-6731: Validation fixes for WebAPI
1 parent a5e087c commit 556666d

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

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

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\Framework\Exception\InputException;
1111
use Magento\Quote\Api\BillingAddressManagementInterface;
1212
use Magento\Quote\Api\Data\AddressInterface;
13-
use Magento\Quote\Api\Data\CartInterface;
1413
use Psr\Log\LoggerInterface as Logger;
1514

1615
/**
@@ -79,7 +78,7 @@ public function assign($cartId, AddressInterface $address, $useForShipping = fal
7978
$quote = $this->quoteRepository->getActive($cartId);
8079

8180
// validate the address
82-
$this->validateAddress($quote, $address);
81+
$this->addressValidator->validateAddress($quote, $address);
8382

8483
$address->setCustomerId($quote->getCustomerId());
8584
$quote->removeAddress($quote->getBillingAddress()->getId());
@@ -118,23 +117,4 @@ private function getShippingAddressAssignment()
118117
}
119118
return $this->shippingAddressAssignment;
120119
}
121-
122-
/**
123-
* Validate address to be used for cart.
124-
*
125-
* @param CartInterface $cart
126-
* @param AddressInterface $address
127-
* @return void
128-
* @throws InputException The specified address belongs to another customer.
129-
*/
130-
public function validateAddress(CartInterface $cart, AddressInterface $address): void
131-
{
132-
// check if address belongs to quote.
133-
if ($address->getId() !== null) {
134-
$old = $cart->getAddressesCollection()->getItemById($address->getId());
135-
if ($old === null) {
136-
throw new InputException(__('Invalid address'));
137-
}
138-
}
139-
}
140120
}

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ public function __construct(
5959
* @param int|null $customerId Cart belongs to
6060
* @return void
6161
* @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer.
62-
* @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid.
62+
* @throws NoSuchEntityException The specified customer ID or address ID is not valid.
6363
*/
6464
private function doValidate(AddressInterface $address, ?int $customerId): void
6565
{
6666
//validate customer id
6767
if ($customerId) {
6868
$customer = $this->customerRepository->getById($customerId);
6969
if (!$customer->getId()) {
70-
throw new \Magento\Framework\Exception\NoSuchEntityException(
70+
throw new NoSuchEntityException(
7171
__('Invalid customer id %1', $customerId)
7272
);
7373
}
@@ -76,15 +76,15 @@ private function doValidate(AddressInterface $address, ?int $customerId): void
7676
if ($address->getCustomerAddressId()) {
7777
//Existing address cannot belong to a guest
7878
if (!$customerId) {
79-
throw new \Magento\Framework\Exception\NoSuchEntityException(
79+
throw new NoSuchEntityException(
8080
__('Invalid customer address id %1', $address->getCustomerAddressId())
8181
);
8282
}
8383
//Validating address ID
8484
try {
8585
$this->addressRepository->getById($address->getCustomerAddressId());
8686
} catch (NoSuchEntityException $e) {
87-
throw new \Magento\Framework\Exception\NoSuchEntityException(
87+
throw new NoSuchEntityException(
8888
__('Invalid address id %1', $address->getId())
8989
);
9090
}
@@ -94,7 +94,7 @@ private function doValidate(AddressInterface $address, ?int $customerId): void
9494
return $address->getId();
9595
}, $this->customerRepository->getById($customerId)->getAddresses());
9696
if (!in_array($address->getCustomerAddressId(), $applicableAddressIds)) {
97-
throw new \Magento\Framework\Exception\NoSuchEntityException(
97+
throw new NoSuchEntityException(
9898
__('Invalid customer address id %1', $address->getCustomerAddressId())
9999
);
100100
}
@@ -107,7 +107,7 @@ private function doValidate(AddressInterface $address, ?int $customerId): void
107107
* @param \Magento\Quote\Api\Data\AddressInterface $addressData The address data object.
108108
* @return bool
109109
* @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer.
110-
* @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid.
110+
* @throws NoSuchEntityException The specified customer ID or address ID is not valid.
111111
*/
112112
public function validate(AddressInterface $addressData)
113113
{
@@ -123,10 +123,31 @@ public function validate(AddressInterface $addressData)
123123
* @param AddressInterface $address
124124
* @return void
125125
* @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer.
126-
* @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid.
126+
* @throws NoSuchEntityException The specified customer ID or address ID is not valid.
127127
*/
128128
public function validateForCart(CartInterface $cart, AddressInterface $address): void
129129
{
130130
$this->doValidate($address, $cart->getCustomerIsGuest() ? null : $cart->getCustomer()->getId());
131131
}
132+
133+
/**
134+
* Validate address id to be used for cart.
135+
*
136+
* @param CartInterface $cart
137+
* @param AddressInterface $address
138+
* @return void
139+
* @throws NoSuchEntityException The specified customer ID or address ID is not valid.
140+
*/
141+
public function validateAddress(CartInterface $cart, AddressInterface $address): void
142+
{
143+
// check if address belongs to quote.
144+
if ($address->getId() !== null) {
145+
$old = $cart->getAddressesCollection()->getItemById($address->getId());
146+
if ($old === null) {
147+
throw new NoSuchEntityException(
148+
__('Invalid quote address id %1', $address->getId())
149+
);
150+
}
151+
}
152+
}
132153
}

0 commit comments

Comments
 (0)