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