Completed
Push — master ( 0fa021...08bd3f )
by smiley
01:32
created

GuzzleClient::getResponse()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.439
c 0
b 0
f 0
cc 6
eloc 20
nc 4
nop 0
1
<?php
2
/**
3
 * Class GuzzleClient
4
 *
5
 * @filesource   GuzzleClient.php
6
 * @created      23.10.2017
7
 * @package      chillerlan\HTTP
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\HTTP;
14
15
use chillerlan\Traits\ContainerInterface;
16
use GuzzleHttp\Client;
17
use Psr\Http\Message\StreamInterface;
18
19
/**
20
 * @property \GuzzleHttp\Client $http
21
 */
22
class GuzzleClient extends HTTPClientAbstract{
23
24
	/**
25
	 * GuzzleClient constructor.
26
	 *
27
	 * @param \chillerlan\Traits\ContainerInterface $options
28
	 * @param \GuzzleHttp\Client|null               $http
29
	 */
30
	public function __construct(ContainerInterface $options, Client $http = null){
31
		parent::__construct($options);
32
33
		if($http instanceof Client){
34
			$this->setClient($http);
35
		}
36
	}
37
38
	/** @inheritdoc */
39
	public function setClient(Client $http):HTTPClientInterface{
40
		$this->http = $http;
41
42
		return $this;
43
	}
44
45
	/** @inheritdoc */
46
	protected function getResponse():HTTPResponseInterface{
47
		// @link http://docs.guzzlephp.org/en/stable/request-options.html
48
		$options = [
49
			'query'       => $this->requestParams,
50
			'headers'     => $this->requestHeaders,
51
			'http_errors' => false, // no exceptions on HTTP errors plz
52
		];
53
54
		if(in_array($this->requestMethod, ['PATCH', 'POST', 'PUT', 'DELETE'], true)){
55
56
			if(is_scalar($this->requestBody) || $this->requestBody instanceof StreamInterface){
57
				$options['body'] = $this->requestBody; // @codeCoverageIgnore
58
			}
59
			elseif(in_array($this->requestMethod, ['PATCH', 'POST', 'PUT'], true) && is_array($this->requestBody)){
60
				$options['form_params'] = $this->requestBody;
61
			}
62
63
		}
64
65
		$url = explode('?', $this->requestURL)[0];
66
67
		$response = $this->http->request($this->requestMethod, $url, $options); // @todo: merge query params
68
69
		$responseHeaders              = $this->parseResponseHeaders($response->getHeaders());
70
		$responseHeaders->statuscode  = $response->getStatusCode();
71
		$responseHeaders->statustext  = $response->getReasonPhrase();
72
		$responseHeaders->httpversion = $response->getProtocolVersion();
73
74
		return new HTTPResponse([
75
			'url'     => $url,
76
			'headers' => $responseHeaders,
77
			'body'    => $response->getBody(),
78
		]);
79
	}
80
81
	/**
82
	 * @param array $headers
83
	 *
84
	 * @return \stdClass
85
	 */
86
	protected function parseResponseHeaders(array $headers):\stdClass {
87
		$h = new \stdClass;
88
89
		foreach($headers as $k => $v){
90
			$h->{strtolower($k)} = $v[0] ?? null;
91
		}
92
93
		return $h;
94
	}
95
96
}
97