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: * Class CommunicatorLoggerHelper
11: *
12: * @package Worldline\Connect\Sdk\Communication
13: */
14: class CommunicatorLoggerHelper
15: {
16: /** @var HttpObfuscator|null */
17: private $httpObfuscator = null;
18:
19: /**
20: * @param CommunicatorLogger $communicatorLogger
21: * @param string $requestId
22: * @param string $requestMethod
23: * @param string $requestUri
24: * @param array $requestHeaders
25: * @param string $requestBody
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: * @param CommunicatorLogger $communicatorLogger
51: * @param string $requestId
52: * @param string $requestUri
53: * @param ConnectionResponse $response
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: * @param CommunicatorLogger $communicatorLogger
67: * @param string $requestId
68: * @param string $requestUri
69: * @param Exception $exception
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: /** @return HttpObfuscator */
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: * @param BodyObfuscator $bodyObfuscator
91: */
92: public function setBodyObfuscator(BodyObfuscator $bodyObfuscator)
93: {
94: $this->getHttpObfuscator()->setBodyObfuscator($bodyObfuscator);
95: }
96:
97: /**
98: * @param HeaderObfuscator $headerObfuscator
99: */
100: public function setHeaderObfuscator(HeaderObfuscator $headerObfuscator)
101: {
102: $this->getHttpObfuscator()->setHeaderObfuscator($headerObfuscator);
103: }
104:
105: /**
106: * @param string $requestUri
107: * @return string
108: */
109: public function getEndpoint($requestUri)
110: {
111: $index = strpos($requestUri, '://');
112: if ($index !== false) {
113: $index = strpos($requestUri, '/', $index + 3);
114: // $index === false means there's no / after the host; there is no relative URI
115: return $index !== false ? substr($requestUri, 0, $index) : $requestUri;
116: } else {
117: // not an absolute URI
118: return '';
119: }
120: }
121:
122: /**
123: * @param string $requestUri
124: * @return string
125: */
126: public function getRelativeUriPathWithRequestParameters($requestUri)
127: {
128: $index = strpos($requestUri, '://');
129: if ($index !== false) {
130: $index = strpos($requestUri, '/', $index + 3);
131: // $index === false means there's no / after the host; there is no relative URI
132: return $index !== false ? substr($requestUri, $index) : '';
133: } else {
134: // not an absolute URI
135: return $requestUri;
136: }
137: }
138: }
139: