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

UrlCheck   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setHttpClient() 0 4 1
A checkStatus() 0 15 3
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