These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * PHP version 5.4 |
||
4 | * |
||
5 | * @category GLICER |
||
6 | * @package GlLinkChecker |
||
7 | * @author Emmanuel ROECKER |
||
8 | * @author Rym BOUCHAGOUR |
||
9 | * @copyright 2015 GLICER |
||
10 | * @license MIT |
||
11 | * @link http://dev.glicer.com/ |
||
12 | * |
||
13 | * Created : 10/03/15 |
||
14 | * File : GlLinkCheckerError.php |
||
15 | * |
||
16 | */ |
||
17 | |||
18 | namespace GlLinkChecker; |
||
19 | |||
20 | use GuzzleHttp\Client; |
||
21 | use GuzzleHttp\Exception\ClientException; |
||
22 | use GuzzleHttp\Exception\RequestException; |
||
23 | |||
24 | |||
25 | /** |
||
26 | * Class GlLinkCheckerUrl |
||
27 | * @package GLLinkChecker |
||
28 | */ |
||
29 | class GlLinkCheckerError |
||
30 | { |
||
31 | /** |
||
32 | * @var \GuzzleHttp\Client $client |
||
33 | */ |
||
34 | private $client; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $link; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private $url; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $files; |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | private $statuscode = 0; |
||
55 | |||
56 | /** |
||
57 | * @var bool |
||
58 | */ |
||
59 | private $isAbsolute = true; |
||
60 | |||
61 | /** |
||
62 | * @var bool |
||
63 | */ |
||
64 | private $isExist = true; |
||
65 | |||
66 | |||
67 | /** |
||
68 | * @var bool |
||
69 | */ |
||
70 | private $isLowerCase = true; |
||
71 | |||
72 | /** |
||
73 | * @var bool |
||
74 | */ |
||
75 | private $isNotEndSlash = true; |
||
76 | |||
77 | /** |
||
78 | * Client $client |
||
79 | * string $link |
||
80 | * array $files |
||
81 | */ |
||
82 | public function __construct(Client $client, $link, $files) |
||
83 | { |
||
84 | $url = parse_url($link); |
||
85 | if ($url === false) { |
||
86 | throw new \Exception("Cannot parse link : " . $link); |
||
87 | } |
||
88 | |||
89 | $this->client = $client; |
||
90 | $this->url = $url; |
||
91 | $this->link = $link; |
||
92 | $this->files = $files; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return bool |
||
97 | */ |
||
98 | View Code Duplication | private function checkexisthead() |
|
99 | { |
||
100 | try { |
||
101 | $response = $this->client->head($this->link); |
||
102 | $this->statuscode = $response->getStatusCode(); |
||
103 | $this->isExist = (($this->statuscode == 200) || ($this->statuscode == 204)); |
||
104 | |||
105 | return $this->isExist; |
||
106 | } catch (ClientException $e) { |
||
107 | $this->statuscode = $e->getCode(); |
||
108 | } catch (RequestException $e) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
109 | |||
110 | } |
||
111 | |||
112 | $this->isExist = false; |
||
113 | |||
114 | return false; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return bool |
||
119 | */ |
||
120 | View Code Duplication | private function checkexistget() |
|
121 | { |
||
122 | try { |
||
123 | $response = $this->client->get($this->link); |
||
124 | $this->statuscode = $response->getStatusCode(); |
||
125 | $this->isExist = (($this->statuscode == 200) || ($this->statuscode == 204)); |
||
126 | |||
127 | return $this->isExist; |
||
128 | } catch (ClientException $e) { |
||
129 | $this->statuscode = $e->getCode(); |
||
130 | } catch (RequestException $e) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||
131 | } |
||
132 | |||
133 | $this->isExist = false; |
||
134 | |||
135 | return false; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * |
||
140 | */ |
||
141 | private function checkexist() |
||
142 | { |
||
143 | if ($this->checkexisthead()) { |
||
144 | return true; |
||
145 | } |
||
146 | if ($this->checkexistget()) { |
||
147 | return true; |
||
148 | } |
||
149 | |||
150 | return false; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @return bool |
||
155 | */ |
||
156 | private function checkendslash() |
||
157 | { |
||
158 | if (substr($this->link, -1) == '/') { |
||
159 | $this->isNotEndSlash = true; |
||
160 | |||
161 | return true; |
||
162 | } |
||
163 | |||
164 | if (isset($this->url['path']) && (strlen($this->url['path']) > 0)) { |
||
165 | $extension = pathinfo($this->url['path'], PATHINFO_EXTENSION); |
||
166 | if (isset($extension) && (strlen($extension) > 0)) { |
||
167 | $this->isNotEndSlash = true; |
||
168 | |||
169 | return true; |
||
170 | } |
||
171 | } |
||
172 | |||
173 | $this->isNotEndSlash = false; |
||
174 | |||
175 | return false; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @return bool |
||
180 | */ |
||
181 | private function checkabsolute() |
||
182 | { |
||
183 | View Code Duplication | if (isset($this->url['host']) && (strlen($this->url['host']) > 0)) { |
|
184 | $this->isAbsolute = true; |
||
185 | |||
186 | return true; |
||
187 | } |
||
188 | View Code Duplication | if (isset($this->url['path']) && (strpos($this->url['path'], "/") === 0)) { |
|
189 | $this->isAbsolute = true; |
||
190 | |||
191 | return true; |
||
192 | } |
||
193 | $this->isAbsolute = false; |
||
194 | |||
195 | return false; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @return bool |
||
200 | */ |
||
201 | private function checklowercase() |
||
202 | { |
||
203 | $this->isLowerCase = ($this->link === strtolower($this->link)); |
||
204 | |||
205 | return $this->isLowerCase; |
||
206 | } |
||
207 | |||
208 | |||
209 | /** |
||
210 | * @param array|null $internalurls |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | public function isInternal($internalurls) |
||
215 | { |
||
216 | if (!isset($internalurls)) { |
||
217 | return true; |
||
218 | } |
||
219 | |||
220 | if (!isset($this->url['host']) || (strlen($this->url['host']) <= 0)) { |
||
221 | return true; |
||
222 | } |
||
223 | |||
224 | foreach ($internalurls as $internalurl) { |
||
225 | if (strpos($this->link, $internalurl) === 0) { |
||
226 | return true; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | return false; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param array $list |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | public function check(array $list) |
||
239 | { |
||
240 | $result = true; |
||
241 | foreach ($list as $element) { |
||
242 | $element = "check" . trim(strtolower($element)); |
||
243 | $result &= $this->$element(); |
||
244 | } |
||
245 | |||
246 | return $result; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @return string |
||
251 | */ |
||
252 | public function getLink() |
||
253 | { |
||
254 | return $this->link; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @return array |
||
259 | */ |
||
260 | public function getFiles() |
||
261 | { |
||
262 | return $this->files; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * @return int |
||
267 | */ |
||
268 | public function getStatusCode() |
||
269 | { |
||
270 | return $this->statuscode; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @return array |
||
275 | */ |
||
276 | public function getErrorMessages() |
||
277 | { |
||
278 | $message = []; |
||
279 | |||
280 | if (!($this->isAbsolute)) { |
||
281 | $message[] = "Must be absolute (Sample : /article/index.html)"; |
||
282 | } |
||
283 | |||
284 | if (!($this->isLowerCase)) { |
||
285 | $message[] = "Must be in lowercase (Sample : http://www.example.com/index.html)"; |
||
286 | } |
||
287 | |||
288 | if (!($this->isExist)) { |
||
289 | $message[] = "Must exist (Http get error)"; |
||
290 | } |
||
291 | |||
292 | if (!($this->isNotEndSlash)) { |
||
293 | $message[] = "Must have a slash at the end (Sample : http://www.example.com/)"; |
||
294 | } |
||
295 | |||
296 | return $message; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @return array |
||
301 | */ |
||
302 | public function getErrorArray() { |
||
303 | $error = []; |
||
304 | |||
305 | $error['absolute'] = $this->isAbsolute; |
||
306 | $error['lowercase'] = $this->isLowerCase; |
||
307 | $error['exist'] = $this->isExist; |
||
308 | $error['notendslash'] = $this->isNotEndSlash; |
||
309 | |||
310 | return $error; |
||
311 | } |
||
312 | } |
||
313 |