1: <?php
2: namespace Worldline\Connect\Sdk\Communication;
3:
4: use Worldline\Connect\Sdk\Logging\BodyObfuscator;
5: use Worldline\Connect\Sdk\Logging\HeaderObfuscator;
6:
7: /**
8: * Class HttpObfuscator
9: *
10: * @package Worldline\Connect\Sdk\Communication
11: */
12: class HttpObfuscator
13: {
14: const HTTP_VERSION = 'HTTP/1.1';
15:
16: /** @var HeaderObfuscator */
17: protected $headerObfuscator;
18:
19: /** @var BodyObfuscator */
20: protected $bodyObfuscator;
21:
22: public function __construct()
23: {
24: $this->headerObfuscator = new HeaderObfuscator();
25: $this->bodyObfuscator = new BodyObfuscator();
26: }
27:
28: /**
29: * @param BodyObfuscator $bodyObfuscator
30: */
31: public function setBodyObfuscator(BodyObfuscator $bodyObfuscator)
32: {
33: $this->bodyObfuscator = $bodyObfuscator;
34: }
35:
36: /**
37: * @param HeaderObfuscator $headerObfuscator
38: */
39: public function setHeaderObfuscator(HeaderObfuscator $headerObfuscator)
40: {
41: $this->headerObfuscator = $headerObfuscator;
42: }
43:
44: /**
45: * @param string $requestMethod
46: * @param string $relativeRequestUri
47: * @param array $requestHeaders
48: * @param string $requestBody
49: * @return string
50: */
51: public function getRawObfuscatedRequest(
52: $requestMethod,
53: $relativeRequestUri,
54: array $requestHeaders,
55: $requestBody = ''
56: ) {
57: $rawObfuscatedRequest = $requestMethod . ' ' . $relativeRequestUri . ' ' . static::HTTP_VERSION;
58: if ($requestHeaders) {
59: $rawObfuscatedRequest .= PHP_EOL . implode(PHP_EOL, HttpHeaderHelper::generateRawHeaders(
60: $this->headerObfuscator->obfuscateHeaders($requestHeaders)
61: ));
62: }
63: if (strlen($requestBody) > 0) {
64: $rawObfuscatedRequest .= PHP_EOL . PHP_EOL . $this->bodyObfuscator->obfuscateBody(
65: array_key_exists('Content-Type', $requestHeaders) ? $requestHeaders['Content-Type'] : '',
66: $requestBody
67: );
68: }
69: return $rawObfuscatedRequest;
70: }
71:
72: /**
73: * @param ConnectionResponse $response
74: * @return string
75: */
76: public function getRawObfuscatedResponse(ConnectionResponse $response)
77: {
78: $rawObfuscatedResponse = '';
79: $responseHeaders = $response->getHeaders();
80: if ($responseHeaders) {
81: $rawObfuscatedResponse .= implode(PHP_EOL, HttpHeaderHelper::generateRawHeaders(
82: $this->headerObfuscator->obfuscateHeaders($responseHeaders)
83: ));
84: }
85: $responseBody = $response->getBody();
86: if (strlen($responseBody) > 0) {
87: $rawObfuscatedResponse .= PHP_EOL . PHP_EOL . $this->bodyObfuscator->obfuscateBody(
88: $response->getHeaderValue('Content-Type'),
89: $responseBody
90: );
91: }
92: return $rawObfuscatedResponse;
93: }
94: }
95: