Passed
Push — master ( a8d88a...70ed4e )
by Sergey
01:25 queued 13s
created

Connection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 28
c 2
b 0
f 0
dl 0
loc 63
ccs 26
cts 26
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A post() 0 23 5
A getResponseBody() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SergeyNezbritskiy\NovaPoshta;
6
7
use GuzzleHttp\Client as HttpClient;
8
use GuzzleHttp\Exception\GuzzleException;
9
use GuzzleHttp\RequestOptions;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * Class Connection
14
 */
15
class Connection
16
{
17
    private const API_URI = 'https://api.novaposhta.ua/v2.0/json/';
18
    private const ERROR_MSG_TEMPLATE = 'Connection to Nova Poshta API failed: %s';
19
    private string $apiKey;
20
    private HttpClient $client;
21
22
    /**
23
     * @param string $apiKey
24
     * @param HttpClient $client
25
     */
26 7
    public function __construct(string $apiKey, HttpClient $client)
27
    {
28 7
        $this->apiKey = $apiKey;
29 7
        $this->client = $client;
30
    }
31
32
    /**
33
     * @param string $model
34
     * @param string $method
35
     * @param array $params
36
     * @return array
37
     * @throws NovaPoshtaApiException
38
     */
39 6
    public function post(string $model, string $method, array $params = []): array
40
    {
41
        try {
42 6
            $request = [
43 6
                'apiKey' => $this->apiKey,
44 6
                'modelName' => $model,
45 6
                'calledMethod' => $method,
46 6
                'methodProperties' => $params
47 6
            ];
48 6
            $response = $this->client->request('POST', self::API_URI, [RequestOptions::JSON => $request]);
49 5
            if ($response->getStatusCode() !== 200) {
50 1
                throw new NovaPoshtaApiException(sprintf(self::ERROR_MSG_TEMPLATE, $response->getReasonPhrase()));
51
            }
52
53 4
            $body = $this->getResponseBody($response);
54
55 3
            if ($body['success'] === false) {
56 1
                $error = array_shift($body['errors']) ?: array_shift($body['warnings']);
57 1
                throw new NovaPoshtaApiException(sprintf(self::ERROR_MSG_TEMPLATE, $error));
58
            }
59 2
            return $body['data'];
60 4
        } catch (GuzzleException $e) {
61 1
            throw new NovaPoshtaApiException(sprintf(self::ERROR_MSG_TEMPLATE, $e->getMessage()), $e->getCode(), $e);
62
        }
63
    }
64
65
    /**
66
     * @param ResponseInterface $response
67
     * @return array
68
     * @throws NovaPoshtaApiException
69
     */
70 4
    private function getResponseBody(ResponseInterface $response): array
71
    {
72 4
        $content = $response->getBody()->getContents();
73 4
        $result = json_decode($content, true);
74 4
        if (empty($result)) {
75 1
            throw new NovaPoshtaApiException('Invalid response from Nova Poshta API');
76
        }
77 3
        return $result;
78
    }
79
}
80