VertexUsageDeterminer.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\Data\AddressInterface;
  8. use Magento\Quote\Api\Data\AddressInterface as QuoteAddressInterface;
  9. /**
  10. * Business logic for determining if Vertex should be used for tax calculation
  11. */
  12. class VertexUsageDeterminer
  13. {
  14. /** @var AddressDeterminer */
  15. private $addressDeterminer;
  16. /** @var Config */
  17. private $config;
  18. /** @var CountryGuard */
  19. private $countryGuard;
  20. /**
  21. * @param Config $config
  22. * @param CountryGuard $countryGuard
  23. * @param AddressDeterminer $addressDeterminer
  24. */
  25. public function __construct(Config $config, CountryGuard $countryGuard, AddressDeterminer $addressDeterminer)
  26. {
  27. $this->config = $config;
  28. $this->countryGuard = $countryGuard;
  29. $this->addressDeterminer = $addressDeterminer;
  30. }
  31. /**
  32. * Determine whether or not to use Vertex to calculate taxes for an address
  33. *
  34. * @param string|null $storeCode
  35. * @param AddressInterface|null $address
  36. * @param int|null $customerId
  37. * @param bool $isVirtual
  38. * @param bool $checkCalculation
  39. * @return bool
  40. */
  41. public function shouldUseVertex(
  42. $storeCode = null,
  43. $address = null,
  44. $customerId = null,
  45. $isVirtual = false,
  46. $checkCalculation = false
  47. ) {
  48. if (!$this->config->isVertexActive($storeCode)
  49. || ($checkCalculation && !$this->config->isTaxCalculationEnabled($storeCode))
  50. ) {
  51. return false;
  52. }
  53. if ($address !== null && !($address instanceof AddressInterface || $address instanceof QuoteAddressInterface)) {
  54. throw new \InvalidArgumentException(
  55. '$address must be a Customer or Quote Address. Is: '
  56. .(is_object($address) ? get_class($address) : gettype($address))
  57. );
  58. }
  59. $address = $this->addressDeterminer->determineAddress($address, $customerId, $isVirtual);
  60. return !$this->config->isDisplayPriceInCatalogEnabled($storeCode)
  61. && $address !== null
  62. && $address->getCountryId()
  63. && $this->countryGuard->isCountryIdServiceableByVertex($address->getCountryId());
  64. }
  65. }