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

URLCheck   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A check() 0 21 4
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