CacheKeyGenerator.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 Magento\Store\Model\ScopeInterface;
  8. use Vertex\Mapper\QuoteRequestMapperInterface;
  9. use Vertex\Services\Quote\RequestInterface;
  10. use Vertex\Tax\Model\Api\Utility\MapperFactoryProxy;
  11. /**
  12. * Generates a cache storage key for a Quotation Request
  13. */
  14. class CacheKeyGenerator
  15. {
  16. /** @var MapperFactoryProxy */
  17. private $mapperFactory;
  18. /**
  19. * @param MapperFactoryProxy $mapperFactory
  20. */
  21. public function __construct(MapperFactoryProxy $mapperFactory)
  22. {
  23. $this->mapperFactory = $mapperFactory;
  24. }
  25. /**
  26. * Convert a Tax Quote Request into a string for caching
  27. *
  28. * @param RequestInterface $request
  29. * @param string|null $scopeCode Store ID
  30. * @param string $scopeType Scope Type
  31. * @return string
  32. * @throws \Vertex\Exception\ValidationException
  33. * @throws \Vertex\Exception\ConfigurationException
  34. */
  35. public function generateCacheKey(
  36. RequestInterface $request,
  37. $scopeCode = null,
  38. $scopeType = ScopeInterface::SCOPE_STORE
  39. ) {
  40. /** @var QuoteRequestMapperInterface $mapper */
  41. $mapper = $this->mapperFactory->getForClass(RequestInterface::class, $scopeCode, $scopeType);
  42. return sha1(json_encode($mapper->map($request)));
  43. }
  44. }