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

TinyCurlClient::getResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
/**
3
 * Class TinyCurlClient
4
 *
5
 * @filesource   TinyCurlClient.php
6
 * @created      09.07.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\TinyCurl\{Request, URL};
16
use chillerlan\Traits\ContainerInterface;
17
18
/**
19
 * @property \chillerlan\TinyCurl\Request $http
20
 */
21
class TinyCurlClient extends HTTPClientAbstract{
22
23
	/**
24
	 * TinyCurlClient constructor.
25
	 *
26
	 * @param \chillerlan\Traits\ContainerInterface $options
27
	 * @param \chillerlan\TinyCurl\Request|null     $http
28
	 */
29
	public function __construct(ContainerInterface $options, Request $http = null){
30
		parent::__construct($options);
31
32
		if($http instanceof Request){
33
			$this->setClient($http);
34
		}
35
	}
36
37
	/** @inheritdoc */
38
	public function setClient(Request $http):HTTPClientInterface{
39
		$this->http = $http;
40
41
		return $this;
42
	}
43
44
	/** @inheritdoc */
45
	protected function getResponse():HTTPResponseInterface{
46
47
		$url = new URL(
48
			explode('?', $this->requestURL)[0],
49
			$this->requestParams,
50
			$this->requestMethod,
51
			$this->requestBody,
52
			$this->requestHeaders
53
		);
54
55
		$response = $this->http->fetch($url);
56
57
		return new HTTPResponse([
58
			'url'     => $url->__toString(),
59
			'headers' => $response->headers,
60
			'body'    => $response->body->content,
61
		]);
62
	}
63
64
}
65