Completed
Push — master ( 0aa8ac...a01137 )
by Bret R.
01:24
created

UrlCheck::checkStatus()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
nc 6
cc 3
nop 0
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->setSuccess(false);
55
                $result->setError('HTTP status code is '.$res->getStatusCode());
56
            }
57
        } catch (Exception $e) {
58
            $result->setSuccess(false);
59
            $result->setError('URL failed: '.$this->url);
60
        }
61
        return $result;
62
    }
63
}
64