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