Client::get()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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