Passed
Push — master ( e89c90...b07112 )
by Bas
03:34
created

Client::process()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 10
cc 3
nc 2
nop 1
crap 3.1406
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace DMT\Insolvency;
4
5
use DMT\CommandBus\Validator\ValidationMiddleware;
6
use DMT\Insolvency\Exception\Exception;
7
use DMT\Insolvency\Exception\ExceptionMiddleware;
8
use DMT\Insolvency\Http\GetReportHandler;
9
use DMT\Insolvency\Http\Middleware\ExceptionMiddleware as HttpExceptionMiddleware;
10
use DMT\Insolvency\Http\Middleware\SoapActionMiddleware;
11
use DMT\Insolvency\Http\Request\GetReport;
12
use DMT\Insolvency\Http\Response\GetReportResponse;
13
use DMT\Insolvency\Soap\Handler as SoapHandler;
14
use DMT\Insolvency\Soap\Request;
15
use DMT\Insolvency\Soap\Request as SoapRequest;
16
use DMT\Insolvency\Soap\Response;
17
use DMT\Insolvency\Soap\Serializer\SoapSerializer;
18
use GuzzleHttp\Client as HttpClient;
19
use GuzzleHttp\Handler\CurlHandler;
20
use GuzzleHttp\HandlerStack;
21
use GuzzleHttp\Middleware;
22
use JMS\Serializer\SerializerInterface;
23
use League\Tactician\CommandBus;
24
use League\Tactician\Handler\CommandHandlerMiddleware;
25
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
26
use League\Tactician\Handler\Locator\CallableLocator;
27
use League\Tactician\Handler\MethodNameInflector\HandleInflector;
28
use League\Tactician\Plugins\LockingMiddleware;
29
30
/**
31
 * Class Client
32
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
33
class Client
34
{
35
    /** @var Config $config */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
36
    protected $config;
37
38
    /** @var SerializerInterface|null $serializer */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
39
    protected $serializer;
40
41
    /** @var CommandBus $commandBus */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
42
    protected $commandBus;
43
44
    /**
45
     * Client constructor.
46
     *
47
     * @param Config $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
48
     * @param SerializerInterface|null $serializer
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
49
     */
50 13
    public function __construct(Config $config, SerializerInterface $serializer = null)
