1 | <?php |
||
10 | class Http extends Base |
||
11 | { |
||
12 | /** |
||
13 | * @return Result |
||
14 | */ |
||
15 | protected $secure = false; |
||
16 | |||
17 | /** |
||
18 | * @var |
||
19 | */ |
||
20 | protected $guzzle; |
||
21 | |||
22 | /** |
||
23 | * @var |
||
24 | */ |
||
25 | private $totalTime; |
||
26 | |||
27 | /** |
||
28 | * @var |
||
29 | */ |
||
30 | private $url; |
||
31 | |||
32 | /** |
||
33 | * HTTP Checker. |
||
34 | * |
||
35 | * @return Result |
||
36 | */ |
||
37 | 1 | public function check() |
|
38 | { |
||
39 | try { |
||
40 | 1 | $health = []; |
|
|
|||
41 | |||
42 | 1 | foreach ($this->getResourceUrlArray() as $url) { |
|
43 | 1 | list($healthy, $message) = $this->checkWebPage( |
|
44 | 1 | $this->makeUrlWithScheme($url, $this->secure), |
|
45 | 1 | $this->secure |
|
46 | ); |
||
47 | |||
48 | 1 | if (!$healthy) { |
|
49 | 1 | return $this->makeResult($healthy, $message); |
|
50 | } |
||
51 | } |
||
52 | |||
53 | 1 | return $this->makeHealthyResult(); |
|
54 | 1 | } catch (\Exception $exception) { |
|
55 | 1 | report($exception); |
|
56 | |||
57 | 1 | return $this->makeResultFromException($exception); |
|
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Get array of resource urls. |
||
63 | * |
||
64 | * @return array |
||
65 | */ |
||
66 | 1 | private function getResourceUrlArray() |
|
67 | { |
||
68 | 1 | if (is_a($this->target->urls, Collection::class)) { |
|
69 | 1 | return $this->target->urls->toArray(); |
|
70 | } |
||
71 | |||
72 | return (array) $this->target->urls; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Check web pages. |
||
77 | * |
||
78 | * @param $url |
||
79 | * @param bool $ssl |
||
80 | * @return mixed |
||
81 | */ |
||
82 | 1 | private function checkWebPage($url, $ssl = false) |
|
83 | { |
||
84 | 1 | $success = $this->requestSuccessful($url, $ssl); |
|
85 | |||
86 | 1 | return [$success, $success ? '' : $this->getErrorMessage()]; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Send an http request and fetch the response. |
||
91 | * |
||
92 | * @param $url |
||
93 | * @param $ssl |
||
94 | * @return mixed|\Psr\Http\Message\ResponseInterface |
||
95 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
96 | */ |
||
97 | 1 | private function fetchResponse($url, $ssl) |
|
98 | { |
||
99 | 1 | $this->url = $url; |
|
100 | |||
101 | 1 | return (new Guzzle())->request( |
|
102 | 1 | 'GET', |
|
103 | 1 | $this->url, |
|
104 | 1 | $this->getConnectionOptions($ssl) |
|
105 | ); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Get http connection options. |
||
110 | * |
||
111 | * @param $ssl |
||
112 | * @return array |
||
113 | */ |
||
114 | 1 | private function getConnectionOptions($ssl) |
|
115 | { |
||
116 | return [ |
||
117 | 1 | 'connect_timeout' => $this->getConnectionTimeout(), |
|
118 | 1 | 'timeout' => $this->getConnectionTimeout(), |
|
119 | 1 | 'verify' => $ssl, |
|
120 | 1 | 'on_stats' => $this->onStatsCallback(), |
|
121 | ]; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Get the error message. |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | private function getErrorMessage() |
||
140 | |||
141 | /** |
||
142 | * The the connection timeout. |
||
143 | * |
||
144 | * @return int |
||
145 | */ |
||
146 | 1 | private function getConnectionTimeout() |
|
150 | |||
151 | /** |
||
152 | * The the roundtrip timeout. |
||
153 | * |
||
154 | * @return int |
||
155 | */ |
||
156 | 1 | private function getRoundtripTimeout() |
|
160 | |||
161 | /** |
||
162 | * Make a url with a proper scheme. |
||
163 | * |
||
164 | * @param $url |
||
165 | * @param $secure |
||
166 | * @return mixed |
||
167 | */ |
||
168 | 1 | private function makeUrlWithScheme($url, $secure) |
|
176 | |||
177 | /** |
||
178 | * Guzzle OnStats callback. |
||
179 | * |
||
180 | * @return \Closure |
||
181 | */ |
||
182 | 1 | private function onStatsCallback() |
|
188 | |||
189 | /** |
||
190 | * Send a request and get the result. |
||
191 | * |
||
192 | * @param $url |
||
193 | * @param $ssl |
||
194 | * @return bool |
||
195 | * @internal param $response |
||
196 | */ |
||
197 | 1 | private function requestSuccessful($url, $ssl) |
|
204 | |||
205 | /** |
||
206 | * Check if the request timed out. |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | 1 | private function requestTimedout() |
|
214 | } |
||
215 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.