Completed
Pull Request — master (#8)
by Josef
08:42
created

GuzzleSoapClientDriver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatEET\Driver;
4
5
class GuzzleSoapClientDriver implements SoapClientDriver
6
{
7
8
	const DEFAULT_TIMEOUT = 2.5;
9
	const HEADER_USER_AGENT = 'PHP';
10
11
	/** @var \GuzzleHttp\Client */
12
	private $httpClient;
13
14
	/** @var float */
15
	private $connectionTimeout;
16
17
	/** @var float */
18
	private $requestTimeout;
19
20 1
	public function __construct(\GuzzleHttp\Client $httpClient, float $connectionTimeout = self::DEFAULT_TIMEOUT, float $requestTimeout = self::DEFAULT_TIMEOUT)
21
	{
22 1
		$this->httpClient = $httpClient;
23 1
		$this->connectionTimeout = $connectionTimeout;
24 1
		$this->requestTimeout = $requestTimeout;
25 1
	}
26
27 1
	public function send(string $request, string $location, string $action, int $soapVersion): string
28
	{
29
		$headers = [
30 1
			'User-Agent' => self::HEADER_USER_AGENT,
31 1
			'Content-Type' => sprintf('%s; charset=utf-8', $soapVersion === 2 ? 'application/soap+xml' : 'text/xml'),
32 1
			'SOAPAction' => $action,
33 1
			'Content-Length' => strlen($request),
34
		];
35
36 1
		$request = new \GuzzleHttp\Psr7\Request('POST', $location, $headers, $request);
37
		try {
38 1
			$httpResponse = $this->httpClient->send($request, [
39 1
				\GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
40 1
				\GuzzleHttp\RequestOptions::ALLOW_REDIRECTS => false,
41 1
				\GuzzleHttp\RequestOptions::CONNECT_TIMEOUT => $this->connectionTimeout,
42 1
				\GuzzleHttp\RequestOptions::TIMEOUT => $this->requestTimeout,
43
			]);
44
45 1
			return (string) $httpResponse->getBody();
46
		} catch (\GuzzleHttp\Exception\RequestException $e) {
47
			throw new DriverRequestFailedException($e);
48
		}
49
	}
50
51
}
52