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