| 1 | <?php |
||
| 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 double |
||
| 22 | */ |
||
| 23 | protected $ping = 0.0; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var integer |
||
| 27 | */ |
||
| 28 | protected $statusCode = 0; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $host The host URL or IP to check |
||
| 32 | */ |
||
| 33 | 18 | public function __construct($host) |
|
| 34 | { |
||
| 35 | 18 | $this->host = $host; |
|
| 36 | 18 | $this->pinger = new Ping($host); |
|
| 37 | 18 | $this->requester = new Client(); |
|
|
|
|||
| 38 | 18 | } |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Checks the ping of the host |
||
| 42 | * |
||
| 43 | * @return double|false |
||
| 44 | */ |
||
| 45 | 3 | public function ping(){ |
|
| 46 | 3 | $this->ping = $this->pinger->ping(); |
|
| 47 | 3 | return $this->getPing(); |
|
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Checks the status of the host |
||
| 52 | * |
||
| 53 | * @return integer |
||
| 54 | */ |
||
| 55 | 3 | public function status(){ |
|
| 56 | 3 | $response = $this->requester->request('GET', $this->host); |
|
| 57 | 3 | $this->statusCode = $response->getStatusCode(); |
|
| 58 | 3 | return $this->getStatusCode(); |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Sets the value of pinger. |
||
| 63 | * |
||
| 64 | * @return void |
||
| 65 | */ |
||
| 66 | 3 | public function setPinger(Ping $pinger) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Sets the value of requester. |
||
| 73 | * |
||
| 74 | * @return void |
||
| 75 | */ |
||
| 76 | 3 | public function setRequester(Client $requester) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Gets the value of host. |
||
| 83 | * |
||
| 84 | * @return mixed |
||
| 85 | */ |
||
| 86 | 3 | public function getHost() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Gets the value of ping. |
||
| 93 | * |
||
| 94 | * @return mixed |
||
| 95 | */ |
||
| 96 | 6 | public function getPing() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Gets the value of statusCode. |
||
| 103 | * |
||
| 104 | * @return mixed |
||
| 105 | */ |
||
| 106 | 6 | public function getStatusCode() |
|
| 110 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: