Total Complexity | 8 |
Total Lines | 48 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
14 | class UrlValidator |
||
15 | { |
||
16 | /** |
||
17 | * Returns true when url does not return error codes or not found. |
||
18 | * |
||
19 | * @param string $url |
||
20 | * @return boolean |
||
21 | */ |
||
22 | public function validate($url) |
||
23 | { |
||
24 | if (null !== ($headers = $this->getHeader($url))) { |
||
25 | $statusCode = $this->getStatusCode($headers); |
||
26 | // see https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html |
||
27 | return ($statusCode >= 200 && $statusCode < 300); |
||
28 | } |
||
29 | return false; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Fetch a url with a HEAD request because we just want to check status code. |
||
34 | * |
||
35 | * @param string $url |
||
36 | * @return array|null |
||
37 | */ |
||
38 | protected function getHeader($url) |
||
39 | { |
||
40 | if (false !== @file_get_contents($url, false, stream_context_create(['http' => ['method' => 'HEAD']]))) { |
||
41 | return $http_response_header; |
||
42 | } else { |
||
43 | return null; |
||
44 | } |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Parse the headers array and search for the status pattern |
||
49 | * |
||
50 | * @param array $headers |
||
51 | * @return int |
||
52 | */ |
||
53 | protected function getStatusCode(array $headers) |
||
62 | } |
||
63 | } |
||
64 |