Passed
Pull Request — master (#1)
by
unknown
01:43
created

StatusCheck::getIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Leankoala\HealthFoundation\Check\Resource\Http;
4
5
use Leankoala\HealthFoundation\Check\Check;
6
use Leankoala\HealthFoundation\Check\Result;
7
8
class StatusCheck implements Check
9
{
10
    const IDENTIFIER = 'base:resource:http:status';
11
12
    private $destination = null;
13
    private $httpStatus = null;
14
15
    public function init($destination,$httpStatus)
16
    {
17
        $this->destination = $destination;
18
		$this->httpStatus = $httpStatus;
19
    }
20
21
    /**
22
     * Checks the response status 
23
     *
24
     * @return Result
25
     */
26
    public function run()
27
    {
28
		$ch = curl_init();
29
30
		curl_setopt( $ch, CURLOPT_URL, $this->destination );
31
32
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
33
		curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'HEAD' );
34
		curl_setopt( $ch, CURLOPT_HEADER, true );
35
		curl_setopt( $ch, CURLOPT_NOBODY, true );
36
37
		curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5 ); // connect timeout
38
		curl_setopt( $ch, CURLOPT_TIMEOUT, 10 ); // curl timeout
39
40
		// @todo configurable timeouts (do not put hundreds of parameters to the init method)
41
		// @todo Handle SSL support on self-signed cert errors
42
		// @todo Optional setup outgoing proxy host and port configuration
43
44
		$httpError = null;
45
		$httpStatus = null;
46
		if ( curl_exec( $ch ) === false ) {
47
			$httpError = curl_error( $ch );
48
		} else {
49
			$httpStatus = curl_getinfo( $ch, CURLINFO_HTTP_CODE );			
50
		}
51
52
		curl_close( $ch );
53
54
		if ( !is_null( $httpError ) ) {
55
    	    return new Result(Result::STATUS_FAIL, 'Http resource request ' . $this->destination . ' has an error ['.$httpError.']');
56
		} elseif ( $httpStatus == $this->httpStatus ) {
57
    	    return new Result(Result::STATUS_PASS, 'Http resource status for ' . $this->destination . ' is ok ['.$httpStatus.']');
58
		} else {
59
	        return new Result(Result::STATUS_WARN, 'Http resource status for ' . $this->destination . ' is different ['.$this->httpStatus.' was expected but '.$httpStatus.' was delivered]' );
60
		}
61
    }
62
63
    public function getIdentifier()
64
    {
65
        return self::IDENTIFIER;
66
    }
67
}
68