Skip to content

Refactor quote address do validate method #38219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
6 changes: 3 additions & 3 deletions app/code/Magento/Customer/Api/AddressRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2014 Adobe
* All Rights Reserved.
*/
namespace Magento\Customer\Api;

Expand All @@ -28,6 +27,7 @@ public function save(\Magento\Customer\Api\Data\AddressInterface $address);
* @param int $addressId
* @return \Magento\Customer\Api\Data\AddressInterface
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getById($addressId);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2014 Adobe
* All Rights Reserved.
*/
namespace Magento\Customer\Model\ResourceModel;

Expand Down Expand Up @@ -164,6 +164,7 @@ private function updateAddressCollection(CustomerModel $customer, CustomerAddres
* @param int $addressId
* @return \Magento\Customer\Api\Data\AddressInterface
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getById($addressId)
{
Expand Down
42 changes: 16 additions & 26 deletions app/code/Magento/Quote/Model/QuoteAddressValidator.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2014 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\Quote\Model;

use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\AddressInterface as CustomerAddress;
use Magento\Customer\Model\Session;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\LocalizedException;
Expand Down Expand Up @@ -68,39 +69,28 @@ public function __construct(
*/
private function doValidate(AddressInterface $address, ?int $customerId): void
{
//validate customer id
if ($customerId) {
$customer = $this->customerRepository->getById($customerId);
if (!$customer->getId()) {
throw new NoSuchEntityException(
__('Invalid customer id %1', $customerId)
);
}
}

if ($address->getCustomerAddressId()) {
$customerAddressId = $address->getCustomerAddressId();
if ($customerAddressId) {
//Existing address cannot belong to a guest
if (!$customerId) {
throw new NoSuchEntityException(
__('Invalid customer address id %1', $address->getCustomerAddressId())
__('Invalid customer address id %1', $customerAddressId)
);
}

$customer = $this->customerRepository->getById($customerId);

//Validating address ID
try {
$this->addressRepository->getById($address->getCustomerAddressId());
} catch (NoSuchEntityException $e) {
throw new NoSuchEntityException(
__('Invalid address id %1', $address->getId())
);
}
$this->addressRepository->getById($customerAddressId);

//Finding available customer's addresses
$applicableAddressIds = array_map(function ($address) {
/** @var \Magento\Customer\Api\Data\AddressInterface $address */
$applicableAddressIds = array_map(function (CustomerAddress $address) {
return $address->getId();
}, $this->customerRepository->getById($customerId)->getAddresses());
if (!in_array($address->getCustomerAddressId(), $applicableAddressIds)) {
}, $customer->getAddresses());

if (!in_array($customerAddressId, $applicableAddressIds)) {
throw new NoSuchEntityException(
__('Invalid customer address id %1', $address->getCustomerAddressId())
__('Invalid customer address id %1', $customerAddressId)
);
}
}
Expand Down