logger = $logger; $this->requestLogger = $requestLogger; $this->soapClientRegistry = $soapClientRegistry; $this->config = $config; } /** * Wrap an API call to ensure it is logged * * @param callable $callable * @param string $type * @param string|null $scopeCode Store ID * @return mixed Result of callable * @throws \Exception */ public function wrapCall(callable $callable, $type, $scopeCode = null) { try { return $callable(); } catch (\Exception $exception) { $this->logException($exception); throw $exception; } finally { $this->logRequest($type, $scopeCode); } } /** * Log an Exception * * @param \Exception $exception * @return void */ private function logException(\Exception $exception) { $this->logger->critical($exception); } /** * Log an API call to the database * * @param string $requestType * @param string|null $scopeCode Store ID * @return void */ private function logRequest($requestType, $scopeCode = null) { if (!$this->config->isLoggingEnabled($scopeCode)) { return; } $soapClient = $this->soapClientRegistry->getLastClient(); try { $this->requestLogger->log( $requestType, $soapClient ? $soapClient->__getLastRequest() : null, $soapClient ? $soapClient->__getLastResponse() : null ); } catch (CouldNotSaveException $originalException) { $loggedException = new \Exception('Failed to log Vertex Request', 0, $originalException); $this->logException($loggedException); } } }