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

GuzzleSoapClientDriver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 47
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A send() 0 23 3
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