TaxRegistry.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\Framework\DataObject;
  8. use Magento\Framework\DataObjectFactory;
  9. use Vertex\Tax\Model\TaxRegistry\StorageInterface;
  10. use Vertex\Tax\Model\TaxQuote\TaxQuoteResponse;
  11. /**
  12. * Provide proprietary storage for Vertex tax information.
  13. */
  14. class TaxRegistry
  15. {
  16. const KEY_TAXES = 'vertex_tax_response';
  17. const KEY_ERROR_GENERIC = 'vertex_error_generic';
  18. const LIFETIME_DEFAULT = 300;
  19. /** @var DataObjectFactory */
  20. private $dataObjectFactory;
  21. /** @var StorageInterface */
  22. private $storage;
  23. /**
  24. * In-object storage of error messages to persist for the life of the request.
  25. *
  26. * @var array
  27. */
  28. private $errorInfo = [];
  29. /**
  30. * In-object storage of calculated taxes to persist for the life of the request.
  31. *
  32. * @var DataObject[]
  33. */
  34. private $calculatedTaxInfo;
  35. /**
  36. * @param DataObjectFactory $dataObjectFactory,
  37. * @param StorageInterface $storage
  38. */
  39. public function __construct(
  40. DataObjectFactory $dataObjectFactory,
  41. StorageInterface $storage
  42. ) {
  43. $this->dataObjectFactory = $dataObjectFactory;
  44. $this->storage = $storage;
  45. }
  46. /**
  47. * Determine whether error information is available.
  48. *
  49. * @param string $type
  50. * @return bool
  51. */
  52. public function hasError($type = self::KEY_ERROR_GENERIC)
  53. {
  54. return !empty($this->errorInfo[$type]);
  55. }
  56. /**
  57. * Determine whether calculated tax data is available.
  58. *
  59. * @return bool
  60. */
  61. public function hasTaxes()
  62. {
  63. return !empty($this->calculatedTaxInfo);
  64. }
  65. /**
  66. * Attempt to retrieve an item from the registry by key.
  67. *
  68. * @param string $key
  69. * @return mixed
  70. */
  71. public function lookup($key)
  72. {
  73. return $this->storage->get($key);
  74. }
  75. /**
  76. * Retrieve calculated tax data from the registry.
  77. *
  78. * @return DataObject[]|null
  79. */
  80. public function lookupTaxes()
  81. {
  82. return $this->calculatedTaxInfo ?: null;
  83. }
  84. /**
  85. * Retrieve stored error message.
  86. *
  87. * @param string $type
  88. * @return string|null
  89. */
  90. public function lookupError($type = self::KEY_ERROR_GENERIC)
  91. {
  92. return !empty($this->errorInfo[$type]) ? $this->errorInfo[$type] : null;
  93. }
  94. /**
  95. * Store information in the registry.
  96. *
  97. * @param string $key
  98. * @param mixed $value
  99. * @param int $lifetime
  100. * @return bool
  101. */
  102. public function register($key, $value, $lifetime = self::LIFETIME_DEFAULT)
  103. {
  104. return $this->storage->set($key, $value, $lifetime);
  105. }
  106. /**
  107. * Register an error message.
  108. *
  109. * @param string $message
  110. * @param string $type
  111. * @return bool
  112. */
  113. public function registerError($message, $type = self::KEY_ERROR_GENERIC)
  114. {
  115. $this->errorInfo[$type] = $message;
  116. return true;
  117. }
  118. /**
  119. * Store calculated tax data in its registry slot.
  120. *
  121. * @param TaxQuoteResponse $taxInfo
  122. * @return bool
  123. */
  124. public function registerTaxes(TaxQuoteResponse $taxInfo)
  125. {
  126. $this->calculatedTaxInfo = $taxInfo->getQuoteTaxedItems();
  127. return !empty($this->calculatedTaxInfo);
  128. }
  129. /**
  130. * Remove information from the registry.
  131. *
  132. * @param string $key
  133. * @return bool
  134. */
  135. public function unregister($key)
  136. {
  137. return $this->storage->unsetData($key);
  138. }
  139. /**
  140. * Remove calculated tax data from its registry slot.
  141. *
  142. * @return bool
  143. */
  144. public function unregisterError($type = null)
  145. {
  146. if ($type === null) {
  147. $this->errorInfo = [];
  148. } elseif (isset($this->errorInfo[$type])) {
  149. $this->errorInfo[$type] = null;
  150. } else {
  151. return false;
  152. }
  153. return true;
  154. }
  155. /**
  156. * Remove calculated tax data from its registry slot.
  157. *
  158. * @return bool
  159. */
  160. public function unregisterTaxes()
  161. {
  162. $this->calculatedTaxInfo = null;
  163. return empty($this->calculatedTaxInfo);
  164. }
  165. }