Completed
Push — master ( e090e4...18d79e )
by Damian
02:26
created

URLCheck::check()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 21
rs 9.0534
cc 4
eloc 14
nc 3
nop 0
1
<?php
2
3
/**
4
 * Check that a given URL is functioning, by default, the homepage.
5
 * 
6
 * Note that Director::test() will be used rather than a CURL check.
7
 */
8
class URLCheck implements EnvironmentCheck
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $url;
14
15
    /**
16
     * @var string
17
     */
18
    protected $testString;
19
    
20
    /*
21
     * @param string $url The URL to check, relative to the site (homepage is '').
22
     * @param string $testString An optional piece of text to search for on the homepage.
23
     */
24
    public function __construct($url = '', $testString = '')
25
    {
26
        $this->url = $url;
27
        $this->testString = $testString;
28
    }
29
30
    /**
31
     * @inheritdoc
32
     *
33
     * @return array
34
     *
35
     * @throws SS_HTTPResponse_Exception
36
     */
37
    public function check()
38
    {
39
        $response = Director::test($this->url);
40
41
        if ($response->getStatusCode() != 200) {
42
            return array(
43
                EnvironmentCheck::ERROR,
44
                sprintf('Error retrieving "%s" (Code: %d)', $this->url, $response->getStatusCode())
45
            );
46
        } elseif ($this->testString && (strpos($response->getBody(), $this->testString) === false)) {
47
            return array(
48
                EnvironmentCheck::WARNING,
49
                sprintf('Success retrieving "%s", but string "%s" not found', $this->url, $this->testString)
50
            );
51
        } else {
52
            return array(
53
                EnvironmentCheck::OK,
54
                sprintf('Success retrieving "%s"', $this->url)
55
            );
56
        }
57
    }
58
}
59