1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Setono\DAO\Client; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
9
|
|
|
use Psr\Http\Client\ClientInterface as HttpClientInterface; |
10
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
11
|
|
|
use Safe\Exceptions\JsonException; |
12
|
|
|
use Safe\Exceptions\StringsException; |
13
|
|
|
use function Safe\json_decode; |
14
|
|
|
use Setono\DAO\Exception\ApiException; |
15
|
|
|
use Setono\DAO\Exception\NotOkStatusCodeException; |
16
|
|
|
use Webmozart\Assert\Assert; |
17
|
|
|
|
18
|
|
|
final class Client implements ClientInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var HttpClientInterface |
22
|
|
|
*/ |
23
|
|
|
private $httpClient; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var RequestFactoryInterface |
27
|
|
|
*/ |
28
|
|
|
private $requestFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $customerId; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $password; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $baseUrl; |
44
|
|
|
|
45
|
|
|
public function __construct( |
46
|
|
|
HttpClientInterface $httpClient, |
47
|
|
|
RequestFactoryInterface $requestFactory, |
48
|
|
|
string $customerId, |
49
|
|
|
string $password, |
50
|
|
|
string $baseUrl = 'https://api.dao.as' |
51
|
|
|
) { |
52
|
|
|
$this->httpClient = $httpClient; |
53
|
|
|
$this->requestFactory = $requestFactory; |
54
|
|
|
$this->customerId = $customerId; |
55
|
|
|
$this->password = $password; |
56
|
|
|
$this->baseUrl = $baseUrl; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
* |
62
|
|
|
* @throws ClientExceptionInterface |
63
|
|
|
* @throws JsonException |
64
|
|
|
* @throws StringsException |
65
|
|
|
* @throws InvalidArgumentException |
66
|
|
|
*/ |
67
|
|
|
public function get(string $endpoint, array $params = []): array |
68
|
|
|
{ |
69
|
|
|
Assert::notContains($endpoint, '?', 'Do not add query parameters to the endpoint. Instead use the third argument, $params'); |
70
|
|
|
|
71
|
|
|
$params = array_merge([ |
72
|
|
|
'kundeid' => $this->customerId, |
73
|
|
|
'kode' => $this->password, |
74
|
|
|
], $params); |
75
|
|
|
|
76
|
|
|
// overrides the format because this implementation only supports JSON |
77
|
|
|
$params['format'] = 'json'; |
78
|
|
|
|
79
|
|
|
$url = $this->baseUrl.'/'.$endpoint.'?'.http_build_query($params, '', '&', PHP_QUERY_RFC3986); |
80
|
|
|
|
81
|
|
|
$request = $this->requestFactory->createRequest('GET', $url); |
82
|
|
|
|
83
|
|
|
$response = $this->httpClient->sendRequest($request); |
84
|
|
|
|
85
|
|
|
if (200 !== $response->getStatusCode()) { |
86
|
|
|
throw new NotOkStatusCodeException($request, $response, $response->getStatusCode()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$data = (array) json_decode((string) $response->getBody(), true); |
90
|
|
|
|
91
|
|
|
if (isset($data['status']) && 'FEJL' === $data['status']) { |
92
|
|
|
throw new ApiException($request, $response, $response->getStatusCode(), $data['fejlkode'], $data['fejltekst']); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $data; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|