/** * Cache validated response * * @param ValidateRequest $validateRequest * @param $storeId * @return ValidateResult * @throws \SoapFault */ public function validate(ValidateRequest $validateRequest, $storeId) { $addressCacheKey = $this->getCacheKey($validateRequest->getAddress()) . $storeId; $validateResult = @unserialize($this->cache->load($addressCacheKey)); if ($validateResult instanceof ValidateResult) { $this->avaTaxLogger->addDebug('Loaded \\AvaTax\\ValidateResult from cache.', ['request' => var_export($validateRequest, true), 'result' => var_export($validateResult, true), 'cache_key' => $addressCacheKey]); return $validateResult; } try { $addressService = $this->interactionAddress->getAddressService($this->type, $storeId); $validateResult = $addressService->validate($validateRequest); $serializedValidateResult = serialize($validateResult); $this->cache->save($serializedValidateResult, $addressCacheKey, [Config::AVATAX_CACHE_TAG]); $validAddress = isset($validateResult->getValidAddresses()[0]) ? $validateResult->getValidAddresses()[0] : null; $validAddressCacheKey = $this->getCacheKey($validAddress); $this->avaTaxLogger->addDebug('Loaded \\AvaTax\\ValidateResult from SOAP.', ['request' => var_export($validateRequest, true), 'result' => var_export($validateResult, true)]); $this->cache->save($serializedValidateResult, $validAddressCacheKey, [Config::AVATAX_CACHE_TAG]); } catch (LocalizedException $e) { $this->avaTaxLogger->addDebug('\\AvaTax\\ValidateResult no valid address found from SOAP.', ['result' => var_export($validateResult, true)]); } catch (\SoapFault $e) { $this->avaTaxLogger->error("Exception: \n" . $e->getMessage() . "\n" . $e->faultstring, ['request' => var_export($addressService->__getLastRequest(), true), 'result' => var_export($addressService->__getLastResponse(), true)]); throw $e; } return $validateResult; }
/** * Cache validated response * * @param GetTaxRequest $getTaxRequest * @param $storeId * @param bool $useCache * @return GetTaxResult * @throws LocalizedException */ public function getTax(GetTaxRequest $getTaxRequest, $storeId, $useCache = false) { $cacheKey = $this->getCacheKey($getTaxRequest) . $storeId; $getTaxResult = @unserialize($this->cache->load($cacheKey)); if ($getTaxResult instanceof GetTaxResult && $useCache) { $this->avaTaxLogger->addDebug('Loaded \\AvaTax\\GetTaxResult from cache.', ['result' => var_export($getTaxResult, true), 'cache_key' => $cacheKey]); return $getTaxResult; } $getTaxResult = $this->taxInteraction->getTaxService($this->type, $storeId)->getTax($getTaxRequest); $this->avaTaxLogger->addDebug('Loaded \\AvaTax\\GetTaxResult from SOAP.', ['request' => var_export($getTaxRequest, true), 'result' => var_export($getTaxResult, true)]); // Only cache successful requests if ($useCache && $getTaxResult->getResultCode() == \AvaTax\SeverityLevel::$Success) { $serializedGetTaxResult = serialize($getTaxResult); $this->cache->save($serializedGetTaxResult, $cacheKey, [Config::AVATAX_CACHE_TAG], self::CACHE_LIFETIME); } return $getTaxResult; }