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