Completed
Pull Request — master (#9)
by Josef
04:32 queued 02:28
created

GuzzleSoapClientDriver::send()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 13
cp 0
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 4
crap 12
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
	public function __construct(\GuzzleHttp\Client $httpClient, float $connectionTimeout = self::DEFAULT_TIMEOUT, float $requestTimeout = self::DEFAULT_TIMEOUT)
21
	{
22
		$this->httpClient = $httpClient;
23
		$this->connectionTimeout = $connectionTimeout;
24
		$this->requestTimeout = $requestTimeout;
25
	}
26
27
	public function send(string $request, string $location, string $action, int $soapVersion): string
28
	{
29
		$headers = [
30
			'User-Agent' => self::HEADER_USER_AGENT,
31
			'Content-Type' => sprintf('%s; charset=utf-8', $soapVersion === 2 ? 'application/soap+xml' : 'text/xml'),
32
			'SOAPAction' => $action,
33
			'Content-Length' => strlen($request),
34
		];
35
36
		$request = new \GuzzleHttp\Psr7\Request('POST', $location, $headers, $request);
37
		try {
38
			$httpResponse = $this->httpClient->send($request, [
39
				\GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
40
				\GuzzleHttp\RequestOptions::ALLOW_REDIRECTS => false,
41
				\GuzzleHttp\RequestOptions::CONNECT_TIMEOUT => $this->connectionTimeout,
42
				\GuzzleHttp\RequestOptions::TIMEOUT => $this->requestTimeout,
43
			]);
44
45
			return (string) $httpResponse->getBody();
46
		} catch (\GuzzleHttp\Exception\RequestException $e) {
47
			throw new DriverRequestFailedException($e);
48
		}
49
	}
50
51
}
52