51
    {
52 13
        $this->config = $config;
53 13
        $this->serializer = $serializer ?? new SoapSerializer($this, $config);
54
55 13
        $this->commandBus = new CommandBus([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
56 13
            new LockingMiddleware(),
57 13
            new ExceptionMiddleware(),
58 13
            new ValidationMiddleware(),
59 13
            new CommandHandlerMiddleware(
60 13
                new ClassNameExtractor(),
61 13
                new CallableLocator([$this, 'getHandler']),
62 13
                new HandleInflector()
63
            )
64
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
65 13
    }
66
67
    /**
68
     * Search for publications of a specific insolvency.
69
     *
70
     * @param string $insolvencyID the insolvency identification.
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
71
     * @param string|null $court the count (number) where the insolvency is registered.
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 1 found
Loading history...
72
     *
73
     * @return Response|Response\SearchInsolvencyIDResponse
74
     * @throws Exception
75
     */
76 1
    public function searchInsolvencyId(string $insolvencyID, string $court = null): Response\SearchInsolvencyIDResponse
77
    {
78 1
        $request = new Request\SearchInsolvencyID();
79 1
        $request->insolvencyID = $insolvencyID;
80 1
        $request->court = $court;
81
82 1
        return $this->process($request);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->process($request) returns the type DMT\Insolvency\Http\Response\GetReportResponse which is incompatible with the type-hinted return DMT\Insolvency\Soap\Resp...rchInsolvencyIDResponse.
Loading history...
83
    }
84
85
    /**
86
     * Search for publications for a person.
87
     *
88
     * @param \DateTime|null $dateOfBirth the date of birth of the person.
89
     * @param string|null $prefix the surname prefix.
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter name; 1 found
Loading history...
90
     * @param string|null $surname the surname of the person.
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
91
     * @param int|null $houseNumber the house number of the person's address.
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
92
     * @param string|null $postalCode the postcode of the person's address.
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
93
     *
94
     * @return Response|Response\SearchNaturalPersonResponse
95
     * @throws Exception
96
     */
97 1
    public function searchNaturalPerson(
98
        \DateTime $dateOfBirth = null,
99
        string $prefix = null,
100
        string $surname = null,
101
        int $houseNumber = null,
102
        string $postalCode = null
103
    ): Response\SearchNaturalPersonResponse {
104 1
        $request = new Request\SearchNaturalPerson();
105 1
        $request->dateOfBirth = $dateOfBirth;
106 1
        $request->prefix = $prefix;
107 1
        $request->surname = $surname;
108 1
        $request->houseNumber = $houseNumber;
109 1
        $request->postalCode = $postalCode;
110
111 1
        return $this->process($request);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->process($request) returns the type DMT\Insolvency\Http\Response\GetReportResponse which is incompatible with the type-hinted return DMT\Insolvency\Soap\Resp...chNaturalPersonResponse.
Loading history...
112
    }
113
114
    /**
115
     * Search for publications of an undertaking.
116
     *
117
     * @param string|null $name the name of the undertaking.
0 ignored issues
show
Coding Style introduced by
Expected 17 spaces after parameter name; 1 found
Loading history...
118
     * @param string|null $commercialRegisterID the chamber of commerce number.
119
     * @param string|null $postalCode the postcode of the undertaking's address.
0 ignored issues
show
Coding Style introduced by
Expected 11 spaces after parameter name; 1 found
Loading history...
120
     * @param int|null $houseNumber the house number of the undertaking' address.
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter name; 1 found
Loading history...
121
     *
122
     * @return Response|Response\SearchUndertakingResponse
123
     * @throws Exception
124
     */
125 1
    public function searchUndertaking(
126
        string $name = null,
127
        string $commercialRegisterID = null,
128
        string $postalCode = null,
129
        int $houseNumber = null
130
    ): Response\SearchUndertakingResponse {
131 1
        $request = new Request\SearchUndertaking();
132 1
        $request->name = $name;
133 1
        $request->commercialRegisterID = $commercialRegisterID;
134 1
        $request->postalCode = $postalCode;
135 1
        $request->houseNumber = $houseNumber;
136
137 1
        return $this->process($request);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->process($request) returns the type DMT\Insolvency\Http\Response\GetReportResponse which is incompatible with the type-hinted return DMT\Insolvency\Soap\Resp...archUndertakingResponse.
Loading history...
138
    }
139
140
    /**
141
     * Get insolvency case.
142
     *
143
     * @param string $publicationNumber the publication number of the case.
144
     *
145
     * @return Response|Response\GetCaseResponse
146
     * @throws Exception
147
     */
148 1
    public function getCase(string $publicationNumber): Response\GetCaseResponse
149
    {
150 1
        $request = new Request\GetCase();
151 1
        $request->publicationNumber = $publicationNumber;
152
153 1
        return $this->process($request);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->process($request) returns the type DMT\Insolvency\Http\Response\GetReportResponse which is incompatible with the type-hinted return DMT\Insolvency\Soap\Response\GetCaseResponse.
Loading history...
154
    }
155
156
    /**
157
     * Get insolvency case with references to its reports.
158
     *
159
     * @param string $publicationNumber the publication number of the case.
160
     *
161
     * @return Response|Response\GetCaseWithReportsResponse
162
     * @throws Exception
163
     */
164 6
    public function getCaseWithReports(string $publicationNumber): Response\GetCaseWithReportsResponse
165
    {
166 6
        $request = new Request\GetCaseWithReports();
167 6
        $request->publicationNumber = $publicationNumber;
168
169 6
        return $this->process($request);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->process($request) returns the type DMT\Insolvency\Http\Response\GetReportResponse which is incompatible with the type-hinted return DMT\Insolvency\Soap\Resp...CaseWithReportsResponse.
Loading history...
170
    }
171
172
    /**
173
     * Get a report for an insolvency.
174
     *
175
     * @param string $reportId the report identification number.
176
     *
177
     * @return GetReportResponse
178
     * @throws Exception
179
     */
180 3
    public function getReport(string $reportId): GetReportResponse
181
    {
182 3
        $request = new GetReport();
183 3
        $request->reportId = $reportId;
184
185 3
        return $this->process($request);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->process($request) could return the type DMT\Insolvency\Soap\Response which is incompatible with the type-hinted return DMT\Insolvency\Http\Response\GetReportResponse. Consider adding an additional type-check to rule them out.
Loading history...
186
    }
187
188
    /**
189
     * Get the date when the latest publication is added.
190
     *
191
     * @return Response|Response\GetLastUpdateResponse
192
     * @throws Exception
193
     */
194 1
    public function getLastUpdate(): Response\GetLastUpdateResponse
195
    {
196 1
        return $this->process(new Request\GetLastUpdate());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->process(ne...equest\GetLastUpdate()) returns the type DMT\Insolvency\Http\Response\GetReportResponse which is incompatible with the type-hinted return DMT\Insolvency\Soap\Response\GetLastUpdateResponse.
Loading history...
197
    }
198
199
    /**
200
     * Search for publications of a specific date.
201
     *
202
     * @param \DateTime $date tne date of the publications to look up.
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
203
     * @param string $court the court (number) where the publications are registered.
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
204
     * @param string|null $pubType the type of publication.
205
     *
206
     * @return Response|Response\SearchByDateResponse
207
     * @throws Exception
208
     */
209 1
    public function searchByDate(\DateTime $date, string $court, string $pubType = null): Response\SearchByDateResponse
210
    {
211 1
        $request = new Request\SearchByDate();
212 1
        $request->date = $date;
213 1
        $request->court = $court;
214 1
        $request->pubType = $pubType;
215
216 1
        return $this->process($request);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->process($request) returns the type DMT\Insolvency\Http\Response\GetReportResponse which is incompatible with the type-hinted return DMT\Insolvency\Soap\Response\SearchByDateResponse.
Loading history...
217
    }
218
219
    /**
220
     * Search for publications of the last mutated/added insolvencies for data replication.
221
     *
222
     * @param \DateTime $modifyDate the modified date since.
223
     *
224
     * @return Response|Response\SearchModifiedSinceResponse
225
     * @throws Exception
226
     */
227 1
    public function searchModifiedSince(\DateTime $modifyDate): Response\SearchModifiedSinceResponse
228
    {
229 1
        $request = new Request\SearchModifiedSince();
230 1
        $request->modifyDate = $modifyDate;
231
232 1
        return $this->process($request);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->process($request) returns the type DMT\Insolvency\Http\Response\GetReportResponse which is incompatible with the type-hinted return DMT\Insolvency\Soap\Resp...chModifiedSinceResponse.
Loading history...
233
    }
234
235
    /**
236
     * Search for removed publications for data replication.
237
     *
238
     * @param \DateTime $modifyDate the modified date since.
239
     *
240
     * @return Response|Response\SearchRemovedSinceResponse
241
     * @throws Exception
242
     */
243 1
    public function searchRemovedSince(\DateTime $modifyDate): Response\SearchRemovedSinceResponse
244
    {
245 1
        $request = new Request\SearchRemovedSince();
246 1
        $request->modifyDate = $modifyDate;
247
248 1
        return $this->process($request);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->process($request) returns the type DMT\Insolvency\Http\Response\GetReportResponse which is incompatible with the type-hinted return DMT\Insolvency\Soap\Resp...rchRemovedSinceResponse.
Loading history...
249
    }
250
251
    /**
252
     * Search for added and modified reposts in a given time period.
253
     *
254
     * @param \DateTime $datetimeFrom the start date.
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
255
     * @param \DateTime|null $datetimeTo the end date.
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
256
     *
257
     * @return Response|Response\SearchReportsSinceResponse
258
     * @throws Exception
259
     */
260 1
    public function searchReportsSince(
261
        \DateTime $datetimeFrom,
262
        \DateTime $datetimeTo = null
263
    ): Response\SearchReportsSinceResponse {
264 1
        $request = new Request\SearchReportsSince();
265 1
        $request->datetimeFrom = $datetimeFrom;
266 1
        $request->datetimeTo = $datetimeTo ?? new \DateTime();
267
268 1
        return $this->process($request);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->process($request) returns the type DMT\Insolvency\Http\Response\GetReportResponse which is incompatible with the type-hinted return DMT\Insolvency\Soap\Resp...rchReportsSinceResponse.
Loading history...
269
    }
270
271
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
272
     * @param string $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 4 spaces but found 1
Loading history...
273
     * @return SoapHandler|object
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
Coding Style introduced by
Tag value for @return tag indented incorrectly; expected 3 spaces but found 1
Loading history...
274
     * @internal
0 ignored issues
show
Coding Style introduced by
Tag @internal cannot be grouped with parameter tags in a doc comment
Loading history...
275
     */
276
    public function getHandler(string $request)
277
    {
278
        if (is_a($request, SoapRequest::class, true)) {
279
            return new SoapHandler($this->getHttpClient(), $this->serializer);
0 ignored issues
show
Bug introduced by
It seems like $this->serializer can also be of type null; however, parameter $serializer of DMT\Insolvency\Soap\Handler::__construct() does only seem to accept JMS\Serializer\SerializerInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

279
            return new SoapHandler($this->getHttpClient(), /** @scrutinizer ignore-type */ $this->serializer);
Loading history...
280
        }
281
282
        return new GetReportHandler($this->getHttpClient(false));
283
    }
284
285
    /**
286
     * Process a request.
287
     *
288
     * @param Request|GetReport $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
289
     *
290
     * @return Response|GetReportResponse
291
     * @throws Exception
292
     */
293 11
    protected function process($request)
294
    {
295 11
        if (!$request instanceof Request && !$request instanceof GetReport) {
0 ignored issues
show
introduced by
$request is always a sub-type of DMT\Insolvency\Http\Request\GetReport.
Loading history...
296
            throw new \TypeError('Invalid request');
297
        }
298
299 11
        return $this->commandBus->handle($request);
300
    }
301
302
    /**
303
     * Get http client.
304
     *
305
     * @param bool $forSoap
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
306
     * @return HttpClient
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
307
     */
308
    protected function getHttpClient(bool $forSoap = true): HttpClient
309
    {
310
        $stack = HandlerStack::create(new CurlHandler());
311
        $stack->push(Middleware::mapResponse(new HttpExceptionMiddleware()));
312
313
        if ($forSoap) {
314
            $stack->push(Middleware::mapRequest(new SoapActionMiddleware()));
315
        }
316
317
        return new HttpClient([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
318
            'base_uri' => $forSoap ? $this->config->endPoint : $this->config->documentUri,
319
            'handler' => $stack,
320
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
321
    }
322
}
323