MonobankClient   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 103
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getPersonalStatement() 0 10 2
A __construct() 0 5 1
A decode() 0 9 2
A getClientInfo() 0 3 1
A getExchangeRates() 0 3 1
A request() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Monobank;
6
7
use GuzzleHttp\RequestOptions;
8
use Monobank\ValueObject\Token;
9
use GuzzleHttp\ClientInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use GuzzleHttp\Exception\GuzzleException;
12
use Monobank\Request\PersonalStatementRequest;
13
use Monobank\Exception\InvalidResponseException;
14
15
/**
16
 * Monobank HTTP client.
17
 */
18
class MonobankClient implements MonobankPublicDataClientInterface, MonobankPrivateDataClientInterface
19
{
20
    private const HOST = 'https://api.monobank.ua';
21
22
    /**
23
     * @var ClientInterface
24
     */
25
    private $client;
26
27
    /**
28
     * @var Token
29
     */
30
    private $token;
31
32
    /**
33
     * @var string
34
     */
35
    private $host;
36
37
    /**
38
     * @param ClientInterface $client
39
     * @param Token           $token
40
     * @param string          $host
41
     */
42
    public function __construct(ClientInterface $client, Token $token, string $host = self::HOST)
43
    {
44
        $this->client = $client;
45
        $this->token = $token;
46
        $this->host = rtrim($host, '/');
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getExchangeRates(): array
53
    {
54
        return $this->decode($this->request('GET', '/bank/currency'));
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getClientInfo(): array
61
    {
62
        return $this->decode($this->request('GET', '/personal/client-info', $this->token));
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getPersonalStatement(PersonalStatementRequest $request): array
69
    {
70
        $endpoint = sprintf(
71
            '/personal/statement/%s/%d/%d',
72
            null === $request->getAccountID() ? '0' : strval($request->getAccountID()), // 0 - default account
73
            $request->getFrom()->getTimestamp(),
74
            $request->getTo()->getTimestamp()
75
        );
76
77
        return $this->decode($this->request('GET', $endpoint, $this->token));
78
    }
79
80
    /**
81
     * @param string     $method
82
     * @param string     $endpoint
83
     * @param Token|null $token
84
     *
85
     * @return ResponseInterface
86
     *
87
     * @throws GuzzleException
88
     */
89
    private function request(string $method, string $endpoint, Token $token = null): ResponseInterface
90
    {
91
        $headers = [
92
            'Accept' => 'application/json',
93
            'User-Agent' => sprintf('%s MonobankPHPClient/%s', $this->client->getConfig('headers')['User-Agent'] ?? '', MONOBANK_CLIENT_VERSION),
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\ClientInterface::getConfig() has been deprecated: ClientInterface::getConfig will be removed in guzzlehttp/guzzle:8.0. ( Ignorable by Annotation )

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

93
            'User-Agent' => sprintf('%s MonobankPHPClient/%s', /** @scrutinizer ignore-deprecated */ $this->client->getConfig('headers')['User-Agent'] ?? '', MONOBANK_CLIENT_VERSION),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
94
        ];
95
96
        if (null !== $token) {
97
            $headers['X-Token'] = strval($token);
98
        }
99
100
        return $this->client->request($method, $this->host.$endpoint, [
101
            RequestOptions::HEADERS => $headers,
102
        ]);
103
    }
104
105
    /**
106
     * @param ResponseInterface $response
107
     *
108
     * @return array
109
     *
110
     * @throws InvalidResponseException
111
     */
112
    private function decode(ResponseInterface $response): array
113
    {
114
        $decodedContents = json_decode($response->getBody()->getContents(), true);
115
116
        if (!is_array($decodedContents)) {
117
            throw InvalidResponseException::wrongFormat();
118
        }
119
120
        return $decodedContents;
121
    }
122
}
123