URLCheck   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 18 4
A __construct() 0 4 1
1
<?php
2
3
namespace SilverStripe\EnvironmentCheck\Checks;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
7
8
/**
9
 * Check that a given URL is functioning, by default, the homepage.
10
 *
11
 * Note that Director::test() will be used rather than a CURL check.
12
 *
13
 * @package environmentcheck
14
 */
15
class URLCheck implements EnvironmentCheck
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $url;
21
22
    /**
23
     * @var string
24
     */
25
    protected $testString;
26
27
    /**
28
     * @param string $url The URL to check, relative to the site (homepage is '').
29
     * @param string $testString An optional piece of text to search for on the homepage.
30
     */
31
    public function __construct($url = '', $testString = '')
32
    {
33
        $this->url = $url;
34
        $this->testString = $testString;
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     *
40
     * @return array
41
     * @throws HTTPResponse_Exception
42
     */
43
    public function check()
44
    {
45
        $response = Director::test($this->url);
46
47
        if ($response->getStatusCode() != 200) {
48
            return [
49
                EnvironmentCheck::ERROR,
50
                sprintf('Error retrieving "%s" (Code: %d)', $this->url, $response->getStatusCode())
51
            ];
52
        } elseif ($this->testString && (strpos($response->getBody(), $this->testString) === false)) {
53
            return [
54
                EnvironmentCheck::WARNING,
55
                sprintf('Success retrieving "%s", but string "%s" not found', $this->url, $this->testString)
56
            ];
57
        }
58
        return [
59
            EnvironmentCheck::OK,
60
            sprintf('Success retrieving "%s"', $this->url)
61
        ];
62
    }
63
}
64