1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tomaj\NetteApi\Misc; |
4
|
|
|
|
5
|
|
|
class ConsoleResponse |
6
|
|
|
{ |
7
|
|
|
private $postFields; |
8
|
|
|
|
9
|
|
|
private $getFields; |
10
|
|
|
|
11
|
|
|
private $cookieFields; |
12
|
|
|
|
13
|
|
|
private $url; |
14
|
|
|
|
15
|
|
|
private $method; |
16
|
|
|
|
17
|
|
|
private $headers; |
18
|
|
|
|
19
|
|
|
private $responseCode; |
20
|
|
|
|
21
|
|
|
private $responseBody; |
22
|
|
|
|
23
|
|
|
private $responseHeaders; |
24
|
|
|
|
25
|
|
|
private $responseTime; |
26
|
|
|
|
27
|
|
|
private $isError = false; |
28
|
|
|
|
29
|
|
|
private $errorNumber; |
30
|
|
|
|
31
|
|
|
private $errorMessage; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ConsoleResponse constructor. |
35
|
|
|
* |
36
|
|
|
* @param string $url |
37
|
|
|
* @param string $method |
38
|
|
|
* @param array $postFields |
39
|
|
|
* @param array $getFields |
40
|
|
|
* @param array $cookieFields |
41
|
|
|
* @param array $headers |
42
|
|
|
*/ |
43
|
9 |
|
public function __construct($url, $method, array $postFields = [], array $getFields = [], $cookieFields = [], array $headers = []) |
44
|
|
|
{ |
45
|
9 |
|
$this->url = $url; |
46
|
9 |
|
$this->method = $method; |
47
|
9 |
|
$this->postFields = $postFields; |
48
|
9 |
|
$this->getFields = $getFields; |
49
|
9 |
|
$this->cookieFields = $cookieFields; |
50
|
9 |
|
$this->headers = $headers; |
51
|
9 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Log response from request |
55
|
|
|
* |
56
|
|
|
* @param $responseCode |
57
|
|
|
* @param $responseBody |
58
|
|
|
* @param $responseHeaders |
59
|
|
|
* @param $responseTime |
60
|
|
|
* |
61
|
|
|
* @return voiud |
62
|
|
|
*/ |
63
|
3 |
|
public function logRequest($responseCode, $responseBody, $responseHeaders, $responseTime) |
64
|
|
|
{ |
65
|
3 |
|
$this->responseCode = $responseCode; |
66
|
3 |
|
$this->responseBody = $responseBody; |
67
|
3 |
|
$this->responseHeaders = $responseHeaders; |
68
|
3 |
|
$this->responseTime = $responseTime; |
69
|
3 |
|
} |
70
|
|
|
|
71
|
6 |
|
public function logError($errorNumber, $errorMessage, $responseTime) |
72
|
|
|
{ |
73
|
6 |
|
$this->isError = true; |
74
|
6 |
|
$this->errorNumber = $errorNumber; |
75
|
6 |
|
$this->errorMessage = $errorMessage; |
76
|
6 |
|
$this->responseTime = $responseTime; |
77
|
6 |
|
} |
78
|
|
|
|
79
|
3 |
|
public function getPostFields() |
80
|
|
|
{ |
81
|
3 |
|
return $this->postFields; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
public function getGetFields() |
85
|
|
|
{ |
86
|
3 |
|
return $this->getFields; |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
public function getCookieFields() |
90
|
|
|
{ |
91
|
3 |
|
return $this->cookieFields; |
92
|
|
|
} |
93
|
|
|
|
94
|
6 |
|
public function getUrl() |
95
|
|
|
{ |
96
|
6 |
|
return $this->url; |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
public function getMethod() |
100
|
|
|
{ |
101
|
3 |
|
return $this->method; |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
public function getHeaders() |
105
|
|
|
{ |
106
|
3 |
|
return $this->headers; |
107
|
|
|
} |
108
|
|
|
|
109
|
9 |
|
public function isError() |
110
|
|
|
{ |
111
|
9 |
|
return $this->isError; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return integer |
116
|
|
|
*/ |
117
|
3 |
|
public function getResponseCode() |
118
|
|
|
{ |
119
|
3 |
|
return $this->responseCode; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
3 |
|
public function getResponseBody() |
126
|
|
|
{ |
127
|
3 |
|
return $this->responseBody; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
3 |
|
public function getFormattedJsonBody() |
134
|
|
|
{ |
135
|
3 |
|
$body = $this->responseBody; |
136
|
3 |
|
$decoded = json_decode($body); |
137
|
3 |
|
if ($decoded) { |
138
|
|
|
$body = json_encode($decoded, JSON_PRETTY_PRINT); |
139
|
|
|
} |
140
|
3 |
|
return $body; |
141
|
|
|
} |
142
|
|
|
|
143
|
3 |
|
public function getResponseHeaders() |
144
|
|
|
{ |
145
|
3 |
|
return $this->responseHeaders; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return integer |
150
|
|
|
*/ |
151
|
6 |
|
public function getResponseTime() |
152
|
|
|
{ |
153
|
6 |
|
return $this->responseTime; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return integer |
158
|
|
|
*/ |
159
|
3 |
|
public function getErrorNumber() |
160
|
|
|
{ |
161
|
3 |
|
return $this->errorNumber; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
3 |
|
public function getErrorMessage() |
168
|
|
|
{ |
169
|
3 |
|
return $this->errorMessage; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|