Client::__get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @author Dmitry Gladyshev <[email protected]>
4
 * @date 17/08/2016 10:37
5
 */
6
7
namespace Yandex\Direct;
8
9
use Yandex\Direct\Service\AdExtensions;
10
use Yandex\Direct\Service\AdGroups;
11
use Yandex\Direct\Service\AdImages;
12
use Yandex\Direct\Service\Ads;
13
use Yandex\Direct\Service\AgencyClients;
14
use Yandex\Direct\Service\AudienceTargets;
15
use Yandex\Direct\Service\BidModifiers;
16
use Yandex\Direct\Service\Bids;
17
use Yandex\Direct\Service\Campaigns;
18
use Yandex\Direct\Service\Changes;
19
use Yandex\Direct\Service\Clients;
20
use Yandex\Direct\Service\Dictionaries;
21
use Yandex\Direct\Service\DynamicTextAdTargets;
22
use Yandex\Direct\Service\KeywordBids;
23
use Yandex\Direct\Service\Keywords;
24
use Yandex\Direct\Service\KeywordsResearch;
25
use Yandex\Direct\Service\Reports;
26
use Yandex\Direct\Service\RetargetingLists;
27
use Yandex\Direct\Service\Sitelinks;
28
use Yandex\Direct\Service\TurboPages;
29
use Yandex\Direct\Service\VCards;
30
use Yandex\Direct\Transport\TransportInterface;
31
32
/**
33
 * Yandex.Direct v5 API client implementation
34
 *
35
 * @package Yandex\Direct
36
 *
37
 * Yandex Services
38
 * @method AdExtensions adExtensions(array $options = [])
39
 * @method AdGroups adGroups(array $options = [])
40
 * @method AdImages adImages(array $options = [])
41
 * @method Ads ads(array $options = [])
42
 * @method AgencyClients agencyClients(array $options)
43
 * @method AudienceTargets audienceTargets(array $options = [])
44
 * @method BidModifiers bidModifiers(array $options = [])
45
 * @method Bids bids(array $options = [])
46
 * @method Campaigns campaigns(array $options = [])
47
 * @method Clients clients(array $options = [])
48
 * @method Changes changes(array $options = [])
49
 * @method Dictionaries dictionaries(array $options = [])
50
 * @method DynamicTextAdTargets dynamicTextAdsTargets(array $options = [])
51
 * @method KeywordBids keywordBids(array $options = [])
52
 * @method Keywords keywords(array $options = [])
53
 * @method KeywordsResearch keywordsResearch(array $options = [])
54
 * @method Reports reports(array $options = [])
55
 * @method RetargetingLists retargetingLists(array $options = [])
56
 * @method Sitelinks sitelinks(array $options = [])
57
 * @method TurboPages turboPages(array $options = [])
58
 * @method VCards vCards(array $options = [])
59
 *
60
 * Aliases (sugar)
61
 * @property AdExtensions $adExtensions
62
 * @property AdGroups $adGroups
63
 * @property AdImages $adImages
64
 * @property Ads $ads
65
 * @property AgencyClients $agencyClients
66
 * @property AudienceTargets $audienceTargets
67
 * @property BidModifiers $bidModifiers
68
 * @property Bids $bids
69
 * @property Campaigns $campaigns
70
 * @property Clients $clients
71
 * @property Changes $changes
72
 * @property Dictionaries $dictionaries
73
 * @property DynamicTextAdTargets $dynamicTextAdsTargets
74
 * @property KeywordBids $keywordBids
75
 * @property Keywords $keywords
76
 * @property KeywordsResearch $keywordsResearch
77
 * @property Reports $reports
78
 * @property RetargetingLists $retargetingLists
79
 * @property Sitelinks $sitelinks
80
 * @property TurboPages $turboPages
81
 * @property VCards $vCards
82
 */
