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

Connection::post()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 23
ccs 17
cts 17
cp 1
rs 9.4222
cc 5
nc 9
nop 3
crap 5
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