QuoteAddressValidator.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Quote\Api\Data\AddressInterface;
  9. use Magento\Quote\Api\Data\CartInterface;
  10. /**
  11. * Quote shipping/billing address validator service.
  12. *
  13. * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
  14. */
  15. class QuoteAddressValidator
  16. {
  17. /**
  18. * Address factory.
  19. *
  20. * @var \Magento\Customer\Api\AddressRepositoryInterface
  21. */
  22. protected $addressRepository;
  23. /**
  24. * Customer repository.
  25. *
  26. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  27. */
  28. protected $customerRepository;
  29. /**
  30. * @deprecated 101.1.1 This class is not a part of HTML presentation layer and should not use sessions.
  31. */
  32. protected $customerSession;
  33. /**
  34. * Constructs a quote shipping address validator service object.
  35. *
  36. * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
  37. * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository Customer repository.
  38. * @param \Magento\Customer\Model\Session $customerSession
  39. */
  40. public function __construct(
  41. \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
  42. \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
  43. \Magento\Customer\Model\Session $customerSession
  44. ) {
  45. $this->addressRepository = $addressRepository;
  46. $this->customerRepository = $customerRepository;
  47. $this->customerSession = $customerSession;
  48. }
  49. /**
  50. * Validate address.
  51. *
  52. * @param AddressInterface $address
  53. * @param int|null $customerId Cart belongs to
  54. * @return void
  55. * @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer.
  56. * @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid.
  57. */
  58. private function doValidate(AddressInterface $address, ?int $customerId): void
  59. {
  60. //validate customer id
  61. if ($customerId) {
  62. $customer = $this->customerRepository->getById($customerId);
  63. if (!$customer->getId()) {
  64. throw new \Magento\Framework\Exception\NoSuchEntityException(
  65. __('Invalid customer id %1', $customerId)
  66. );
  67. }
  68. }
  69. if ($address->getCustomerAddressId()) {
  70. //Existing address cannot belong to a guest
  71. if (!$customerId) {
  72. throw new \Magento\Framework\Exception\NoSuchEntityException(
  73. __('Invalid customer address id %1', $address->getCustomerAddressId())
  74. );
  75. }
  76. //Validating address ID
  77. try {
  78. $this->addressRepository->getById($address->getCustomerAddressId());
  79. } catch (NoSuchEntityException $e) {
  80. throw new \Magento\Framework\Exception\NoSuchEntityException(
  81. __('Invalid address id %1', $address->getId())
  82. );
  83. }
  84. //Finding available customer's addresses
  85. $applicableAddressIds = array_map(function ($address) {
  86. /** @var \Magento\Customer\Api\Data\AddressInterface $address */
  87. return $address->getId();
  88. }, $this->customerRepository->getById($customerId)->getAddresses());
  89. if (!in_array($address->getCustomerAddressId(), $applicableAddressIds)) {
  90. throw new \Magento\Framework\Exception\NoSuchEntityException(
  91. __('Invalid customer address id %1', $address->getCustomerAddressId())
  92. );
  93. }
  94. }
  95. }
  96. /**
  97. * Validates the fields in a specified address data object.
  98. *
  99. * @param \Magento\Quote\Api\Data\AddressInterface $addressData The address data object.
  100. * @return bool
  101. * @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer.
  102. * @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid.
  103. */
  104. public function validate(AddressInterface $addressData)
  105. {
  106. $this->doValidate($addressData, $addressData->getCustomerId());
  107. return true;
  108. }
  109. /**
  110. * Validate address to be used for cart.
  111. *
  112. * @param CartInterface $cart
  113. * @param AddressInterface $address
  114. * @return void
  115. * @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer.
  116. * @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid.
  117. */
  118. public function validateForCart(CartInterface $cart, AddressInterface $address): void
  119. {
  120. $this->doValidate($address, $cart->getCustomerIsGuest() ? null : $cart->getCustomer()->getId());
  121. }
  122. }