Passed
Push — master ( b07112...16f901 )
by Bas
04:04 queued 14s
created

Client::getRequestHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 11
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace DMT\Insolvency;
4
5
use DateTime;
6
use DMT\CommandBus\Validator\ValidationMiddleware;
7
use DMT\Http\Client\RequestHandler;
8
use DMT\Insolvency\Exception\Exception;
9
use DMT\Insolvency\Exception\ExceptionMiddleware;
10
use DMT\Insolvency\Http\GetReportHandler;
11
use DMT\Insolvency\Http\Middleware\ExceptionMiddleware as HttpExceptionMiddleware;
12
use DMT\Insolvency\Http\Middleware\SoapActionMiddleware;
13
use DMT\Insolvency\Http\Request\GetReport;
14
use DMT\Insolvency\Http\Response\GetReportResponse;
15
use DMT\Insolvency\Model\BeschikbareVerslagen;
16
use DMT\Insolvency\Model\Document;
17
use DMT\Insolvency\Model\Insolvente;
18
use DMT\Insolvency\Model\LastUpdate;
19
use DMT\Insolvency\Model\PublicatieLijst;
20
use DMT\Insolvency\Model\VerwijderdePublicatieLijst;
21
use DMT\Insolvency\Soap\Handler as SoapHandler;
22
use DMT\Insolvency\Soap\Request;
23
use DMT\Insolvency\Soap\Request as SoapRequest;
24
use DMT\Insolvency\Soap\Response;
25
use DMT\Insolvency\Soap\Serializer\SoapSerializer;
26
use GuzzleHttp\Client as HttpClient;
27
use JMS\Serializer\SerializerInterface;
28
use League\Tactician\CommandBus;
29
use League\Tactician\Handler\CommandHandlerMiddleware;
30
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
31
use League\Tactician\Handler\Locator\CallableLocator;
32
use League\Tactician\Handler\MethodNameInflector\HandleInflector;
33
use League\Tactician\Plugins\LockingMiddleware;
34
use Psr\Http\Client\ClientInterface;
35
use Psr\Http\Message\RequestFactoryInterface;
36
37
/**
38
 * Class Client
39
 */
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...
40
class Client
41
{
42
    private Config $config;
0 ignored issues
show
Coding Style introduced by
Private member variable "config" must be prefixed with an underscore
Loading history...
43
    private ClientInterface $client;
0 ignored issues
show
Coding Style introduced by
Private member variable "client" must be prefixed with an underscore
Loading history...
44
    private RequestFactoryInterface $requestFactory;
0 ignored issues
show
Coding Style introduced by
Private member variable "requestFactory" must be prefixed with an underscore
Loading history...
45
    private SerializerInterface $serializer;
0 ignored issues
show
Coding Style introduced by
Private member variable "serializer" must be prefixed with an underscore
Loading history...
46
    private CommandBus $commandBus;
0 ignored issues
show
Coding Style introduced by
Private member variable "commandBus" must be prefixed with an underscore
Loading history...
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @param Config $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 18 spaces after parameter type; 1 found
Loading history...
50
     * @param ClientInterface $client
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
51
     * @param RequestFactoryInterface $requestFactory
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
52
     * @param SoapSerializer|null $serializer
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
53
     */
54 11
    public function __construct(
55
        Config $config,
56
        ClientInterface $client,
57
        RequestFactoryInterface $requestFactory,
58
        SoapSerializer $serializer = null
59
    ) {
60 11
        $this->config = $config;
61 11
        $this->client = $client;
62 11
        $this->requestFactory = $requestFactory;
63 11
        $this->serializer = $serializer ?? new SoapSerializer($this, $config);
64
65 11
        $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...
66 11
            new LockingMiddleware(),
67 11
            new ExceptionMiddleware(),
68 11
            new ValidationMiddleware(),
69 11
            new CommandHandlerMiddleware(
70 11
                new ClassNameExtractor(),
71 11
                new CallableLocator([$this, 'getHandler']),
72 11
                new HandleInflector()
73
            )
74
        ]);
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...
75 11
    }
76
77
    /**
78
     * Search for publications of a specific insolvency.
79
     *
80
     * @param string $insolvencyID the insolvency identification.
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
81
     * @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...
82
     *
83
     * @return PublicatieLijst
84
     * @throws Exception
85
     */
86 1
    public function searchInsolvencyId(string $insolvencyID, string $court = null): PublicatieLijst
