Client::send()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 4
nc 4
nop 1
ccs 9
cts 11
cp 0.8182
crap 4.0961
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatEET;
4
5
use SlevomatEET\Cryptography\CryptographyService;
6
use SlevomatEET\Driver\DriverRequestFailedException;
7
use SlevomatEET\Driver\SoapClientDriver;
8
use SoapFault;
9
10
class Client
11
{
12
13
	/** @var CryptographyService */
14
	private $cryptographyService;
15
16
	/** @var Configuration */
17
	private $configuration;
18
19
	/** @var SoapClient|null */
20
	private $soapClient;
21
22
	/** @var SoapClientDriver */
23
	private $soapClientDriver;
24
25 3
	public function __construct(CryptographyService $cryptographyService, Configuration $configuration, SoapClientDriver $soapClientDriver)
26
	{
27 3
		$this->cryptographyService = $cryptographyService;
28 3
		$this->configuration = $configuration;
29 3
		$this->soapClientDriver = $soapClientDriver;
30 3
	}
31
32 3
	public function send(Receipt $receipt): EvidenceResponse
33
	{
34 3
		$request = new EvidenceRequest($receipt, $this->configuration, $this->cryptographyService);
35
36
		try {
37 3
			$response = $this->getSoapClient()->OdeslaniTrzby($request->getRequestData());
38 1
		} catch (DriverRequestFailedException $e) {
39 1
			throw new FailedRequestException($request, $e);
40
		} catch (SoapFault $e) {
41
			throw new FailedRequestException($request, $e);
42
		}
43
44 2
		$response = new EvidenceResponse($response, $request);
45 2
		if (!$response->isValid()) {
46 1
			throw new InvalidResponseReceivedException($response);
47
		}
48
49 1
		return $response;
50
	}
51
52 3
	private function getSoapClient(): SoapClient
53
	{
54 3
		if ($this->soapClient === null) {
55 1
			$this->soapClient = new SoapClient($this->configuration->getEvidenceEnvironment()->getWsdlPath(), $this->cryptographyService, $this->soapClientDriver);
56
		}
57
58 3
		return $this->soapClient;
59
	}
60
61
}
62