HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/magento.bikenow.co/vendor/vertex/module-tax/Model/Api/Logger.php
<?php
/**
 * @copyright  Vertex. All rights reserved.  https://www.vertexinc.com/
 * @author     Mediotype                     https://www.mediotype.com/
 */

namespace Vertex\Tax\Model\Api;

use Magento\Framework\Exception\CouldNotSaveException;
use Vertex\Services\SoapCallResponseInterface;
use Vertex\Tax\Model\Api\Utility\SoapClientRegistry;
use Vertex\Tax\Model\Config;
use Vertex\Tax\Model\ExceptionLogger;
use Vertex\Tax\Model\RequestLogger;

/**
 * Contains functionality for logging API calls
 */
class Logger
{
    /** @var Config */
    private $config;

    /** @var ExceptionLogger */
    private $logger;

    /** @var RequestLogger */
    private $requestLogger;

    /** @var SoapClientRegistry */
    private $soapClientRegistry;

    /**
     * @param ExceptionLogger $logger
     * @param RequestLogger $requestLogger
     * @param SoapClientRegistry $soapClientRegistry
     * @param Config $config
     */
    public function __construct(
        ExceptionLogger $logger,
        RequestLogger $requestLogger,
        SoapClientRegistry $soapClientRegistry,
        Config $config
    ) {
        $this->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)
    {
        $result = null;
        try {
            $result = $callable();
            return $result;
        } catch (\Exception $exception) {
            $this->logException($exception);
            throw $exception;
        } finally {
            $this->logRequest($type, $result, $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 mixed $result
     * @param string|null $scopeCode Store ID
     * @return void
     */
    private function logRequest($requestType, $result, $scopeCode = null)
    {
        if (!$this->config->isLoggingEnabled($scopeCode)) {
            return;
        }

        $responseTime = $result instanceof SoapCallResponseInterface ? $result->getHttpCallTime() : null;

        $soapClient = $this->soapClientRegistry->getLastClient();
        try {
            $this->requestLogger->log(
                $requestType,
                $soapClient ? $soapClient->__getLastRequest() : null,
                $soapClient ? $soapClient->__getLastResponse() : null,
                $responseTime
            );
        } catch (CouldNotSaveException $originalException) {
            $loggedException = new \Exception('Failed to log Vertex Request', 0, $originalException);
            $this->logException($loggedException);
        }
    }
}