Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
QuoteAddressValidator.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Quote\Model;
7 
9 
15 {
21  protected $addressRepository;
22 
29 
33  protected $customerSession;
34 
42  public function __construct(
45  \Magento\Customer\Model\Session $customerSession
46  ) {
47  $this->addressRepository = $addressRepository;
48  $this->customerRepository = $customerRepository;
49  $this->customerSession = $customerSession;
50  }
51 
60  public function validate(\Magento\Quote\Api\Data\AddressInterface $addressData)
61  {
62  //validate customer id
63  if ($addressData->getCustomerId()) {
64  $customer = $this->customerRepository->getById($addressData->getCustomerId());
65  if (!$customer->getId()) {
66  throw new \Magento\Framework\Exception\NoSuchEntityException(
67  __('Invalid customer id %1', $addressData->getCustomerId())
68  );
69  }
70  }
71 
72  if ($addressData->getCustomerAddressId()) {
73  try {
74  $this->addressRepository->getById($addressData->getCustomerAddressId());
75  } catch (NoSuchEntityException $e) {
76  throw new \Magento\Framework\Exception\NoSuchEntityException(
77  __('Invalid address id %1', $addressData->getId())
78  );
79  }
80 
81  $applicableAddressIds = array_map(function ($address) {
83  return $address->getId();
84  }, $this->customerRepository->getById($addressData->getCustomerId())->getAddresses());
85  if (!in_array($addressData->getCustomerAddressId(), $applicableAddressIds)) {
86  throw new \Magento\Framework\Exception\NoSuchEntityException(
87  __('Invalid customer address id %1', $addressData->getCustomerAddressId())
88  );
89  }
90  }
91  return true;
92  }
93 }
$customer
Definition: customers.php:11
__()
Definition: __.php:13
$address
Definition: customer.php:38
$addressData
Definition: order.php:19
__construct(\Magento\Customer\Api\AddressRepositoryInterface $addressRepository, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Customer\Model\Session $customerSession)