Completed
Push — master ( 33546a...611b2c )
by Dmitry
35:36 queued 32:56
created

Client   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 69.39%

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 3
dl 0
loc 141
ccs 34
cts 49
cp 0.6939
rs 10
c 0
b 0
f 0

8 Methods

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