1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Damax\Services\Client; |
6
|
|
|
|
7
|
|
|
use Http\Client\Common\HttpMethodsClient; |
8
|
|
|
use Http\Client\HttpClient; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
class Client |
13
|
|
|
{ |
14
|
|
|
private $httpClient; |
15
|
|
|
|
16
|
|
|
public function __construct(HttpMethodsClient $httpClient) |
17
|
|
|
{ |
18
|
|
|
$this->httpClient = $httpClient; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function getHttpClient(): HttpClient |
22
|
|
|
{ |
23
|
|
|
return $this->httpClient; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @throws InvalidRequestException |
28
|
|
|
*/ |
29
|
|
|
public function checkPassport(string $input): PassportCheck |
30
|
|
|
{ |
31
|
|
|
$response = $this->httpClient->get('/mvd/passports?input=' . urlencode($input), ['accept' => 'application/json']); |
32
|
|
|
|
33
|
|
|
return PassportCheck::fromArray($this->parseResponse($response)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return PassportCheck[] |
38
|
|
|
* |
39
|
|
|
* @throws InvalidArgumentException |
40
|
|
|
* @throws InvalidRequestException |
41
|
|
|
*/ |
42
|
|
|
public function checkMultiplePassports(array $inputs): array |
43
|
|
|
{ |
44
|
|
|
if (!count($inputs)) { |
45
|
|
|
throw new InvalidArgumentException('At least one element required.'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$input = implode(',', array_map('urlencode', $inputs)); |
49
|
|
|
|
50
|
|
|
$response = $this->httpClient->get('/mvd/passports/multiple?input=' . $input, ['accept' => 'application/json']); |
51
|
|
|
|
52
|
|
|
return array_map([PassportCheck::class, 'fromArray'], $this->parseResponse($response)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @throws InvalidRequestException |
57
|
|
|
*/ |
58
|
|
|
public function downloadPassportCheck(string $input): ResponseInterface |
59
|
|
|
{ |
60
|
|
|
$response = $this->httpClient->get('/mvd/passports?input=' . urlencode($input), ['accept' => 'application/pdf']); |
61
|
|
|
|
62
|
|
|
if ($response->getStatusCode() >= 400) { |
63
|
|
|
throw new InvalidRequestException('Invalid request.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $response; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @throws InvalidRequestException |
71
|
|
|
*/ |
72
|
|
|
public function checkRosfin(string $fullName, string $birthDate = null): RosfinCheck |
73
|
|
|
{ |
74
|
|
|
$response = $this->httpClient->get('/rosfin/catalogue?' . http_build_query([ |
75
|
|
|
'fullName' => $fullName, |
76
|
|
|
'birthDate' => $birthDate, |
77
|
|
|
])); |
78
|
|
|
|
79
|
|
|
return RosfinCheck::fromArray($this->parseResponse($response)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @throws InvalidRequestException |
84
|
|
|
*/ |
85
|
|
|
private function parseResponse(ResponseInterface $response): array |
86
|
|
|
{ |
87
|
|
|
$data = json_decode((string) $response->getBody(), true); |
88
|
|
|
|
89
|
|
|
if ($response->getStatusCode() >= 400) { |
90
|
|
|
throw new InvalidRequestException($data['message'] ?? 'Invalid request.'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $data; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|