Completed
Push — master ( f9afbd...858090 )
by Dmitry
02:10
created

Client::getServiceFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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