CacheKeyGenerator.php 1.42 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<?php
/**
 * @copyright  Vertex. All rights reserved.  https://www.vertexinc.com/
 * @author     Mediotype                     https://www.mediotype.com/
 */

namespace Vertex\Tax\Model\TaxQuote;

use Magento\Store\Model\ScopeInterface;
use Vertex\Mapper\QuoteRequestMapperInterface;
use Vertex\Services\Quote\RequestInterface;
use Vertex\Tax\Model\Api\Utility\MapperFactoryProxy;

/**
 * Generates a cache storage key for a Quotation Request
 */
class CacheKeyGenerator
{
    /** @var MapperFactoryProxy */
    private $mapperFactory;

    /**
     * @param MapperFactoryProxy $mapperFactory
     */
    public function __construct(MapperFactoryProxy $mapperFactory)
    {
        $this->mapperFactory = $mapperFactory;
    }

    /**
     * Convert a Tax Quote Request into a string for caching
     *
     * @param RequestInterface $request
     * @param string|null $scopeCode Store ID
     * @param string $scopeType Scope Type
     * @return string
     * @throws \Vertex\Exception\ValidationException
     * @throws \Vertex\Exception\ConfigurationException
     */
    public function generateCacheKey(
        RequestInterface $request,
        $scopeCode = null,
        $scopeType = ScopeInterface::SCOPE_STORE
    ) {
        /** @var QuoteRequestMapperInterface $mapper */
        $mapper = $this->mapperFactory->getForClass(RequestInterface::class, $scopeCode, $scopeType);
        return sha1(json_encode($mapper->map($request)));
    }
}