Client::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
ccs 1
cts 1
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SergeyNezbritskiy\NovaPoshta;
6
7
use AllowDynamicProperties;
0 ignored issues
show
Bug introduced by
The type AllowDynamicProperties was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Exception;
9
use GuzzleHttp\Client as HttpClient;
10
use SergeyNezbritskiy\NovaPoshta\Models\Address;
11
use SergeyNezbritskiy\NovaPoshta\Models\Common;
12
use SergeyNezbritskiy\NovaPoshta\Models\ContactPerson;
13
use SergeyNezbritskiy\NovaPoshta\Models\Counterparty;
14
use SergeyNezbritskiy\NovaPoshta\Models\InternetDocument;
15
16
/**
17
 * Class Client
18
 *
19
 * Class-connector with NovaPoshta API
20
 *
21
 * @property Address $address
22
 * @property Counterparty $counterparty
23
 * @property ContactPerson $contactPerson
24
 * @property ContactPerson $common
25
 * @property ContactPerson $internetDocument
26
 * @see      https://developers.novaposhta.ua/documentation
27
 */
28
#[AllowDynamicProperties] class Client
29
{
30
    private const MODELS_MAP = [
31 2
        'address' => Address::class,
32
        'counterparty' => Counterparty::class,
33 2
        'contactPerson' => ContactPerson::class,
34
        'common' => Common::class,
35
        'internetDocument' => InternetDocument::class,
36 2
    ];
37
38 2
    private string $apiKey;
39 1
    private Connection $connection;
40
41 1
    /**
42 1
     * @param string $apiKey
43 1
     */
44
    public function __construct(string $apiKey)
45
    {
46 1
        $this->apiKey = $apiKey;
47
    }
48 1
49 1
    /**
50
     * @throws Exception
51 1
     */
52
    public function __get(string $property): ModelInterface
53
    {
54
        if (!array_key_exists($property, self::MODELS_MAP)) {
55
            throw new Exception(sprintf('Model `%s` not supported by Nova Poshta API Client', $property));
56
        }
57
        $class = self::MODELS_MAP[$property];
58
        $this->$property = new $class($this->getConnection());
59
        return $this->$property;
60
    }
61
62
    private function getConnection(): Connection
63
    {
64
        if (empty($this->connection)) {
65
            $this->connection = new Connection($this->apiKey, new HttpClient());
66
        }
67
        return $this->connection;
68
    }
69
}
70