87
    {
88 1
        $request = new Request\SearchInsolvencyID();
89 1
        $request->insolvencyID = $insolvencyID;
90 1
        $request->court = $court;
91
92
        /** @var Response\SearchInsolvencyIDResponse $response */
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...
93 1
        $response = $this->process($request);
94
95 1
        return $response->result->publicatieLijst;
96
    }
97
98
    /**
99
     * Search for publications for a person.
100
     *
101
     * @param DateTime|null $dateOfBirth the date of birth of the person.
102
     * @param string|null $prefix the surname prefix.
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter name; 1 found
Loading history...
103
     * @param string|null $surname the surname of the person.
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
104
     * @param int|null $houseNumber the house number of the person's address.
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
105
     * @param string|null $postalCode the postcode of the person's address.
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
106
     *
107
     * @return PublicatieLijst
108
     * @throws Exception
109
     */
110 1
    public function searchNaturalPerson(
111
        DateTime $dateOfBirth = null,
112
        string $prefix = null,
113
        string $surname = null,
114
        int $houseNumber = null,
115
        string $postalCode = null
116
    ): PublicatieLijst {
117 1
        $request = new Request\SearchNaturalPerson();
118 1
        $request->dateOfBirth = $dateOfBirth;
119 1
        $request->prefix = $prefix;
120 1
        $request->surname = $surname;
121 1
        $request->houseNumber = $houseNumber;
122 1
        $request->postalCode = $postalCode;
123
124
        /** @var Response\SearchNaturalPersonResponse $response */
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...
125 1
        $response = $this->process($request);
126
127 1
        return $response->result->publicatieLijst;
128
    }
129
130
    /**
131
     * Search for publications of an undertaking.
132
     *
133
     * @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...
134
     * @param string|null $commercialRegisterID the chamber of commerce number.
135
     * @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...
136
     * @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...
137
     *
138
     * @return PublicatieLijst
139
     * @throws Exception
140
     */
141 1
    public function searchUndertaking(
142
        string $name = null,
143
        string $commercialRegisterID = null,
144
        string $postalCode = null,
145
        int $houseNumber = null
146
    ): PublicatieLijst {
147 1
        $request = new Request\SearchUndertaking();
148 1
        $request->name = $name;
149 1
        $request->commercialRegisterID = $commercialRegisterID;
150 1
        $request->postalCode = $postalCode;
151 1
        $request->houseNumber = $houseNumber;
152
153
        /** @var Response\SearchUndertakingResponse $response */
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...
154 1
        $response = $this->process($request);
155
156 1
        return $response->result->publicatieLijst;
157
    }
158
159
    /**
160
     * Get insolvency case.
161
     *
162
     * @param string $publicationNumber the publication number of the case.
163
     * @param bool $includeReports set to true to include reports.
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
164
     *
165
     * @return Insolvente
166
     * @throws Exception
167
     */
168 7
    public function getCase(string $publicationNumber, bool $includeReports = false): Insolvente
169
    {
170 7
        $request = $includeReports ? new Request\GetCaseWithReports() : new Request\GetCase();
171 7
        $request->publicationNumber = $publicationNumber;
172
173
        /** @var Response\GetCaseResponse $response */
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...
174 7
        $response = $this->process($request);
175
176 7
        return $response->result->inspubWebserviceInsolvente->insolvente;
177
    }
178
179
    /**
180
     * Get a report for an insolvency.
181
     *
182
     * @param string $reportId the report identification number.
183
     *
184
     * @return Document
185
     * @throws Exception
186
     */
187 3
    public function getReport(string $reportId): Document
188
    {
189 3
        $request = new GetReport();
190 3
        $request->reportId = $reportId;
191
192
        /** @var GetReportResponse $response */
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...
193 3
        $response = $this->process($request);
194
195 3
        return $response->result->report;
196
197
    }
198
199
    /**
200
     * Get the date when the latest publication is added.
201
     *
202
     * @return LastUpdate
203
     * @throws Exception
204
     */
205 1
    public function getLastUpdate(): LastUpdate
206
    {
207
        /** @var Response\GetLastUpdateResponse $response */
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...
208 1
        $response = $this->process(new Request\GetLastUpdate());
209
210 1
        return $response->result->lastUpdate;
211
    }
212
213
    /**
214
     * Search for publications of a specific date.
215
     *
216
     * @param DateTime $date tne date of the publications to look up.
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
217
     * @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...
218
     * @param string|null $pubType the type of publication.
219
     *
220
     * @return PublicatieLijst
221
     * @throws Exception
222
     */
