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

code/checks/URLCheck.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
As per PSR2, the opening brace for this class should be on a new line.
Loading history...
9
	/**
10
	 * @var string
11
	 */
12
	protected $url;
13
14
	/**
15
	 * @var string
16
	 */
17
	protected $testString;
18
	
19
	/*
20
	 * @param string $url The URL to check, relative to the site (homepage is '').
21
	 * @param string $testString An optional piece of text to search for on the homepage.
22
	 */
23
	function __construct($url = '', $testString = '') {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
		$this->url = $url;
25
		$this->testString = $testString;
26
	}
27
28
	/**
29
	 * @inheritdoc
30
	 *
31
	 * @return array
32
	 *
33
	 * @throws SS_HTTPResponse_Exception
34
	 */
35
	function check() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
36
		$response = Director::test($this->url);
37
38
		if($response->getStatusCode() != 200) {
39
			return array(
40
				EnvironmentCheck::ERROR, 
41
				sprintf('Error retrieving "%s" (Code: %d)', $this->url, $response->getStatusCode())
42
			);
43
44
		} else if($this->testString && (strpos($response->getBody(), $this->testString) === false)) {
45
			return array(
46
				EnvironmentCheck::WARNING, 
47
				sprintf('Success retrieving "%s", but string "%s" not found', $this->url, $this->testString)
48
			);
49
50
		} else {
51
			return array(
52
				EnvironmentCheck::OK, 
53
				sprintf('Success retrieving "%s"', $this->url)
54
			);
55
		}
56
	}
57
}
58