Logger.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model\Api;
  7. use Magento\Framework\Exception\CouldNotSaveException;
  8. use Vertex\Tax\Model\Api\Utility\SoapClientRegistry;
  9. use Vertex\Tax\Model\Config;
  10. use Vertex\Tax\Model\ExceptionLogger;
  11. use Vertex\Tax\Model\RequestLogger;
  12. /**
  13. * Contains functionality for logging API calls
  14. */
  15. class Logger
  16. {
  17. /** @var Config */
  18. private $config;
  19. /** @var ExceptionLogger */
  20. private $logger;
  21. /** @var RequestLogger */
  22. private $requestLogger;
  23. /** @var SoapClientRegistry */
  24. private $soapClientRegistry;
  25. /**
  26. * @param ExceptionLogger $logger
  27. * @param RequestLogger $requestLogger
  28. * @param SoapClientRegistry $soapClientRegistry
  29. * @param Config $config
  30. */
  31. public function __construct(
  32. ExceptionLogger $logger,
  33. RequestLogger $requestLogger,
  34. SoapClientRegistry $soapClientRegistry,
  35. Config $config
  36. ) {
  37. $this->logger = $logger;
  38. $this->requestLogger = $requestLogger;
  39. $this->soapClientRegistry = $soapClientRegistry;
  40. $this->config = $config;
  41. }
  42. /**
  43. * Wrap an API call to ensure it is logged
  44. *
  45. * @param callable $callable
  46. * @param string $type
  47. * @param string|null $scopeCode Store ID
  48. * @return mixed Result of callable
  49. * @throws \Exception
  50. */
  51. public function wrapCall(callable $callable, $type, $scopeCode = null)
  52. {
  53. try {
  54. return $callable();
  55. } catch (\Exception $exception) {
  56. $this->logException($exception);
  57. throw $exception;
  58. } finally {
  59. $this->logRequest($type, $scopeCode);
  60. }
  61. }
  62. /**
  63. * Log an Exception
  64. *
  65. * @param \Exception $exception
  66. * @return void
  67. */
  68. private function logException(\Exception $exception)
  69. {
  70. $this->logger->critical($exception);
  71. }
  72. /**
  73. * Log an API call to the database
  74. *
  75. * @param string $requestType
  76. * @param string|null $scopeCode Store ID
  77. * @return void
  78. */
  79. private function logRequest($requestType, $scopeCode = null)
  80. {
  81. if (!$this->config->isLoggingEnabled($scopeCode)) {
  82. return;
  83. }
  84. $soapClient = $this->soapClientRegistry->getLastClient();
  85. try {
  86. $this->requestLogger->log(
  87. $requestType,
  88. $soapClient ? $soapClient->__getLastRequest() : null,
  89. $soapClient ? $soapClient->__getLastResponse() : null
  90. );
  91. } catch (CouldNotSaveException $originalException) {
  92. $loggedException = new \Exception('Failed to log Vertex Request', 0, $originalException);
  93. $this->logException($loggedException);
  94. }
  95. }
  96. }