Checker   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 89.66%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 80
ccs 26
cts 29
cp 0.8966
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A check() 0 43 4
A validateUrl() 0 6 2
1
<?php
2
3
namespace Viirre\UrlChecker;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Exception\ConnectException;
8
9
class Checker
10
{
11
    /**
12
     * @var Client
13
     */
14
    protected $guzzle;
15
16
    /**
17
     * Create a new Instance
18
     */
19 5
    public function __construct()
20
    {
21 5
        $this->guzzle = new Client();
22 5
    }
23
24
    /**
25
     * Perform the check of a URL
26
     *
27
     * @param string $url URL to check
28
     * @param int $timeout timeout for the request. Defaults to 5 seconds
29
     * @return UrlStatus
30
     * @throws UrlMalformedException
31
     */
32 5
    public function check($url, $timeout = 5)
33
    {
34 5
        $this->validateUrl($url);
35
36 4
        $response = null;
37 4
        $statusCode = null;
38 4
        $reason = null;
39 4
        $unresolved = false;
40 4
        $timeStart = microtime(true);
41
42
        try {
43
44 4
            $response = $this->guzzle->get($url, [
45 4
                'timeout' => $timeout
46
            ]);
47
48 2
            $statusCode = $response->getStatusCode();
49
50 2
        } catch (ClientException $e) {
51
52
            // When not a 200 status but still responding
53 1
            $statusCode = $e->getCode();
54 1
            $reason = $e->getMessage();
55
56 1
        } catch (ConnectException $e) {
57
58
            // Unresolvable host etc
59 1
            $reason = $e->getMessage();
60 1
            $unresolved = true;
61
62
        } catch (\Exception $e) {
63
64
            // Other errors
65
            $reason = $e->getMessage();
66
            $unresolved = true;
67
68
        }
69
70 4
        $timeEnd = microtime(true);
71 4
        $time = ($timeEnd - $timeStart); // seconds
72
73 4
        return new UrlStatus($url, $statusCode, $time, $unresolved, $response, $reason);
0 ignored issues
show
Documentation introduced by
$response is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a object<GuzzleHttp\Message\Response>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
    }
75
76
    /**
77
     * Validate that a url is a valid url
78
     *
79
     * @param string $url
80
     * @throws UrlMalformedException
81
     */
82 5
    private function validateUrl($url)
83
    {
84 5
        if (!filter_var($url, FILTER_VALIDATE_URL)) {
85 1
            throw new UrlMalformedException("The provided url: $url is malformed");
86
        }
87 4
    }
88
}
89