HostTrait::checkWhetherHostIsLive()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0026

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 36
ccs 14
cts 15
cp 0.9333
crap 3.0026
rs 9.6666
1
<?php
2
3
namespace Mediadevs\Validator\Traits;
4
5
trait HostTrait
6
{
7
    /**
8
     * Pinging the hostname to see if it is live / an actual working hostname.
9
     *
10
     * @param string $host
11
     *
12
     * @return bool
13
     */
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
    }
51
}
52