TaxQuoteRequest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\TaxQuote;
  7. use Vertex\Exception\ApiException;
  8. use Vertex\Exception\ConfigurationException;
  9. use Vertex\Exception\ValidationException;
  10. use Vertex\Mapper\QuoteResponseMapperInterface;
  11. use Vertex\Services\Quote\RequestInterface;
  12. use Vertex\Services\Quote\ResponseInterface;
  13. use Vertex\Tax\Api\QuoteInterface;
  14. use Vertex\Tax\Model\Api\Utility\MapperFactoryProxy;
  15. use Vertex\Tax\Model\ExceptionLogger;
  16. use Vertex\Tax\Model\TaxRegistry;
  17. /**
  18. * Tax Quotation Request Service
  19. */
  20. class TaxQuoteRequest
  21. {
  22. /** @var CacheKeyGenerator */
  23. private $cacheKeyGenerator;
  24. /** @var ExceptionLogger */
  25. private $logger;
  26. /** @var MapperFactoryProxy */
  27. private $mapperFactory;
  28. /** @var QuoteInterface */
  29. private $quote;
  30. /** @var TaxRegistry */
  31. private $taxRegistry;
  32. /**
  33. * @param QuoteInterface $quote
  34. * @param CacheKeyGenerator $cacheKeyGenerator
  35. * @param TaxRegistry $taxRegistry
  36. * @param ExceptionLogger $logger
  37. * @param MapperFactoryProxy $mapperFactory
  38. */
  39. public function __construct(
  40. QuoteInterface $quote,
  41. CacheKeyGenerator $cacheKeyGenerator,
  42. TaxRegistry $taxRegistry,
  43. ExceptionLogger $logger,
  44. MapperFactoryProxy $mapperFactory
  45. ) {
  46. $this->quote = $quote;
  47. $this->cacheKeyGenerator = $cacheKeyGenerator;
  48. $this->taxRegistry = $taxRegistry;
  49. $this->logger = $logger;
  50. $this->mapperFactory = $mapperFactory;
  51. }
  52. /**
  53. * Perform a Quotation Request
  54. *
  55. * @param RequestInterface $request
  56. * @param string|null $scopeCode
  57. * @return ResponseInterface|bool
  58. * @throws ApiException
  59. * @throws ValidationException
  60. * @throws ConfigurationException
  61. */
  62. public function taxQuote(RequestInterface $request, $scopeCode = null)
  63. {
  64. $cacheKey = false;
  65. $response = false;
  66. try {
  67. $cacheKey = $this->cacheKeyGenerator->generateCacheKey($request);
  68. } catch (\Exception $e) {
  69. $this->logger->warning($e);
  70. }
  71. if ($cacheKey !== false) {
  72. try {
  73. $response = $this->getCachedResponse($cacheKey, $scopeCode);
  74. } catch (\Exception $e) {
  75. $this->logger->warning($e);
  76. }
  77. }
  78. if (!$response) {
  79. try {
  80. $response = $this->quote->request($request, $scopeCode);
  81. } catch (\Exception $e) {
  82. $this->logger->critical($e);
  83. throw $e;
  84. }
  85. $this->registerResponseInCache($cacheKey, $response, $scopeCode);
  86. }
  87. return $response;
  88. }
  89. /**
  90. * Retrieve the Response from the Cache
  91. *
  92. * @param string $cacheKey
  93. * @param string|null $scopeCode Store ID
  94. * @return ResponseInterface|bool
  95. */
  96. private function getCachedResponse($cacheKey, $scopeCode = null)
  97. {
  98. try {
  99. /** @var QuoteResponseMapperInterface $mapper */
  100. $mapper = $this->mapperFactory->getForClass(ResponseInterface::class, $scopeCode);
  101. } catch (\Exception $e) {
  102. $this->logger->warning($e);
  103. return false;
  104. }
  105. $mappedResponse = $this->taxRegistry->lookup($cacheKey);
  106. return $mappedResponse !== null ? $mapper->build($mappedResponse) : false;
  107. }
  108. /**
  109. * Register the Response in the Cache
  110. *
  111. * @param string $cacheKey
  112. * @param ResponseInterface $response
  113. * @param string|null $scopeCode Store ID
  114. * @return void
  115. */
  116. private function registerResponseInCache($cacheKey, ResponseInterface $response, $scopeCode = null)
  117. {
  118. try {
  119. /** @var QuoteResponseMapperInterface $mapper */
  120. $mapper = $this->mapperFactory->getForClass(ResponseInterface::class, $scopeCode);
  121. $mappedResponse = $mapper->map($response);
  122. } catch (\Exception $e) {
  123. $this->logger->warning($e);
  124. return;
  125. }
  126. $this->taxRegistry->register($cacheKey, $mappedResponse);
  127. }
  128. }