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