Completed
Push — master ( fc4353...2e8e64 )
by Haralan
01:19
created

Driver_Simple_RequestFactory_HTTP::current_path()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 6
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
	public function user_agent($user_agent = NULL)
33
	{
34
		if ($user_agent !== NULL)
35
		{
36
			$this->_user_agent = $user_agent;
37
			return $this;
38
		}
39
		return $this->_user_agent;
40
	}
41
42
	/**
43
	 * Get the url of the last request
44
	 * @return string
45
	 */
46
	public function current_url()
47
	{
48
		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
	public function current_path()
56
	{
57
		$url = parse_url($this->current_url());
58
59
		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
	public function execute($method, $url, array $post = array())
70
	{
71
		$curl = curl_init($url);
72
73
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
74
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
75
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
76
		curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent());
77
78
		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
			curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
81
		}
82
83
		$response = curl_exec($curl);
84
85
		if ($response === FALSE OR curl_getinfo($curl, CURLINFO_HTTP_CODE) !== 200)
86
		{
87
			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
		$this->_current_url = urldecode(curl_getinfo($curl, CURLINFO_EFFECTIVE_URL));
91
92
		return $response;
93
	}
94
}
95