Passed
Branch v4.x (42ae95)
by Dmitry
08:38
created

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gladyshev\Yandex\Direct;
6
7
/**
8
 * Yandex.Direct v5 API client implementation
9
 *
10
 * @property \Gladyshev\Yandex\Direct\Service\AdExtensions $adExtensions
11
 * @property \Gladyshev\Yandex\Direct\Service\AdGroups $adGroups
12
 * @property \Gladyshev\Yandex\Direct\Service\AdImages $adImages
13
 * @property \Gladyshev\Yandex\Direct\Service\Ads $ads
14
 * @property \Gladyshev\Yandex\Direct\Service\AgencyClients $agencyClients
15
 * @property \Gladyshev\Yandex\Direct\Service\AudienceTargets $audienceTargets
16
 * @property \Gladyshev\Yandex\Direct\Service\BidModifiers $bidModifiers
17
 * @property \Gladyshev\Yandex\Direct\Service\Bids $bids
18
 * @property \Gladyshev\Yandex\Direct\Service\Campaigns $campaigns
19
 * @property \Gladyshev\Yandex\Direct\Service\Changes $changes
20
 * @property \Gladyshev\Yandex\Direct\Service\Clients $clients
21
 * @property \Gladyshev\Yandex\Direct\Service\Dictionaries $dictionaries
22
 * @property \Gladyshev\Yandex\Direct\Service\DynamicTextAdTargets $dynamicTextAdTargets
23
 * @property \Gladyshev\Yandex\Direct\Service\KeywordBids $keywordBids
24
 * @property \Gladyshev\Yandex\Direct\Service\Keywords $keywords
25
 * @property \Gladyshev\Yandex\Direct\Service\KeywordsResearch $keywordsResearch
26
 * @property \Gladyshev\Yandex\Direct\Service\Reports $reports
27
 * @property \Gladyshev\Yandex\Direct\Service\RetargetingLists $retargetingLists
28
 * @property \Gladyshev\Yandex\Direct\Service\Sitelinks $sitelinks
29
 * @property \Gladyshev\Yandex\Direct\Service\TurboPages $turboPages
30
 * @property \Gladyshev\Yandex\Direct\Service\VCards $vCards
31
 */
32
class Client implements ServiceFactoryInterface
33
{
34
    private const SERVICE_NAMESPACE = __NAMESPACE__ . '\\Service\\';
35
36
    /**
37
     * @var \Gladyshev\Yandex\Direct\ServiceInterface[]
38
     */
39
    private $services = [];
40
41
    /**
42
     * @var \Gladyshev\Yandex\Direct\CredentialsInterface
43
     */
44
    private $credentials;
45
46
    /**
47
     * @var \Psr\Http\Client\ClientInterface
48
     */
49
    private $httpClient;
50
51 60
    public function __construct(
52
        CredentialsInterface $credentials,
53
        \Psr\Http\Client\ClientInterface $httpClient
54
    ) {
55 60
        $this->credentials = $credentials;
56 60
        $this->httpClient = $httpClient;
57 60
    }
58
59 60
    public function createService(string $serviceName): \Gladyshev\Yandex\Direct\ServiceInterface
60
    {
61 60
        if (!isset($this->services[$serviceName])) {
62 60
            $className = self::SERVICE_NAMESPACE . ucfirst($serviceName);
63
64 60
            if (!class_exists($className)) {
65 6
                throw new \Gladyshev\Yandex\Direct\Exception\ServiceNotFoundException(
66 6
                    $serviceName,
67 6
                    "Class '{$className}' is not found."
68
                );
69
            }
70
71 54
            $classInstance = new $className($serviceName, $this->credentials, $this->httpClient);
72
73 54
            if (!$classInstance instanceof \Gladyshev\Yandex\Direct\ServiceInterface) {
74
                throw new \Gladyshev\Yandex\Direct\Exception\ServiceNotFoundException(
75
                    $serviceName,
76
                    "Class '{$className}' must be an instance of '\Gladyshev\Yandex\Direct\ServiceInterface'."
77
                );
78
            }
79
80 54
            $this->services[$serviceName] = $classInstance;
81
        }
82
83 54
        return $this->services[$serviceName];
84
    }
85
86 30
    public function __get(string $serviceName): \Gladyshev\Yandex\Direct\ServiceInterface
87
    {
88 30
        return $this->createService($serviceName);
89
    }
90
}
91