|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tylercd100\ServerStatus; |
|
4
|
|
|
|
|
5
|
|
|
use JJG\Ping; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
|
|
8
|
|
|
class Host |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $host; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var Ping |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $pinger; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Client |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $requester; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var double |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $ping = 0.0; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var integer |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $statusCode = 0; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $host The host URL or IP to check |
|
37
|
|
|
*/ |
|
38
|
18 |
|
public function __construct($host) |
|
39
|
|
|
{ |
|
40
|
18 |
|
$this->host = $this->strip($host); |
|
41
|
18 |
|
$this->pinger = new Ping($this->host); |
|
42
|
18 |
|
$this->requester = new Client(); |
|
43
|
18 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Strips off the https and http |
|
47
|
|
|
* @param string $host The URL to strip |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
18 |
|
protected function strip($host) |
|
51
|
|
|
{ |
|
52
|
18 |
|
$host = str_replace("https://","",$host); |
|
53
|
18 |
|
$host = str_replace("http://", "",$host); |
|
54
|
18 |
|
return $host; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Checks the ping of the host |
|
59
|
|
|
* |
|
60
|
|
|
* @return double|false |
|
61
|
|
|
*/ |
|
62
|
6 |
|
public function ping() |
|
63
|
|
|
{ |
|
64
|
3 |
|
$this->ping = $this->pinger->ping(); |
|
65
|
6 |
|
return $this->getPing(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Checks the status of the host |
|
70
|
|
|
* |
|
71
|
|
|
* @param integer $timeout The timeout of the request |
|
72
|
|
|
* @return integer |
|
73
|
|
|
*/ |
|
74
|
3 |
|
public function status($timeout = 5){ |
|
|
|
|
|
|
75
|
3 |
|
$response = $this->requester->request('GET', $this->host, [ |
|
76
|
3 |
|
'connect_timeout' => 5, |
|
77
|
|
|
'timeout' => 5 |
|
78
|
3 |
|
]); |
|
79
|
3 |
|
$this->statusCode = $response->getStatusCode(); |
|
80
|
3 |
|
return $this->getStatusCode(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Sets the value of pinger. |
|
85
|
|
|
* |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
3 |
|
public function setPinger(Ping $pinger) |
|
89
|
|
|
{ |
|
90
|
3 |
|
$this->pinger = $pinger; |
|
91
|
3 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Sets the value of requester. |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
3 |
|
public function setRequester(Client $requester) |
|
99
|
|
|
{ |
|
100
|
3 |
|
$this->requester = $requester; |
|
101
|
3 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Gets the value of host. |
|
105
|
|
|
* |
|
106
|
|
|
* @return mixed |
|
107
|
|
|
*/ |
|
108
|
3 |
|
public function getHost() |
|
109
|
|
|
{ |
|
110
|
3 |
|
return $this->host; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Gets the value of ping. |
|
115
|
|
|
* |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
*/ |
|
118
|
6 |
|
public function getPing() |
|
119
|
|
|
{ |
|
120
|
6 |
|
return $this->ping; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Gets the value of statusCode. |
|
125
|
|
|
* |
|
126
|
|
|
* @return mixed |
|
127
|
|
|
*/ |
|
128
|
6 |
|
public function getStatusCode() |
|
129
|
|
|
{ |
|
130
|
6 |
|
return $this->statusCode; |
|
131
|
|
|
} |
|
132
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.