Completed
Branch v4.x (712f3d)
by Dmitry
04:56
created

Client::__construct()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9.648

Importance

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