1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EightPoints\Bundle\GuzzleBundle\Log; |
4
|
|
|
|
5
|
|
|
use EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundle; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
|
8
|
|
|
class LogResponse |
9
|
|
|
{ |
10
|
|
|
/** @var integer */ |
11
|
|
|
protected $statusCode; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
protected $statusPhrase; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $body; |
18
|
|
|
|
19
|
|
|
/** @var string[][] */ |
20
|
|
|
protected $headers = []; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $protocolVersion; |
24
|
|
|
|
25
|
|
|
/** @var bool */ |
26
|
|
|
private $logBody; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
30
|
|
|
* @param bool $logBody |
31
|
|
|
*/ |
32
|
|
|
public function __construct(ResponseInterface $response, bool $logBody = true) |
33
|
|
|
{ |
34
|
|
|
$this->logBody = $logBody; |
35
|
|
|
$this->save($response); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Save data |
40
|
|
|
* |
41
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function save(ResponseInterface $response) : void |
46
|
|
|
{ |
47
|
|
|
$this->setStatusCode($response->getStatusCode()); |
48
|
|
|
$this->setStatusPhrase($response->getReasonPhrase()); |
49
|
|
|
|
50
|
|
|
$this->setHeaders($response->getHeaders()); |
51
|
|
|
$this->setProtocolVersion($response->getProtocolVersion()); |
52
|
|
|
|
53
|
|
|
if ($this->logBody) { |
54
|
|
|
$this->setBody($response->getBody()->getContents()); |
55
|
|
|
|
56
|
|
|
// rewind to previous position after reading response body |
57
|
|
|
if ($response->getBody()->isSeekable()) { |
58
|
|
|
$response->getBody()->rewind(); |
59
|
|
|
} |
60
|
|
|
} else { |
61
|
|
|
$this->setBody(EightPointsGuzzleBundle::class . ': [response body log disabled]'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return HTTP status code |
67
|
|
|
* |
68
|
|
|
* @return integer |
69
|
|
|
*/ |
70
|
|
|
public function getStatusCode() : int |
71
|
|
|
{ |
72
|
|
|
return $this->statusCode; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set HTTP status code |
77
|
|
|
* |
78
|
|
|
* @param integer $value |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function setStatusCode(int $value) : void |
83
|
|
|
{ |
84
|
|
|
$this->statusCode = $value; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return HTTP status phrase |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getStatusPhrase() : string |
93
|
|
|
{ |
94
|
|
|
return $this->statusPhrase; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set HTTP status phrase |
99
|
|
|
* |
100
|
|
|
* @param string $value |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
public function setStatusPhrase(string $value) : void |
105
|
|
|
{ |
106
|
|
|
$this->statusPhrase = $value; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return response body |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getBody() |
115
|
|
|
{ |
116
|
|
|
return $this->body; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set response body |
121
|
|
|
* |
122
|
|
|
* @param string $value |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function setBody(string $value) : void |
127
|
|
|
{ |
128
|
|
|
$this->body = $value; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Return protocol version |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getProtocolVersion() : string |
137
|
|
|
{ |
138
|
|
|
return $this->protocolVersion; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set protocol version |
143
|
|
|
* |
144
|
|
|
* @param string $value |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
public function setProtocolVersion(string $value) : void |
149
|
|
|
{ |
150
|
|
|
$this->protocolVersion = $value; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Return response headers |
155
|
|
|
* |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
|
|
public function getHeaders() : array |
159
|
|
|
{ |
160
|
|
|
return $this->headers; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Set response headers |
165
|
|
|
* |
166
|
|
|
* @param array $value |
167
|
|
|
* |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
public function setHeaders(array $value) : void |
171
|
|
|
{ |
172
|
|
|
$this->headers = $value; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|