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: /**
17: * @var HttpObfuscator|null
18: */
19: private ?HttpObfuscator $httpObfuscator = null;
20:
21: /**
22: * @param CommunicatorLogger $communicatorLogger
23: * @param string $requestId
24: * @param string $requestMethod
25: * @param string $requestUri
26: * @param array $requestHeaders
27: * @param string $requestBody
28: *
29: * @return void
30: */
31: public function logRequest(
32: CommunicatorLogger $communicatorLogger,
33: string $requestId,
34: string $requestMethod,
35: string $requestUri,
36: array $requestHeaders,
37: string $requestBody = ''
38: ): void {
39: $communicatorLogger->log(
40: sprintf(
41: "Outgoing request to %s (requestId='%s')\n%s",
42: $this->getEndpoint($requestUri),
43: $requestId,
44: $this->getHttpObfuscator()->getRawObfuscatedRequest(
45: $requestMethod,
46: $this->getRelativeUriPathWithRequestParameters($requestUri),
47: $requestHeaders,
48: $requestBody
49: )
50: )
51: );
52: }
53:
54: /**
55: * @param CommunicatorLogger $communicatorLogger
56: * @param string $requestId
57: * @param string $requestUri
58: * @param ConnectionResponse $response
59: *
60: * @return void
61: */
62: public function logResponse(
63: CommunicatorLogger $communicatorLogger,
64: string $requestId,
65: string $requestUri,
66: ConnectionResponse $response
67: ): void {
68: $communicatorLogger->log(
69: sprintf(
70: "Incoming response from %s (requestId='%s')\n%s",
71: $this->getEndpoint($requestUri),
72: $requestId,
73: $this->getHttpObfuscator()->getRawObfuscatedResponse($response)
74: )
75: );
76: }
77:
78: /**
79: * @param CommunicatorLogger $communicatorLogger
80: * @param string $requestId
81: * @param string $requestUri
82: * @param Exception $exception
83: *
84: * @return void
85: */
86: public function logException(
87: CommunicatorLogger $communicatorLogger,
88: string $requestId,
89: string $requestUri,
90: Exception $exception
91: ): void {
92: $communicatorLogger->logException(
93: sprintf(
94: "Error occurred while executing request to %s (requestId='%s')",
95: $this->getEndpoint($requestUri),
96: $requestId
97: ),
98: $exception
99: );
100: }
101:
102: /**
103: * @return HttpObfuscator
104: */
105: protected function getHttpObfuscator(): HttpObfuscator
106: {
107: if (is_null($this->httpObfuscator)) {
108: $this->httpObfuscator = new HttpObfuscator();
109: }
110: return $this->httpObfuscator;
111: }
112:
113: /**
114: * @param BodyObfuscator $bodyObfuscator
115: *
116: * @return void
117: */
118: public function setBodyObfuscator(BodyObfuscator $bodyObfuscator): void
119: {
120: $this->getHttpObfuscator()->setBodyObfuscator($bodyObfuscator);
121: }
122:
123: /**
124: * @param HeaderObfuscator $headerObfuscator
125: *
126: * @return void
127: */
128: public function setHeaderObfuscator(HeaderObfuscator $headerObfuscator): void
129: {
130: $this->getHttpObfuscator()->setHeaderObfuscator($headerObfuscator);
131: }
132:
133: /**
134: * @param string $requestUri
135: *
136: * @return string
137: */
138: public function getEndpoint(string $requestUri): string
139: {
140: $index = strpos($requestUri, '://');
141: if ($index !== false) {
142: $index = strpos($requestUri, '/', $index + 3);
143: // $index === false means there's no / after the host; there is no relative URI
144: return $index !== false ? substr($requestUri, 0, $index) : $requestUri;
145: } else {
146: // not an absolute URI
147: return '';
148: }
149: }
150:
151: /**
152: * @param string $requestUri
153: *
154: * @return string
155: */
156: public function getRelativeUriPathWithRequestParameters(string $requestUri): string
157: {
158: $index = strpos($requestUri, '://');
159: if ($index !== false) {
160: $index = strpos($requestUri, '/', $index + 3);
161: // $index === false means there's no / after the host; there is no relative URI
162: return $index !== false ? substr($requestUri, $index) : '';
163: } else {
164: // not an absolute URI
165: return $requestUri;
166: }
167: }
168: }
169: