Passed
Branch master (899b2b)
by Bret R.
02:52
created

UrlCheck   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 51
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkStatus() 0 12 3
A __construct() 0 5 1
A setHttpClient() 0 3 1
1
<?php
2
namespace BretRZaun\StatusPage\Check;
3
4
use BretRZaun\StatusPage\Result;
5
use Exception;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\GuzzleException;
8
9
class UrlCheck extends AbstractCheck
10
{
11
12
    protected $url;
13
14
    /**
15
     * @var Client
16
     */
17
    protected $client;
18
19
    /**
20
     * UrlCheck constructor.
21
     *
22
     * @param string $label Label
23
     * @param string $url URL to be tested
24
     */
25
    public function __construct(string $label, string $url)
26
    {
27
        parent::__construct($label);
28
        $this->url = $url;
29
        $this->setHttpClient(new Client());
30
    }
31
32
    /**
33
     * set http client
34
     *
35
     * @param Client $client
36
     */
37
    public function setHttpClient(Client $client): void
38
    {
39
        $this->client = $client;
40
    }
41
42
    /**
43
     * Check URL
44
     *
45
     * @return Result
46
     * @throws GuzzleException
47
     */
48
    public function checkStatus(): Result
49
    {
50
        $result = new Result($this->label);
51
        try {
52
            $res = $this->client->request('GET', $this->url);
53
            if ($res->getStatusCode() !== 200) {
54
                $result->setError('HTTP status code is '.$res->getStatusCode());
55
            }
56
        } catch (Exception $e) {
57
            $result->setError('URL failed: '.$this->url);
58
        }
59
        return $result;
60
    }
61
}
62