223 1
    public function searchByDate(DateTime $date, string $court, string $pubType = null): PublicatieLijst
224
    {
225 1
        $request = new Request\SearchByDate();
226 1
        $request->date = $date;
227 1
        $request->court = $court;
228 1
        $request->pubType = $pubType;
229
230
        /** @var Response\SearchByDateResponse $response */
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...
231 1
        $response = $this->process($request);
232
233 1
        return $response->result->publicatieLijst;
234
    }
235
236
    /**
237
     * Search for publications of the last mutated/added insolvencies for data replication.
238
     *
239
     * @param DateTime $modifyDate the modified date since.
240
     *
241
     * @return PublicatieLijst
242
     * @throws Exception
243
     */
244 1
    public function searchModifiedSince(DateTime $modifyDate): PublicatieLijst
245
    {
246 1
        $request = new Request\SearchModifiedSince();
247 1
        $request->modifyDate = $modifyDate;
248
249
        /** @var Response\SearchModifiedSinceResponse $response */
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...
250 1
        $response = $this->process($request);
251
252 1
        return $response->result->publicatieLijst;
253
    }
254
255
    /**
256
     * Search for removed publications for data replication.
257
     *
258
     * @param DateTime $modifyDate the modified date since.
259
     *
260
     * @return VerwijderdePublicatieLijst
261
     * @throws Exception
262
     */
263 1
    public function searchRemovedSince(DateTime $modifyDate): VerwijderdePublicatieLijst
264
    {
265 1
        $request = new Request\SearchRemovedSince();
266 1
        $request->modifyDate = $modifyDate;
267
268
        /** @var Response\SearchRemovedSinceResponse $response */
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...
269 1
        $response = $this->process($request);
270
271 1
        return $response->result->verwijderdePublicatieLijst;
272
    }
273
274
    /**
275
     * Search for added and modified reposts in a given time period.
276
     *
277
     * @param DateTime $datetimeFrom the start date.
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
278
     * @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...
279
     *
280
     * @return BeschikbareVerslagen
281
     * @throws Exception
282
     */
283 1
    public function searchReportsSince(DateTime $datetimeFrom, DateTime $datetimeTo = null): BeschikbareVerslagen
284
    {
285 1
        $request = new Request\SearchReportsSince();
286 1
        $request->datetimeFrom = $datetimeFrom;
287 1
        $request->datetimeTo = $datetimeTo ?? new DateTime();
288
289
        /** @var Response\SearchReportsSinceResponse $response */
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...
290 1
        $response = $this->process($request);
291
292 1
        return $response->result->beschikbareVerslagen;
293
    }
294
295
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
296
     * @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...
297
     * @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...
298
     * @internal
0 ignored issues
show
Coding Style introduced by
Tag @internal cannot be grouped with parameter tags in a doc comment
Loading history...
299
     */
300
    public function getHandler(string $request)
301
    {
302
        if (is_a($request, SoapRequest::class, true)) {
303
            return new SoapHandler($this->config, $this->getRequestHandler(), $this->requestFactory, $this->serializer);
304
        }
305
306
        return new GetReportHandler($this->config, $this->getRequestHandler(false), $this->requestFactory);
307
    }
308
309
    /**
310
     * Process a request.
311
     *
312
     * @param Request|GetReport $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
313
     *
314
     * @return Response|GetReportResponse
315
     * @throws Exception
316
     */
317 11
    protected function process($request)
318
    {
319 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...
320
            throw new \TypeError('Invalid request');
321
        }
322
323 11
        return $this->commandBus->handle($request);
324
    }
325
326
    /**
327
     * Get http client.
328
     *
329
     * @param bool $forSoap
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
330
     * @return HttpClient
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
331
     */
332
    protected function getRequestHandler(bool $forSoap = true): RequestHandler
333
    {
334
        $middleware = [
335
            new HttpExceptionMiddleware(),
336
        ];
337
338
        if ($forSoap) {
339
            $middleware[] = new SoapActionMiddleware();
340
        }
341
342
        return new RequestHandler($this->client, ...$middleware);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new DMT\Http\Clie...s->client, $middleware) returns the type DMT\Http\Client\RequestHandler which is incompatible with the documented return type GuzzleHttp\Client.
Loading history...
343
    }
344
}
345