Completed
Push — master ( f52567...d70777 )
by Bret R.
03:35
created

UrlCheck::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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