AddressDeterminer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model;
  7. use Magento\Customer\Api\AddressRepositoryInterface;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Customer\Api\Data\AddressInterface;
  10. use Magento\Quote\Api\Data\AddressInterface as QuoteAddressInterface;
  11. /**
  12. * Determines the address to use for tax calculation
  13. */
  14. class AddressDeterminer
  15. {
  16. /** @var AddressRepositoryInterface */
  17. private $addressRepository;
  18. /** @var CustomerRepositoryInterface */
  19. private $customerRepository;
  20. /** @var ExceptionLogger */
  21. private $logger;
  22. /**
  23. * @param CustomerRepositoryInterface $customerRepository
  24. * @param AddressRepositoryInterface $addressRepository
  25. * @param ExceptionLogger $logger
  26. */
  27. public function __construct(
  28. CustomerRepositoryInterface $customerRepository,
  29. AddressRepositoryInterface $addressRepository,
  30. ExceptionLogger $logger
  31. ) {
  32. $this->customerRepository = $customerRepository;
  33. $this->addressRepository = $addressRepository;
  34. $this->logger = $logger;
  35. }
  36. /**
  37. * Determine the address to use for Tax Calculation
  38. *
  39. * @param AddressInterface|QuoteAddressInterface $address
  40. * @param int|null $customerId
  41. * @param bool $virtual Whether or not the cart or order is virtual
  42. * @return AddressInterface|null
  43. */
  44. public function determineAddress($address = null, $customerId = null, $virtual = false)
  45. {
  46. if ($address !== null && !($address instanceof AddressInterface || $address instanceof QuoteAddressInterface)) {
  47. throw new \InvalidArgumentException(
  48. '$address must be a Customer or Quote Address. Is: '
  49. . (is_object($address) ? get_class($address) : gettype($address))
  50. );
  51. }
  52. if (!$customerId || ($address !== null && $address->getCountryId() !== null)) {
  53. return $address;
  54. }
  55. // Default to billing address for virtual orders unless there is not one
  56. if ($virtual) {
  57. $billing = $this->getDefaultBilling($customerId);
  58. return $billing !== null ? $billing : $this->getDefaultShipping($customerId);
  59. }
  60. // Default to shipping address for physical orders unless there is not one
  61. $shipping = $this->getDefaultShipping($customerId);
  62. return $shipping !== null ? $shipping : $this->getDefaultBilling($customerId);
  63. }
  64. /**
  65. * Retrieve the default billing address for a customer
  66. *
  67. * @param int $customerId
  68. * @return AddressInterface|null
  69. */
  70. private function getDefaultBilling($customerId)
  71. {
  72. try {
  73. $customer = $this->customerRepository->getById($customerId);
  74. $addressId = $customer->getDefaultBilling();
  75. return $this->addressRepository->getById($addressId);
  76. } catch (\Exception $e) {
  77. $this->logger->warning($e);
  78. return null;
  79. }
  80. }
  81. /**
  82. * Retrieve the default shipping address for a customer
  83. *
  84. * @param int $customerId
  85. * @return AddressInterface|null
  86. */
  87. private function getDefaultShipping($customerId)
  88. {
  89. try {
  90. $customer = $this->customerRepository->getById($customerId);
  91. $addressId = $customer->getDefaultShipping();
  92. return $this->addressRepository->getById($addressId);
  93. } catch (\Exception $e) {
  94. $this->logger->warning($e);
  95. return null;
  96. }
  97. }
  98. }