GwItemKeyManager.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Calculation\VertexCalculator;
  7. use Vertex\Tax\Model\Calculation\VertexCalculator;
  8. use Vertex\Tax\Model\TaxRegistry;
  9. use Magento\Tax\Api\Data\QuoteDetailsItemInterface;
  10. use Magento\Quote\Model\Quote\Item\AbstractItem;
  11. use Magento\GiftWrapping\Model\Total\Quote\Tax\Giftwrapping;
  12. /**
  13. * Storage utility for gift wrapping item-level tax information that persists across a tax calculation cycle.
  14. */
  15. class GwItemKeyManager
  16. {
  17. /** @var TaxRegistry */
  18. private $taxRegistry;
  19. /**
  20. * @param TaxRegistry $taxRegistry
  21. */
  22. public function __construct(TaxRegistry $taxRegistry)
  23. {
  24. $this->taxRegistry = $taxRegistry;
  25. }
  26. /**
  27. * Generate a gift wrapping-item map ID for the given quote item.
  28. *
  29. * @param AbstractItem $item
  30. * @return string
  31. */
  32. public function generateGwItemMapCode(AbstractItem $item)
  33. {
  34. $prefix = class_exists(Giftwrapping::class) ? Giftwrapping::CODE_ITEM_GW_PREFIX : 'item_gw';
  35. return $prefix . '_' . $item->getItemId();
  36. }
  37. /**
  38. * Retrieve the key for the given item.
  39. *
  40. * @param QuoteDetailsItemInterface $item
  41. * @return string|null
  42. */
  43. public function get(QuoteDetailsItemInterface $item)
  44. {
  45. return $this->taxRegistry->lookup($this->generateItemStorageKey($item));
  46. }
  47. /**
  48. * Write the given quote item ID to storage.
  49. *
  50. * @param QuoteDetailsItemInterface $item
  51. * @param AbstractItem $quoteItem
  52. * @return void
  53. */
  54. public function set(QuoteDetailsItemInterface $item, AbstractItem $quoteItem)
  55. {
  56. $cacheKey = $this->generateItemStorageKey($item);
  57. $this->taxRegistry->unregister($cacheKey);
  58. $this->taxRegistry->register($cacheKey, $this->generateGwItemMapCode($quoteItem));
  59. }
  60. /**
  61. * Generate a unique storage key for the given item.
  62. *
  63. * @param QuoteDetailsItemInterface $item
  64. * @return string
  65. */
  66. private function generateItemStorageKey(QuoteDetailsItemInterface $item)
  67. {
  68. return sha1(
  69. 'gw'
  70. . VertexCalculator::VERTEX_QUOTE_ITEM_ID_PREFIX
  71. . $item->getItemId()
  72. . '_'
  73. . $item->getGwId()
  74. );
  75. }
  76. }