HostTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 19
c 1
b 0
f 0
dl 0
loc 45
ccs 14
cts 15
cp 0.9333
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A checkWhetherHostIsLive() 0 36 3
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