Log.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Model\SignifydGateway\Debugger;
  7. use Psr\Log\LoggerInterface;
  8. use Exception;
  9. /**
  10. * Debugger writes information about request, response and possible exception to standard system log.
  11. */
  12. class Log implements DebuggerInterface
  13. {
  14. /**
  15. * @var LoggerInterface
  16. */
  17. private $logger;
  18. /**
  19. * Log constructor.
  20. *
  21. * @param LoggerInterface $logger
  22. */
  23. public function __construct(LoggerInterface $logger)
  24. {
  25. $this->logger = $logger;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function success($requestUrl, $requestData, $responseStatus, $responseBody)
  31. {
  32. $requestInfo = $this->buildRequestInfo($requestUrl, $requestData);
  33. $responseInfo = $this->buildResponseInfo($responseStatus, $responseBody);
  34. $info = $requestInfo
  35. . $responseInfo;
  36. $this->writeToLog($info);
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function failure($requestUrl, $requestData, Exception $exception)
  42. {
  43. $requestInfo = $this->buildRequestInfo($requestUrl, $requestData);
  44. $exceptionInfo = $this->buildExceptionInfo($exception);
  45. $info = $requestInfo
  46. . $exceptionInfo;
  47. $this->writeToLog($info);
  48. }
  49. /**
  50. * Build string with request URL and body
  51. *
  52. * @param string $requestUrl
  53. * @param string $requestData
  54. * @return string
  55. */
  56. private function buildRequestInfo($requestUrl, $requestData)
  57. {
  58. $infoContent = $this->buildInfoSection('URL', $requestUrl)
  59. . $this->buildInfoSection('Body', $requestData);
  60. $info = $this->buildInfoSection('Request', $infoContent);
  61. return $info;
  62. }
  63. /**
  64. * Build string with response status code and body
  65. *
  66. * @param string $responseStatus
  67. * @param string $responseBody
  68. * @return string
  69. */
  70. private function buildResponseInfo($responseStatus, $responseBody)
  71. {
  72. $infoContent = $this->buildInfoSection('Status', $responseStatus)
  73. . $this->buildInfoSection('Body', $responseBody);
  74. $info = $this->buildInfoSection('Response', $infoContent);
  75. return $info;
  76. }
  77. /**
  78. * Build string with exception information
  79. *
  80. * @param Exception $exception
  81. * @return string
  82. */
  83. private function buildExceptionInfo(Exception $exception)
  84. {
  85. $infoContent = (string)$exception;
  86. $info = $this->buildInfoSection('Exception', $infoContent);
  87. return $info;
  88. }
  89. /**
  90. * Write debug information to log file (var/log/debug.log by default)
  91. *
  92. * @param string $info
  93. * @return void
  94. */
  95. private function writeToLog($info)
  96. {
  97. $logMessage = $this->buildInfoSection('Signifyd API integration debug info', $info);
  98. $this->logger->debug($logMessage);
  99. }
  100. /**
  101. * Build unified debug section string
  102. *
  103. * @param string $title
  104. * @param string $content
  105. * @return string
  106. */
  107. private function buildInfoSection($title, $content)
  108. {
  109. $formattedInfo = $title . ":\n"
  110. . $this->addIndent($content) . "\n";
  111. return $formattedInfo;
  112. }
  113. /**
  114. * Add indent to each line in content
  115. *
  116. * @param string $content
  117. * @param string $indent
  118. * @return string
  119. */
  120. private function addIndent($content, $indent = ' ')
  121. {
  122. $contentLines = explode("\n", $content);
  123. $contentLinesWithIndent = array_map(function ($line) use ($indent) {
  124. return $indent . $line;
  125. }, $contentLines);
  126. $content = implode("\n", $contentLinesWithIndent);
  127. return $content;
  128. }
  129. }