Driver_Simple_RequestFactory_HTTP   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 82
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A current_url() 0 4 1
A current_path() 0 6 2
A user_agent() 0 9 2
A execute() 0 25 4
1
<?php
2
3
namespace Openbuildings\Spiderling;
4
5
/**
6
 * Load urls using Curl
7
 *
8
 * @package    Openbuildings\Spiderling
9
 * @author     Ivan Kerin
10
 * @copyright  (c) 2013 OpenBuildings Ltd.
11
 * @license    http://spdx.org/licenses/BSD-3-Clause
12
 */
13
class Driver_Simple_RequestFactory_HTTP implements Driver_Simple_RequestFactory
14
{
15
	/**
16
	 * The user agent to be used when performing the requests
17
	 * @var string
18
	 */
19
	protected $_user_agent = 'Spiderling Simple Driver';
20
21
	/**
22
	 * The last visited url address
23
	 * @var string
24
	 */
25
	protected $_current_url;
26
27
	/**
28
	 * Getter / Setter for the user agent, used when performing the requests
29
	 * @param  string $user_agent
30
	 * @return string|Driver_Simple_RequestFactory_HTTP
31
	 */
32 2
	public function user_agent($user_agent = NULL)
33
	{
34 2
		if ($user_agent !== NULL)
35
		{
36 1
			$this->_user_agent = $user_agent;
37 1
			return $this;
38
		}
39 2
		return $this->_user_agent;
40
	}
41
42
	/**
43
	 * Get the url of the last request
44
	 * @return string
45
	 */
46 1
	public function current_url()
47
	{
48 1
		return $this->_current_url;
49
	}
50
51
	/**
52
	 * Get the path (no protocol or host) of the last request
53
	 * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
54
	 */
55 1
	public function current_path()
56
	{
57 1
		$url = parse_url($this->current_url());
58
59 1
		return $url['path'].(isset($url['query']) ? '?'.$url['query'] : '');
60
	}
61
62
	/**
63
	 * Perform the request, follow redirects, return the response
64
	 * @param  string $method
65
	 * @param  string $url
66
	 * @param  array $post
67
	 * @return string
68
	 */
69 1
	public function execute($method, $url, array $post = array())
70
	{
71 1
		$curl = curl_init($url);
72
73 1
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
74 1
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
75 1
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
76 1
		curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent());
77
78 1
		if ($post)
0 ignored issues
show
Bug Best Practice introduced by
The expression $post of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
79
		{
80 1
			curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
81
		}
82
83 1
		$response = curl_exec($curl);
84
85 1
		if ($response === FALSE OR curl_getinfo($curl, CURLINFO_HTTP_CODE) !== 200)
86
		{
87 1
			throw new Exception_Curl('Curl: Download Error: :error, status :status on url :url', array(':url' => $url, ':status' => curl_getinfo($curl, CURLINFO_HTTP_CODE), ':error' => curl_error($curl)));
88
		}
89
90 1
		$this->_current_url = urldecode(curl_getinfo($curl, CURLINFO_EFFECTIVE_URL));
91
92 1
		return $response;
93
	}
94
}
95