GuzzleSoapClientDriver   A
last analyzed

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
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\RequestException;
7
use GuzzleHttp\Psr7\Request;
8
use GuzzleHttp\RequestOptions;
9
10
class GuzzleSoapClientDriver implements SoapClientDriver
11
{
12
13
	public const DEFAULT_TIMEOUT = 2.5;
14
	public const HEADER_USER_AGENT = 'PHP';
15
16
	/** @var Client */
17
	private $httpClient;
18
19
	/** @var float */
20
	private $connectionTimeout;
21
22
	/** @var float */
23
	private $requestTimeout;
24
25 1
	public function __construct(Client $httpClient, float $connectionTimeout = self::DEFAULT_TIMEOUT, float $requestTimeout = self::DEFAULT_TIMEOUT)
26
	{
27 1
		$this->httpClient = $httpClient;
28 1
		$this->connectionTimeout = $connectionTimeout;
29 1
		$this->requestTimeout = $requestTimeout;
30 1
	}
31
32 1
	public function send(string $request, string $location, string $action, int $soapVersion): string
33
	{
34
		$headers = [
35 1
			'User-Agent' => self::HEADER_USER_AGENT,
36 1
			'Content-Type' => sprintf('%s; charset=utf-8', $soapVersion === 2 ? 'application/soap+xml' : 'text/xml'),
37 1
			'SOAPAction' => $action,
38 1
			'Content-Length' => strlen($request),
39
		];
40
41 1
		$request = new Request('POST', $location, $headers, $request);
42
		try {
43 1
			$httpResponse = $this->httpClient->send($request, [
44 1
				RequestOptions::HTTP_ERRORS => false,
45 1
				RequestOptions::ALLOW_REDIRECTS => false,
46 1
				RequestOptions::CONNECT_TIMEOUT => $this->connectionTimeout,
47 1
				RequestOptions::TIMEOUT => $this->requestTimeout,
48
			]);
49
50 1
			return (string) $httpResponse->getBody();
51
		} catch (RequestException $e) {
52
			throw new DriverRequestFailedException($e);
53
		}
54
	}
55
56
}
57