Test Failed
Push — master ( b5a074...813da4 )
by Sergey
02:49
created

Connection::getResponseBody()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 1
crap 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 1
     * @param HttpClient $client
25
     */
26 1
    public function __construct(string $apiKey, HttpClient $client)
27
    {
28
        $this->apiKey = $apiKey;
29
        $this->client = $client;
30
    }
31
32
    /**
33
     * @param string $model
34
     * @param string $method
35
     * @param array $params
36 1
     * @return array
37
     * @throws NovaPoshtaApiException
38
     */
39 1
    public function post(string $model, string $method, array $params = []): array
40 1
    {
41 1
        try {
42 1
            $request = [
43 1
                'apiKey' => $this->apiKey,
44 1
                'modelName' => $model,
45 1
                'calledMethod' => $method,
46 1
                'methodProperties' => $params
47
            ];
48
            $response = $this->client->request('POST', self::API_URI, [RequestOptions::JSON => $request]);
49
            if ($response->getStatusCode() !== 200) {
50 1
                throw new NovaPoshtaApiException(sprintf(self::ERROR_MSG_TEMPLATE, $response->getReasonPhrase()));
51
            }
52 1
53
            $body = $this->getResponseBody($response);
54
55 1
            if ($body['success'] === false) {
56
                $error = array_shift($body['errors']) ?: array_shift($body['warnings']);
57
                throw new NovaPoshtaApiException(sprintf(self::ERROR_MSG_TEMPLATE, $error));
58
            }
59
            return $body['data'];
60
        } catch (GuzzleException $e) {
61
            throw new NovaPoshtaApiException(sprintf(self::ERROR_MSG_TEMPLATE, $e->getMessage()), $e->getCode(), $e);
62
        }
63
    }
64 1
65
    /**
66 1
     * @param ResponseInterface $response
67
     * @return array
68
     * @throws NovaPoshtaApiException
69
     */
70
    private function getResponseBody(ResponseInterface $response): array
71
    {
72
        $content = $response->getBody()->getContents();
73
        $result = json_decode($content, true);
74 1
        if (empty($result)) {
75
            throw new NovaPoshtaApiException('Invalid response from Nova Poshta API');
76 1
        }
77 1
        return $result;
78 1
    }
79
}
80