CurlClient   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 12
c 3
b 0
f 0
dl 0
loc 27
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A sendRequest() 0 20 3
1
<?php
2
/**
3
 * Class HTTPClient
4
 *
5
 * @created      27.08.2018
6
 * @author       smiley <[email protected]>
7
 * @copyright    2018 smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\HTTP\Psr18;
12
13
use chillerlan\HTTP\CurlUtils\CurlHandle;
14
use Psr\Http\Message\{RequestInterface, ResponseInterface};
15
use function in_array, sprintf;
16
use const CURLE_OK;
17
18
class CurlClient extends HTTPClientAbstract{
19
20
	protected CurlHandle $handle;
21
22
	/**
23
	 * @inheritDoc
24
	 */
25
	public function sendRequest(RequestInterface $request):ResponseInterface{
26
		$stream       = $this->streamFactory?->createStream();
27
		$this->handle = new CurlHandle($request, $this->responseFactory->createResponse(), $this->options, $stream);
28
		$errno        = $this->handle->exec();
29
30
		if($errno !== CURLE_OK){
31
			$error = $this->handle->getError();
32
33
			$this->logger->error(sprintf('cURL error #%s: %s', $errno, $error));
34
35
			if(in_array($errno, $this->handle::CURL_NETWORK_ERRORS, true)){
36
				throw new NetworkException($error, $request);
37
			}
38
39
			throw new RequestException($error, $request);
40
		}
41
42
		$this->handle->close();
43
44
		return $this->handle->getResponse();
45
	}
46
47
}
48