ResponseAbstract   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 1
dl 0
loc 150
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
exec() 0 1 ?
A headerLine() 0 16 3
A getBody() 0 9 1
A getInfo() 0 13 3
A response_headers_array() 0 9 2
A __construct() 0 13 2
C __get() 0 22 8
1
<?php
2
/**
3
 * Class ResponseAbstract
4
 *
5
 * @filesource   ResponseAbstract.php
6
 * @created      06.04.2016
7
 * @package      chillerlan\TinyCurl
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2015 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\TinyCurl;
14
15
use stdClass;
16
17
/**
18
 * @todo: Container
19
 *
20
 * @property mixed body
21
 * @property mixed error
22
 * @property mixed headers
23
 * @property mixed headers_array
24
 * @property mixed info
25
 * @property mixed json
26
 * @property mixed json_array
27
 */
28
abstract class ResponseAbstract implements ResponseInterface{
29
30
	/**
31
	 * @var resource
32
	 */
33
	protected $curl;
34
35
	/**
36
	 * @var \stdClass
37
	 */
38
	protected $curl_info;
39
40
	/**
41
	 * @var \stdClass
42
	 */
43
	protected $response_headers;
44
45
	/**
46
	 * @var \stdClass
47
	 */
48
	protected $response_error;
49
50
	/**
51
	 * @var mixed
52
	 */
53
	protected $response_body;
54
55
	/**
56
	 * Response constructor.
57
	 *
58
	 * @param resource $curl
59
	 *
60
	 * @throws \chillerlan\TinyCurl\ResponseException
61
	 */
62
	public function __construct($curl){
63
64
		if(!is_resource($curl)){
65
			throw new ResponseException('no cURL handle given');
66
		}
67
68
		$this->curl             = $curl;
69
		$this->curl_info        = new stdClass;
70
		$this->response_error   = new stdClass;
71
		$this->response_headers = new stdClass;
72
73
		$this->exec();
74
	}
75
76
	/** @inheritdoc */
77
	public function __get($property){
78
79
		switch($property){
80
			case 'body':
81
				return $this->getBody();
82
			case 'info':
83
				return $this->curl_info;
84
			case 'json':
85
				return json_decode($this->response_body);
86
			case 'json_array':
87
				return json_decode($this->response_body, true);
88
			case 'error':
89
				return $this->response_error;
90
			case 'headers':
91
				return $this->response_headers;
92
			case 'headers_array':
93
				return $this->response_headers_array((array)$this->response_headers);
94
			default:
95
				return false;
96
		}
97
98
	}
99
100
	/**
101
	 * executes the cURL call, fills self::$response_body and calls self::getInfo()
102
	 *
103
	 * @return void
104
	 */
105
	abstract protected function exec();
106
107
	/**
108
	 * @param resource $curl
109
	 * @param string   $header_line
110
	 *
111
	 * @return int
112
	 *
113
	 * @link http://php.net/manual/function.curl-setopt.php CURLOPT_HEADERFUNCTION
114
	 */
115
	protected function headerLine(/** @noinspection PhpUnusedParameterInspection */$curl, $header_line){
116
		$header = explode(':', $header_line, 2);
117
118
		if(count($header) === 2){
119
			$this->response_headers->{trim(strtolower($header[0]))} = trim($header[1]);
120
		}
121
		elseif(substr($header_line, 0, 4) === 'HTTP'){
122
			$status = explode(' ', $header_line, 3);
123
124
			$this->response_headers->httpversion = explode('/', $status[0], 2)[1];
125
			$this->response_headers->statuscode  = intval($status[1]);
126
			$this->response_headers->statustext  = trim($status[2]);
127
		}
128
129
		return strlen($header_line);
130
	}
131
132
	/**
133
	 * @return \stdClass
134
	 */
135
	protected function getBody(){
136
		$body = new stdClass;
137
138
		$body->content      = $this->response_body;
139
		$body->length       = strlen($this->response_body);
140
		$body->content_type = $this->response_headers->content_type ?? $this->curl_info->content_type ?? null;
141
142
		return $body;
143
	}
144
145
	/**
146
	 * @return void
147
	 */
148
	protected function getInfo(){
149
		$curl_info = curl_getinfo($this->curl);
150
151
		if(is_array($curl_info)){
152
			foreach($curl_info as $key => $value){
153
				$this->curl_info->{$key} = $value;
154
			}
155
		}
156
157
		$this->response_error->code    = curl_errno($this->curl);
158
		$this->response_error->message = curl_error($this->curl);
159
		$this->response_error->version = curl_version();
160
	}
161
162
	/**
163
	 * @param array $response_headers
164
	 *
165
	 * @return array
166
	 */
167
	protected function response_headers_array(array $response_headers):array {
168
		$headers = [];
169
170
		foreach($response_headers as $key => $value){
171
			$headers[$key] = $value;
172
		}
173
174
		return $headers;
175
	}
176
177
}
178