GusApiClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 3
dl 0
loc 2
rs 10
c 1
b 1
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GusApi\Client;
6
7
use GusApi\Context\ContextInterface;
8
use GusApi\Exception\NotFoundException;
9
use GusApi\Type\Request\GetBulkReport;
10
use GusApi\Type\Request\GetFullReport;
11
use GusApi\Type\Request\GetValue;
12
use GusApi\Type\Request\Login;
13
use GusApi\Type\Request\Logout;
14
use GusApi\Type\Request\RequestInterface;
15
use GusApi\Type\Request\SearchData;
16
use GusApi\Type\Response\GetFullReportResponse;
17
use GusApi\Type\Response\GetValueResponse;
18
use GusApi\Type\Response\LoginResponse;
19
use GusApi\Type\Response\LogoutResponse;
20
use GusApi\Type\Response\SearchDataResponse;
21
use GusApi\Type\Response\SearchResponseRaw;
22
use GusApi\Util\BulkReportResponseDecoder;
23
use GusApi\Util\DataSearchDecoder;
24
use GusApi\Util\FullReportResponseDecoder;
25
26
class GusApiClient
27
{
28
    public const ADDRESSING_NAMESPACE = 'http://www.w3.org/2005/08/addressing';
29
30
    public function __construct(private \SoapClient $soapClient, private string $location, private ContextInterface $streamContext)
31
    {
32
    }
33
34
    public function login(Login $login): LoginResponse
35
    {
36
        return $this->call('Zaloguj', $login);
37
    }
38
39
    public function logout(Logout $logout): LogoutResponse
40
    {
41
        return $this->call('Wyloguj', $logout);
42
    }
43
44
    public function getValue(GetValue $getValue, ?string $sessionId = null): GetValueResponse
45
    {
46
        return $this->call('GetValue', $getValue, $sessionId);
47
    }
48
49
    /**
50
     * @throws NotFoundException
51
     */
52
    public function searchData(SearchData $searchData, string $sessionId): SearchDataResponse
53
    {
54
        /**
55
         * @var SearchResponseRaw $result
56
         */
57
        $result = $this->call('DaneSzukajPodmioty', $searchData, $sessionId);
58
59
        if ('' === $result->getDaneSzukajPodmiotyResult()) {
60
            throw new NotFoundException('No data found');
61
        }
62
63
        return DataSearchDecoder::decode($result);
64
    }
65
66
    public function getFullReport(GetFullReport $getFullReport, string $sessionId): GetFullReportResponse
67
    {
68
        $rawResponse = $this->call('DanePobierzPelnyRaport', $getFullReport, $sessionId);
69
70
        return FullReportResponseDecoder::decode($rawResponse);
71
    }
72
73
    public function getBulkReport(GetBulkReport $getBulkReport, string $sessionId): array
74
    {
75
        $rawResponse = $this->call('DanePobierzRaportZbiorczy', $getBulkReport, $sessionId);
76
77
        return BulkReportResponseDecoder::decode($rawResponse);
78
    }
79
80
    private function setHttpOptions(array $options): void
81
    {
82
        $this->streamContext->setOptions([
83
            'http' => $options,
84
        ]);
85
    }
86
87
    private function call(string $functionName, RequestInterface $request, ?string $sid = null)
88
    {
89
        $action = SoapActionMapper::getAction($functionName);
90
        $soapHeaders = $this->getRequestHeaders($action, $this->location);
91
        $this->soapClient->__setLocation($this->location);
92
        $this->setHttpOptions([
93
            'header' => 'sid: ' . $sid,
94
            'user_agent' => 'PHP GusApi',
95
        ]);
96
97
        return $this->soapClient->__soapCall($functionName, [$request->toArray()], [], $soapHeaders);
98
    }
99
100
    /**
101
     * @return \SoapHeader[]
102
     */
103
    private function getRequestHeaders(string $action, string $to): array
104
    {
105
        return [
106
            new \SoapHeader(self::ADDRESSING_NAMESPACE, 'Action', $action),
107
            new \SoapHeader(self::ADDRESSING_NAMESPACE, 'To', $to),
108
        ];
109
    }
110
111
    public function getSoapClient(): \SoapClient
112
    {
113
        return $this->soapClient;
114
    }
115
}
116