83
class Client
84
{
85
    /**
86
     * @var ServiceFactoryInterface
87
     */
88
    protected $serviceFactory;
89
90
    /**
91
     * Service options.
92
     * @var array
93
     */
94
    protected $options = [];
95
96
    /**
97
     * Client constructor with overloading.
98
     *
99
     * @param mixed[] ...$args    The order of the arguments doesn't matter.
100
     *                            Credentials is required, it can be CredentialsInterface instance or
101
     *                            login and token strings in order.
102
     *      Example:
103
     *      $client = new Client('login', 'token');
104
     *      $client = new Client(new Credentials('login', 'token'));
105
     *      $client = new Client(new Credentials('login', 'token'), ['useOperatorUnits' => true]);
106
     *      $client = new Client('login', 'token', ['useOperatorUnits' => true]);
107
     *      $client = new Client('login', 'token', new Transport(['logger' => new Log]), ['useOperatorUnits' => true]);
108
     *      // etc
109
     */
110 52
    public function __construct(...$args)
111
    {
112 52
        if (empty($args)) {
113
            return;
114
        }
115
116 52
        $strArgs = [];
117
118 52
        foreach ($args as $key => $val) {
119 52
            if ($val instanceof CredentialsInterface) {
120
                $this->setCredentials($val);
121 52
            } elseif ($val instanceof TransportInterface) {
122
                $this->setTransport($val);
123 52
            } elseif (is_array($val)) {
124
                $this->setOptions($val);
125 52
            } elseif (is_string($val)) {
126 52
                $strArgs[] = $val;
127 52
            }
128 52
        }
129
130 52
        list($login, $token, $masterToken) = array_pad($strArgs, 3, '');
131
132 52
        if ($login && $token) {
133 52
            $this->setCredentials(new Credentials($login, $token, $masterToken));
134 52
        }
135 52
    }
136
137
    /**
138
     * Returns specific Service instance.
139
     *
140
     * @param string $serviceName # The Name of Yandex service
141
     * @param array $args # The First argument is the service options override.
142
     * @return Service
143
     */
144 52
    public function __call($serviceName, array $args = [])
145
    {
146 52
        $userOptions = isset($args[0]) && is_array($args[0]) ? $args[0] : [];
147 52
        $options = array_merge($this->options, $userOptions);
148
149 52
        return $this
150 52
            ->getServiceFactory()
151 52
            ->createService($serviceName, $options);
152
    }
153
154
    /**
155
     * Alias of __call()
156
     *
157
     * @param string $name
158
     * @return Service
159
     * @see Client::__call()
160
     */
161 26
    public function __get($name)
162
    {
163 26
        return $this->__call($name);
164
    }
165
166
    /* Setters & Getters */
167
168
    /**
169
     * @param mixed[] $credentials
170
     * @return $this
171
     */
172 52
    public function setCredentials(...$credentials)
173
    {
174 52
        if ($credentials[0] instanceof CredentialsInterface) {
175 52
            $this->options[ServiceFactoryInterface::OPTION_CREDENTIALS] = $credentials[0];
176 52
        } else {
177
            list($login, $token, $masterToken) = array_pad($credentials, 3, '');
178
            $this->options[ServiceFactoryInterface::OPTION_CREDENTIALS] = new Credentials($login, $token, $masterToken);
179
        }
180 52
        return $this;
181
    }
182
183
    /**
184
     * @param TransportInterface $transport
185
     * @return $this
186
     */
187
    public function setTransport(TransportInterface $transport)
188
    {
189
        $this->options[ServiceFactoryInterface::OPTION_TRANSPORT] = $transport;
190
        return $this;
191
    }
192
193
    /**
194
     * @param array $options
195
     * @return $this
196
     */
197
    public function setOptions(array $options)
198
    {
199
        $this->options = $options;
200
        return $this;
201
    }
202
203
    /**
204
     * @param ServiceFactoryInterface $serviceFactory
205
     * @return $this
206
     */
207
    public function setServiceFactory(ServiceFactoryInterface $serviceFactory)
208
    {
209
        $this->serviceFactory = $serviceFactory;
210
        return $this;
211
    }
212
213
    /**
214
     * @return ServiceFactoryInterface
215
     */
216 52
    protected function getServiceFactory()
217
    {
218 52
        if ($this->serviceFactory === null) {
219 52
            $this->serviceFactory = new ServiceFactory;
220 52
        }
221 52
        return $this->serviceFactory;
222
    }
223
}
224