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
|
|
|
|