Completed
Push — 1.11.x ( 518476...344d9e )
by José
55:02 queued 28:13
created

Requests_Exception_HTTP   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getReason() 0 3 1
A get_class() 0 12 3
1
<?php
2
/**
3
 * Exception based on HTTP response
4
 *
5
 * @package Requests
6
 */
7
8
/**
9
 * Exception based on HTTP response
10
 *
11
 * @package Requests
12
 */
13
class Requests_Exception_HTTP extends Requests_Exception {
14
	/**
15
	 * HTTP status code
16
	 *
17
	 * @var integer
18
	 */
19
	protected $code = 0;
20
21
	/**
22
	 * Reason phrase
23
	 *
24
	 * @var string
25
	 */
26
	protected $reason = 'Unknown';
27
28
	/**
29
	 * Create a new exception
30
	 *
31
	 * There is no mechanism to pass in the status code, as this is set by the
32
	 * subclass used. Reason phrases can vary, however.
33
	 *
34
	 * @param string|null $reason Reason phrase
35
	 * @param mixed $data Associated data
36
	 */
37
	public function __construct($reason = null, $data = null) {
38
		if ($reason !== null) {
39
			$this->reason = $reason;
40
		}
41
42
		$message = sprintf('%d %s', $this->code, $this->reason);
43
		parent::__construct($message, 'httpresponse', $data, $this->code);
44
	}
45
46
	/**
47
	 * Get the status message
48
	 */
49
	public function getReason() {
50
		return $this->reason;
51
	}
52
53
	/**
54
	 * Get the correct exception class for a given error code
55
	 *
56
	 * @param int|bool $code HTTP status code, or false if unavailable
57
	 * @return string Exception class name to use
58
	 */
59
	public static function get_class($code) {
60
		if (!$code) {
61
			return 'Requests_Exception_HTTP_Unknown';
62
		}
63
64
		$class = sprintf('Requests_Exception_HTTP_%d', $code);
65
		if (class_exists($class)) {
66
			return $class;
67
		}
68
69
		return 'Requests_Exception_HTTP_Unknown';
70
	}
71
}