Completed
Push — master ( 1deced...926e5e )
by Daniel
08:35
created

url_checker   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 7
c 4
b 1
f 1
lcom 0
cbo 0
dl 0
loc 50
ccs 26
cts 26
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 14 3
A get_headers() 0 14 3
A curl_header() 0 16 1
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2015 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services;
11
12
class url_checker
13
{
14 6
	public function exists($url, $curl = true)
15
	{
16 6
		if (!filter_var($url, FILTER_VALIDATE_URL))
17 6
		{
18 2
			return false;
19
		}
20
21 4
		$status = array();
22 4
		$headers = $this->get_headers($url, $curl);
23
24 4
		preg_match('/HTTP\/.* ([0-9]+) .*/', $headers , $status);
25
26 4
		return ($status[1] == 200) ? true : false;
27
	}
28
29 4
	protected function get_headers($url, $curl)
30
	{
31 4
		if (extension_loaded('curl') && $curl)
32 4
		{
33 2
			$headers = $this->curl_header($url);
34 2
		}
35
		else
36
		{
37 2
			$headers = get_headers($url);
38 2
			$headers = $headers[0];
39
		}
40
41 4
		return $headers;
42
	}
43
44
	// http://snipplr.com/view.php?codeview&id=61985
45 2
	protected function curl_header($url)
46
	{
47 2
		$ch = curl_init();
48
49 2
		curl_setopt($ch, CURLOPT_URL, $url);
50 2
		curl_setopt($ch, CURLOPT_HEADER, true);
51 2
		curl_setopt($ch, CURLOPT_NOBODY, true);
52 2
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
53 2
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
54
55 2
		$info = curl_exec($ch);
56
57 2
		curl_close($ch);
58
59 2
		return $info;
60
	}
61
}
62