Completed
Pull Request — master (#271)
by
unknown
03:48
created

Response::is_redirect()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Rmccue\Requests;
3
4
use Rmccue\Requests\Exception as Exception;
5
use Rmccue\Requests\Exception\HTTP as Exception_HTTP;
6
use Rmccue\Requests\Cookie\Jar as Cookie_Jar;
7
use Rmccue\Requests\Response\Headers as Headers;
8
/**
9
 * HTTP response class
10
 *
11
 * Contains a response from Requests::request()
12
 * @package Rmccue\Requests
13
 */
14
15
/**
16
 * HTTP response class
17
 *
18
 * Contains a response from Requests::request()
19
 * @package Rmccue\Requests
20
 */
21
class Response {
22
	/**
23
	 * Constructor
24
	 */
25
	public function __construct() {
26
		$this->headers = new Headers();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Rmccue\Requests\Response\Headers() of type object<Rmccue\Requests\Response\Headers> is incompatible with the declared type object<Rmccue\Requests\R...uests\Response\Headers> of property $headers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
		$this->cookies = new Cookie_Jar();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Rmccue\Requests\Cookie\Jar() of type object<Rmccue\Requests\Cookie\Jar> is incompatible with the declared type object<Rmccue\Requests\R...ue\Requests\Cookie\Jar> of property $cookies.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
	}
29
30
	/**
31
	 * Response body
32
	 *
33
	 * @var string
34
	 */
35
	public $body = '';
36
37
	/**
38
	 * Raw HTTP data from the transport
39
	 *
40
	 * @var string
41
	 */
42
	public $raw = '';
43
44
	/**
45
	 * Headers, as an associative array
46
	 *
47
	 * @var Rmccue\Requests\Response\Headers Array-like object representing headers
48
	 */
49
	public $headers = array();
50
51
	/**
52
	 * Status code, false if non-blocking
53
	 *
54
	 * @var integer|boolean
55
	 */
56
	public $status_code = false;
57
58
	/**
59
	 * Protocol version, false if non-blocking
60
	 * @var float|boolean
61
	 */
62
	public $protocol_version = false;
63
64
	/**
65
	 * Whether the request succeeded or not
66
	 *
67
	 * @var boolean
68
	 */
69
	public $success = false;
70
71
	/**
72
	 * Number of redirects the request used
73
	 *
74
	 * @var integer
75
	 */
76
	public $redirects = 0;
77
78
	/**
79
	 * URL requested
80
	 *
81
	 * @var string
82
	 */
83
	public $url = '';
84
85
	/**
86
	 * Previous requests (from redirects)
87
	 *
88
	 * @var array Array of Rmccue\Requests\Response objects
89
	 */
90
	public $history = array();
91
92
	/**
93
	 * Cookies from the request
94
	 *
95
	 * @var Rmccue\Requests\Cookie\Jar Array-like object representing a cookie jar
96
	 */
97
	public $cookies = array();
98
99
	/**
100
	 * Is the response a redirect?
101
	 *
102
	 * @return boolean True if redirect (3xx status), false if not.
103
	 */
104
	public function is_redirect() {
105
		$code = $this->status_code;
106
		return in_array($code, array(300, 301, 302, 303, 307)) || $code > 307 && $code < 400;
107
	}
108
109
	/**
110
	 * Throws an exception if the request was not successful
111
	 *
112
	 * @throws Rmccue\Requests\Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`)
113
	 * @throws Rmccue\Requests\Exception\HTTP On non-successful status code. Exception class corresponds to code (e.g. {@see Rmccue\Requests\Exception\HTTP\Response404})
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 166 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
114
	 * @param boolean $allow_redirects Set to false to throw on a 3xx as well
115
	 */
116
	public function throw_for_status($allow_redirects = true) {
117
		if ($this->is_redirect()) {
118
			if (!$allow_redirects) {
119
				throw new Exception('Redirection not allowed', 'response.no_redirects', $this);
120
			}
121
		}
122
		elseif (!$this->success) {
123
			$exception = Exception_HTTP::get_class($this->status_code);
124
			throw new $exception(null, $this);
125
		}
126
	}
127
}
128