Passed
Push — master ( 4aa2ac...f6bdf0 )
by Sergey
02:57
created

Client::getConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SergeyNezbritskiy\NovaPoshta;
6
7
use GuzzleHttp\Client as HttpClient;
8
use PHPUnit\Logging\Exception;
9
use SergeyNezbritskiy\NovaPoshta\Models\Address;
10
11
/**
12
 * Class Client
13
 *
14
 * Class-connector with NovaPoshta API
15
 *
16
 * @property Address $address
17
 * @see      https://developers.novaposhta.ua/documentation
18
 */
19
class Client
20
{
21
    private const MODELS_MAP = [
22
        'address' => Address::class
23
    ];
24
25
    private string $apiKey;
26
    private Connection $connection;
27
28
    /**
29
     * @param string $apiKey
30
     */
31 2
    public function __construct(string $apiKey)
32
    {
33 2
        $this->apiKey = $apiKey;
34
    }
35
36 2
    public function __get(string $property): ModelInterface
37
    {
38 2
        if (!array_key_exists($property, self::MODELS_MAP)) {
39 1
            throw new Exception(sprintf('Model `%s` not supported by Nova Poshta API Client', $property));
40
        }
41 1
        $class = self::MODELS_MAP[$property];
42 1
        $this->$property = new $class($this->getConnection());
43 1
        return $this->$property;
44
    }
45
46 1
    private function getConnection(): Connection
47
    {
48 1
        if (empty($this->connection)) {
49 1
            $this->connection = new Connection($this->apiKey, new HttpClient());
50
        }
51 1
        return $this->connection;
52
    }
53
}
54