quote = $quote; $this->cacheKeyGenerator = $cacheKeyGenerator; $this->taxRegistry = $taxRegistry; $this->logger = $logger; $this->mapperFactory = $mapperFactory; } /** * Perform a Quotation Request * * @param RequestInterface $request * @param string|null $scopeCode * @return ResponseInterface|bool * @throws ApiException * @throws ValidationException * @throws ConfigurationException */ public function taxQuote(RequestInterface $request, $scopeCode = null) { $cacheKey = false; $response = false; try { $cacheKey = $this->cacheKeyGenerator->generateCacheKey($request); } catch (\Exception $e) { $this->logger->warning($e); } if ($cacheKey !== false) { try { $response = $this->getCachedResponse($cacheKey, $scopeCode); } catch (\Exception $e) { $this->logger->warning($e); } } if (!$response) { try { $response = $this->quote->request($request, $scopeCode); } catch (\Exception $e) { $this->logger->critical($e); throw $e; } $this->registerResponseInCache($cacheKey, $response, $scopeCode); } return $response; } /** * Retrieve the Response from the Cache * * @param string $cacheKey * @param string|null $scopeCode Store ID * @return ResponseInterface|bool */ private function getCachedResponse($cacheKey, $scopeCode = null) { try { /** @var QuoteResponseMapperInterface $mapper */ $mapper = $this->mapperFactory->getForClass(ResponseInterface::class, $scopeCode); } catch (\Exception $e) { $this->logger->warning($e); return false; } $mappedResponse = $this->taxRegistry->lookup($cacheKey); return $mappedResponse !== null ? $mapper->build($mappedResponse) : false; } /** * Register the Response in the Cache * * @param string $cacheKey * @param ResponseInterface $response * @param string|null $scopeCode Store ID * @return void */ private function registerResponseInCache($cacheKey, ResponseInterface $response, $scopeCode = null) { try { /** @var QuoteResponseMapperInterface $mapper */ $mapper = $this->mapperFactory->getForClass(ResponseInterface::class, $scopeCode); $mappedResponse = $mapper->map($response); } catch (\Exception $e) { $this->logger->warning($e); return; } $this->taxRegistry->register($cacheKey, $mappedResponse); } }