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: $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: * @param CommunicatorLogger $communicatorLogger
50: * @param string $requestId
51: * @param string $requestUri
52: * @param ConnectionResponse $response
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: * @param CommunicatorLogger $communicatorLogger
66: * @param string $requestId
67: * @param string $requestUri
68: * @param Exception $exception
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: /** @return HttpObfuscator */
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: * @param BodyObfuscator $bodyObfuscator
90: */
91: public function setBodyObfuscator(BodyObfuscator $bodyObfuscator)
92: {
93: $this->getHttpObfuscator()->setBodyObfuscator($bodyObfuscator);
94: }
95:
96: /**
97: * @param HeaderObfuscator $headerObfuscator
98: */
99: public function setHeaderObfuscator(HeaderObfuscator $headerObfuscator)
100: {
101: $this->getHttpObfuscator()->setHeaderObfuscator($headerObfuscator);
102: }
103:
104: /**
105: * @param string $requestUri
106: * @return string
107: */
108: public function getEndpoint($requestUri)
109: {
110: $index = strpos($requestUri, '://');
111: if ($index !== false) {
112: $index = strpos($requestUri, '/', $index + 3);
113: // $index === false means there's no / after the host; there is no relative URI
114: return $index !== false ? substr($requestUri, 0, $index) : $requestUri;
115: } else {
116: // not an absolute URI
117: return '';
118: }
119: }
120:
121: /**
122: * @param string $requestUri
123: * @return string
124: */
125: public function getRelativeUriPathWithRequestParameters($requestUri)
126: {
127: $index = strpos($requestUri, '://');
128: if ($index !== false) {
129: $index = strpos($requestUri, '/', $index + 3);
130: // $index === false means there's no / after the host; there is no relative URI
131: return $index !== false ? substr($requestUri, $index) : '';
132: } else {
133: // not an absolute URI
134: return $requestUri;
135: }
136: }
137: }
138: