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

Driver_Kohana_RequestFactory_Kohana::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Openbuildings\Spiderling;
4
5
/**
6
 * Use kohana requests to load urls, handle redirects
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_Kohana_RequestFactory_Kohana implements Driver_Simple_RequestFactory {
14
15
16
	protected $_request;
17
	protected $_response;
18
	protected $_max_redirects = 5;
19
	protected $_previous_url;
20
21
	public function max_redirects($max_redirects = NULL)
22
	{
23
		if ($max_redirects !== NULL)
24
		{
25
			$this->_max_redirects = (int) $max_redirects;
26
			return $this;
27
		}
28
		return $this->_max_redirects;
29
	}
30
31 1
	public function user_agent()
32
	{
33 1
		return \Request::$user_agent;
34
	}
35
36 1
	public function current_url()
37
	{
38 1
		return \URL::site($this->current_path(), TRUE);
39
	}
40
41
	public function previous_url()
42
	{
43
		return $this->_previous_url;
44
	}
45
46 1
	public function current_path()
47
	{
48 1
		if ( ! $this->_request)
49 1
			return NULL;
50
51 1
		return '/'.ltrim($this->_request->uri(), '/');
52
	}
53
54
	public function request()
55
	{
56
		return $this->_request;
57
	}
58
59 1
	public function response()
60
	{
61 1
		return $this->_response;
62
	}
63
64 1
	public function execute($method, $url, array $post = array())
65
	{
66 1
		$redirects_count = 1;
67
68 1
		$this->_request = (new \Request($url))
69 1
			->method($method)
70 1
			->post($post)
71 1
			->body(http_build_query($post));
72
73 1
		if ($this->_previous_url) {
74
			$this->_request->referrer($this->_previous_url);
75
		}
76
77 1
		$this->_previous_url = $this->current_url().\URL::query($this->_request->query(), FALSE);
78
79 1
		\Request::$initial = $this->_request;
80
81 1
		$this->_response = $this->_request->execute();
82
83 1
		while (($this->_response->status() >= 300 AND $this->_response->status() < 400))
84
		{
85
			$redirects_count++;
86
87
			if ($redirects_count >= $this->max_redirects())
88
				throw new Exception_Toomanyredirects('Maximum Number of redirects (5) for url :url', array(':url' => $url));
89
90
			$url_parts = parse_url($this->_response->headers('location'));
91
92
			$query = isset($url_parts['query']) ? $url_parts['query'] : '';
93
			parse_str($query, $query);
94
95
			$_GET = $query;
96
97
			$url = $url_parts['path'];
98
99
			$this->_request = new \Request($url);
100
			$this->_request->query($query);
101
102
			\Request::$initial = $this->_request;
103
104
			$this->_response = $this->_request->execute();
105
		}
106
107 1
		return $this->_response->body();
108
	}
109
}
110