| Conditions | 3 |
| Paths | 4 |
| Total Lines | 36 |
| Code Lines | 18 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 14 |
| CRAP Score | 3.0026 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 14 | 4 | protected function checkWhetherHostIsLive(string $host): bool |
|
| 15 | { |
||
| 16 | $statuses = array( |
||
| 17 | 4 | 200, |
|
| 18 | 301, |
||
| 19 | 302, |
||
| 20 | ); |
||
| 21 | |||
| 22 | // Initializing curl |
||
| 23 | 4 | $curlHandle = curl_init($host); |
|
| 24 | |||
| 25 | // Validating whether the curl handle can reach the host, else returning false. |
||
| 26 | 4 | if ($curlHandle !== false) { |
|
| 27 | // Calling the host for a response |
||
| 28 | 4 | curl_setopt_array($curlHandle, array( |
|
| 29 | 4 | CURLOPT_HEADER => true, |
|
| 30 | 4 | CURLOPT_NOBODY => true, |
|
| 31 | 4 | CURLOPT_RETURNTRANSFER => true, |
|
| 32 | 4 | CURLOPT_TIMEOUT => 10, |
|
| 33 | 4 | CURLOPT_USERAGENT => 'page-check/1.0', |
|
| 34 | )); |
||
| 35 | |||
| 36 | // Executing request |
||
| 37 | 4 | curl_exec($curlHandle); |
|
| 38 | |||
| 39 | // Collecting the status code and casting it to an integer |
||
| 40 | 4 | $status = (int) curl_getinfo($curlHandle, CURLINFO_HTTP_CODE); |
|
| 41 | |||
| 42 | // Closing curl |
||
| 43 | 4 | curl_close($curlHandle); |
|
| 44 | } else { |
||
| 45 | $status = 500; |
||
| 46 | } |
||
| 47 | |||
| 48 | // Validating whether the http status is inside the allowed statuses array (Whether the page is live or not) |
||
| 49 | 4 | return in_array($status, $statuses) ? true : false; |
|
| 50 | } |
||
| 52 |