1: | <?php |
2: | namespace Worldline\Acquiring\Sdk\Communication; |
3: | |
4: | use Exception; |
5: | use Worldline\Acquiring\Sdk\Logging\BodyObfuscator; |
6: | use Worldline\Acquiring\Sdk\Logging\CommunicatorLogger; |
7: | use Worldline\Acquiring\Sdk\Logging\HeaderObfuscator; |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | class CommunicatorLoggerHelper |
15: | { |
16: | |
17: | private $httpObfuscator = null; |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | public function logRequest( |
28: | CommunicatorLogger $communicatorLogger, |
29: | $requestId, |
30: | $requestMethod, |
31: | $requestUri, |
32: | array $requestHeaders, |
33: | $requestBody = '' |
34: | ) { |
35: | $communicatorLogger->log(sprintf( |
36: | "Outgoing request to %s (requestId='%s')\n%s", |
37: | $this->getEndpoint($requestUri), |
38: | $requestId, |
39: | $this->getHttpObfuscator()->getRawObfuscatedRequest( |
40: | $requestMethod, |
41: | $this->getRelativeUriPathWithRequestParameters($requestUri), |
42: | $requestHeaders, |
43: | $requestBody |
44: | ) |
45: | )); |
46: | } |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | |
53: | |
54: | public function logResponse(CommunicatorLogger $communicatorLogger, $requestId, $requestUri, ConnectionResponse $response) |
55: | { |
56: | $communicatorLogger->log(sprintf( |
57: | "Incoming response from %s (requestId='%s')\n%s", |
58: | $this->getEndpoint($requestUri), |
59: | $requestId, |
60: | $this->getHttpObfuscator()->getRawObfuscatedResponse($response) |
61: | )); |
62: | } |
63: | |
64: | |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | public function logException(CommunicatorLogger $communicatorLogger, $requestId, $requestUri, Exception $exception) |
71: | { |
72: | $communicatorLogger->logException(sprintf( |
73: | "Error occurred while executing request to %s (requestId='%s')", |
74: | $this->getEndpoint($requestUri), |
75: | $requestId |
76: | ), $exception); |
77: | } |
78: | |
79: | |
80: | protected function getHttpObfuscator() |
81: | { |
82: | if (is_null($this->httpObfuscator)) { |
83: | $this->httpObfuscator = new HttpObfuscator(); |
84: | } |
85: | return $this->httpObfuscator; |
86: | } |
87: | |
88: | |
89: | |
90: | |
91: | public function setBodyObfuscator(BodyObfuscator $bodyObfuscator) |
92: | { |
93: | $this->getHttpObfuscator()->setBodyObfuscator($bodyObfuscator); |
94: | } |
95: | |
96: | |
97: | |
98: | |
99: | public function setHeaderObfuscator(HeaderObfuscator $headerObfuscator) |
100: | { |
101: | $this->getHttpObfuscator()->setHeaderObfuscator($headerObfuscator); |
102: | } |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | public function getEndpoint($requestUri) |
109: | { |
110: | $index = strpos($requestUri, '://'); |
111: | if ($index !== FALSE) { |
112: | $index = strpos($requestUri, '/', $index + 3); |
113: | |
114: | return $index !== FALSE ? substr($requestUri, 0, $index) : $requestUri; |
115: | } else { |
116: | |
117: | return ''; |
118: | } |
119: | } |
120: | |
121: | |
122: | |
123: | |
124: | |
125: | public function getRelativeUriPathWithRequestParameters($requestUri) |
126: | { |
127: | $index = strpos($requestUri, '://'); |
128: | if ($index !== FALSE) { |
129: | $index = strpos($requestUri, '/', $index + 3); |
130: | |
131: | return $index !== FALSE ? substr($requestUri, $index) : ''; |
132: | } else { |
133: | |
134: | return $requestUri; |
135: | } |
136: | } |
137: | } |
138: | |