1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Viirre\UrlChecker; |
4
|
|
|
|
5
|
|
|
class UrlStatus { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Url to check |
9
|
|
|
* |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
protected $url; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Status code returned, if URL is responding |
16
|
|
|
* |
17
|
|
|
* @var int|null |
18
|
|
|
*/ |
19
|
|
|
protected $statusCode; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Reason for when URL is not responding with 200 status code |
23
|
|
|
* |
24
|
|
|
* @var string|null |
25
|
|
|
*/ |
26
|
|
|
protected $reason; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* If the url's host is unresolvable |
30
|
|
|
* |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
protected $unresolvableHost; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The Guzzle response object (if successful connection) |
37
|
|
|
* |
38
|
|
|
* @var \GuzzleHttp\Message\Response|null |
39
|
|
|
*/ |
40
|
|
|
protected $response; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Time it took for the request in seconds |
44
|
|
|
* |
45
|
|
|
* @var double |
46
|
|
|
*/ |
47
|
|
|
protected $time; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create an instance of an UrlStatus |
51
|
|
|
* |
52
|
|
|
* @param string $url |
53
|
|
|
* @param int|null $statusCode |
54
|
|
|
* @param double $time in seconds |
55
|
|
|
* @param bool $unresolvableHost |
56
|
|
|
* @param \GuzzleHttp\Message\Response|null $response |
57
|
|
|
* @param string|null $reason |
58
|
|
|
*/ |
59
|
4 |
|
public function __construct($url, $statusCode, $time, $unresolvableHost, $response = null, $reason = null) |
60
|
|
|
{ |
61
|
4 |
|
$this->statusCode = $statusCode; |
62
|
4 |
|
$this->url = $url; |
63
|
4 |
|
$this->reason = $reason; |
64
|
4 |
|
$this->unresolvableHost = $unresolvableHost; |
65
|
4 |
|
$this->response = $response; |
66
|
4 |
|
$this->time = $time; |
67
|
4 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return if the URL is responding with a 200 status |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
3 |
|
public function isRespondingOk() |
75
|
|
|
{ |
76
|
3 |
|
return $this->statusCode == 200; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Return if the URL is responding but not with a 200 status code |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
1 |
|
public function isRespondingButNotOk() |
86
|
|
|
{ |
87
|
1 |
|
return ! $this->isRespondingOk() && ! $this->unresolvableHost; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return if the URL is not responding at all |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
1 |
|
public function isNotResponding() |
96
|
|
|
{ |
97
|
1 |
|
return $this->hostIsUnresolvable(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return if the URL's host is unresolvable |
102
|
|
|
* |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
1 |
|
public function hostIsUnresolvable() |
106
|
|
|
{ |
107
|
1 |
|
return $this->unresolvableHost; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the status code returned (if successful connection) |
112
|
|
|
* |
113
|
|
|
* @return int|null |
114
|
|
|
*/ |
115
|
2 |
|
public function getStatusCode() |
116
|
|
|
{ |
117
|
2 |
|
return $this->statusCode; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the URL |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getUrl() |
126
|
|
|
{ |
127
|
|
|
return $this->url; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the reason if connection was unsuccessful |
132
|
|
|
* |
133
|
|
|
* @return string|null |
134
|
|
|
*/ |
135
|
|
|
public function getReason() |
136
|
|
|
{ |
137
|
|
|
return $this->reason; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the time it took for the request to complete (in seconds) |
142
|
|
|
* |
143
|
|
|
* @return double |
144
|
|
|
*/ |
145
|
1 |
|
public function getTimeInSeconds() |
146
|
|
|
{ |
147
|
1 |
|
return $this->time; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the time it took for the request to complete (in milliseconds) |
153
|
|
|
* |
154
|
|
|
* @return double |
155
|
|
|
*/ |
156
|
1 |
|
public function getTimeInMilliSeconds() |
157
|
|
|
{ |
158
|
1 |
|
return round($this->time * 100); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get the Guzzle response object (if successful connection) |
163
|
|
|
* |
164
|
|
|
* @return null|GuzzleHttp\Message\Response |
165
|
|
|
*/ |
166
|
1 |
|
public function getResponse() |
167
|
|
|
{ |
168
|
1 |
|
return $this->response; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|