Passed
Pull Request — master (#17)
by Sergey
03:01
created

Client   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 18
c 1
b 0
f 0
dl 0
loc 40
rs 10
ccs 12
cts 12
cp 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SergeyNezbritskiy\NovaPoshta;
6
7
use Exception;
8
use GuzzleHttp\Client as HttpClient;
9
use SergeyNezbritskiy\NovaPoshta\Models\Address;
10
use SergeyNezbritskiy\NovaPoshta\Models\Common;
11
use SergeyNezbritskiy\NovaPoshta\Models\ContactPerson;
12
use SergeyNezbritskiy\NovaPoshta\Models\Counterparty;
13
use SergeyNezbritskiy\NovaPoshta\Models\InternetDocument;
14
15
/**
16
 * Class Client
17
 *
18
 * Class-connector with NovaPoshta API
19
 *
20
 * @property Address $address
21
 * @property Counterparty $counterparty
22
 * @property ContactPerson $contactPerson
23
 * @property ContactPerson $common
24
 * @property ContactPerson $internetDocument
25
 * @see      https://developers.novaposhta.ua/documentation
26
 */
27
class Client
28
{
29
    private const array MODELS_MAP = [
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 29 at column 24
Loading history...
30
        'address' => Address::class,
31 2
        'counterparty' => Counterparty::class,
32
        'contactPerson' => ContactPerson::class,
33 2
        'common' => Common::class,
34
        'internetDocument' => InternetDocument::class,
35
    ];
36 2
37
    private string $apiKey;
38 2
    private Connection $connection;
39 1
40
    /**
41 1
     * @param string $apiKey
42 1
     */
43 1
    public function __construct(string $apiKey)
44
    {
45
        $this->apiKey = $apiKey;
46 1
    }
47
48 1
    /**
49 1
     * @throws Exception
50
     */
51 1
    public function __get(string $property): ModelInterface
52
    {
53
        if (!array_key_exists($property, self::MODELS_MAP)) {
54
            throw new Exception(sprintf('Model `%s` not supported by Nova Poshta API Client', $property));
55
        }
56
        $class = self::MODELS_MAP[$property];
57
        $this->$property = new $class($this->getConnection());
58
        return $this->$property;
59
    }
60
61
    private function getConnection(): Connection
62
    {
63
        if (empty($this->connection)) {
64
            $this->connection = new Connection($this->apiKey, new HttpClient());
65
        }
66
        return $this->connection;
67
    }
68
}
69