GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#71)
by Yong
06:21
created

ClientTrait::getDefaultClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AlibabaCloud\Client\Traits;
4
5
use AlibabaCloud\Client\AlibabaCloud;
6
use AlibabaCloud\Client\Clients\AccessKeyClient;
7
use AlibabaCloud\Client\Clients\BearerTokenClient;
8
use AlibabaCloud\Client\Clients\Client;
9
use AlibabaCloud\Client\Clients\EcsRamRoleClient;
10
use AlibabaCloud\Client\Clients\RamRoleArnClient;
11
use AlibabaCloud\Client\Clients\RsaKeyPairClient;
12
use AlibabaCloud\Client\Clients\StsClient;
13
use AlibabaCloud\Client\Credentials\CredentialsInterface;
14
use AlibabaCloud\Client\Credentials\Ini\IniCredential;
15
use AlibabaCloud\Client\Credentials\Providers\CredentialsProvider;
16
use AlibabaCloud\Client\Exception\ClientException;
17
use AlibabaCloud\Client\Signature\SignatureInterface;
18
19
/**
20
 * Trait of the manage clients.
21
 *
22
 * @package   AlibabaCloud\Client\Traits
23
 *
24
 * @mixin     AlibabaCloud
25
 */
26
trait ClientTrait
27
{
28
    /**
29
     * @var array Containers of Clients
30
     */
31
    protected static $clients = [];
32
33
    /**
34
     * Get the Client instance by name.
35
     *
36
     * @param string $clientName
37
     *
38
     * @return Client
39
     * @throws ClientException
40 113
     */
41
    public static function get($clientName)
42 113
    {
43 112
        if (!$clientName) {
44
            throw new ClientException(
45 2
                'The argument $clientName cannot be empty',
46 2
                \ALIBABA_CLOUD_INVALID_ARGUMENT
47
            );
48 2
        }
49
50
        if (self::has($clientName)) {
51
            return self::$clients[\strtolower($clientName)];
52
        }
53
54
        throw new ClientException(
55
            "Client '$clientName' not found",
56
            \ALIBABA_CLOUD_CLIENT_NOT_FOUND
57 117
        );
58
    }
59 117
60
    /**
61
     * @param string $clientName
62
     * @param Client $client
63
     *
64
     * @return Client
65
     * @throws ClientException
66
     */
67 3
    public static function set($clientName, Client $client)
68
    {
69 3
        if (!$clientName) {
70
            throw new ClientException(
71
                'The argument $clientName cannot be empty',
72
                \ALIBABA_CLOUD_INVALID_ARGUMENT
73
            );
74
        }
75
76
        return self::$clients[\strtolower($clientName)] = $client;
77 57
    }
78
79 57
    /**
80 57
     * Get all clients.
81
     *
82
     * @return array
83
     */
84
    public static function all()
85
    {
86
        return self::$clients;
87 4
    }
88
89 4
    /**
90 4
     * Delete the client by specifying name.
91 4
     *
92
     * @param string $clientName
93
     *
94
     * @throws ClientException
95
     */
96
    public static function del($clientName)
97
    {
98
        if (!$clientName) {
99 11
            throw new ClientException(
100
                'The argument $clientName cannot be empty',
101 11
                \ALIBABA_CLOUD_INVALID_ARGUMENT
102
            );
103
        }
104
105
        unset(self::$clients[\strtolower($clientName)]);
106
    }
107
108
    /**
109
     * Delete all clients.
110
     *
111 120
     * @return void
112
     */
113 120
    public static function flush()
114
    {
115
        self::$clients         = [];
116
        self::$defaultRegionId = null;
0 ignored issues
show
Bug Best Practice introduced by
The property defaultRegionId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
117
    }
118
119
    /**
120
     * @codeCoverageIgnore
121
     * @deprecated
122 26
     * Get the global client.
123
     *
124 26
     * @return Client
125 1
     * @throws ClientException
126
     */
127 25
    public static function getGlobalClient()
128 25
    {
129 25
        return self::getDefaultClient();
130 9
    }
131 9
132
    /**
133
     * Get the default client.
134
     *
135
     * @return Client
136
     * @throws ClientException
137
     */
138
    public static function getDefaultClient()
139
    {
140
        return self::get(CredentialsProvider::getDefaultName());
141
    }
142 10
143
    /**
144 10
     * Determine whether there is a client.
145
     *
146
     * @param string $clientName
147
     *
148
     * @return bool
149
     * @throws ClientException
150
     */
151
    public static function has($clientName)
152
    {
153
        if (!$clientName) {
154
            throw new ClientException(
155 70
                'The argument $clientName cannot be empty',
156
                \ALIBABA_CLOUD_INVALID_ARGUMENT
157 70
            );
158
        }
159
160
        return isset(self::$clients[\strtolower($clientName)]);
161
    }
162
163
    /**
164
     * A list of additional files to load.
165
     *
166
     * @return array
167
     * @throws ClientException when a file has a syntax error or does not exist or is not readable
168
     */
169
    public static function load()
170 7
    {
171
        if (\func_get_args() === []) {
172 7
            return (new IniCredential())->load();
173
        }
174
        $list = [];
175
        foreach (\func_get_args() as $filename) {
176
            $list[$filename] = (new IniCredential($filename))->load();
177
        }
178
179
        return $list;
180
    }
181
182 8
    /**
183
     * Custom Client.
184 8
     *
185
     * @param CredentialsInterface $credentials
186
     * @param SignatureInterface   $signature
187
     *
188
     * @return Client
189
     */
190
    public static function client(CredentialsInterface $credentials, SignatureInterface $signature)
191
    {
192
        return new Client($credentials, $signature);
193
    }
194 15
195
    /**
196 15
     * Use the AccessKey to complete the authentication.
197
     *
198
     * @param string $accessKeyId
199
     * @param string $accessKeySecret
200
     *
201
     * @return AccessKeyClient
202
     * @throws ClientException
203
     */
204
    public static function accessKeyClient($accessKeyId, $accessKeySecret)
205
    {
206
        if (!$accessKeyId) {
207
            throw new ClientException(
208 1
                'The argument $accessKeyId cannot be empty',
209
                \ALIBABA_CLOUD_INVALID_ARGUMENT
210 1
            );
211
        }
212
213
        if (!$accessKeySecret) {
214
            throw new ClientException(
215
                'The argument $accessKeySecret cannot be empty',
216
                \ALIBABA_CLOUD_INVALID_ARGUMENT
217
            );
218
        }
219
220
        return new AccessKeyClient($accessKeyId, $accessKeySecret);
221
    }
222 8
223
    /**
224 8
     * Use the AssumeRole of the RAM account to complete  the authentication.
225
     *
226
     * @param string $accessKeyId
227
     * @param string $accessKeySecret
228
     * @param string $roleArn
229
     * @param string $roleSessionName
230
     *
231
     * @return RamRoleArnClient
232
     * @throws ClientException
233
     */
234
    public static function ramRoleArnClient($accessKeyId, $accessKeySecret, $roleArn, $roleSessionName)
235
    {
236
        if (!$accessKeyId) {
237
            throw new ClientException(
238
                'The argument $accessKeyId cannot be empty',
239
                \ALIBABA_CLOUD_INVALID_ARGUMENT
240
            );
241
        }
242
243
        if (!$accessKeySecret) {
244
            throw new ClientException(
245
                'The argument $accessKeySecret cannot be empty',
246
                \ALIBABA_CLOUD_INVALID_ARGUMENT
247
            );
248
        }
249
250
        return new RamRoleArnClient($accessKeyId, $accessKeySecret, $roleArn, $roleSessionName);
251
    }
252
253
    /**
254
     * Use the RAM role of an ECS instance to complete the authentication.
255
     *
256
     * @param string $roleName
257
     *
258
     * @return EcsRamRoleClient
259
     * @throws ClientException
260
     */
261
    public static function ecsRamRoleClient($roleName)
262
    {
263
        if (!$roleName) {
264
            throw new ClientException(
265
                'The argument $roleName cannot be empty',
266
                \ALIBABA_CLOUD_INVALID_ARGUMENT
267
            );
268
        }
269
270
        return new EcsRamRoleClient($roleName);
271
    }
272
273
    /**
274
     * Use the Bearer Token to complete the authentication.
275
     *
276
     * @param string $bearerToken
277
     *
278
     * @return BearerTokenClient
279
     * @throws ClientException
280
     */
281
    public static function bearerTokenClient($bearerToken)
282
    {
283
        if (!$bearerToken) {
284
            throw new ClientException(
285
                'The argument $bearerToken cannot be empty',
286
                \ALIBABA_CLOUD_INVALID_ARGUMENT
287
            );
288
        }
289
290
        return new BearerTokenClient($bearerToken);
291
    }
292
293
    /**
294
     * Use the STS Token to complete the authentication.
295
     *
296
     * @param string $accessKeyId     Access key ID
297
     * @param string $accessKeySecret Access Key Secret
298
     * @param string $securityToken   Security Token
299
     *
300
     * @return StsClient
301
     * @throws ClientException
302
     */
303
    public static function stsClient($accessKeyId, $accessKeySecret, $securityToken = '')
304
    {
305
        if (!$accessKeyId) {
306
            throw new ClientException(
307
                'The argument $accessKeyId cannot be empty',
308
                \ALIBABA_CLOUD_INVALID_ARGUMENT
309
            );
310
        }
311
312
        if (!$accessKeySecret) {
313
            throw new ClientException(
314
                'The argument $accessKeySecret cannot be empty',
315
                \ALIBABA_CLOUD_INVALID_ARGUMENT
316
            );
317
        }
318
319
        return new StsClient($accessKeyId, $accessKeySecret, $securityToken);
320
    }
321
322
    /**
323
     * Use the RSA key pair to complete the authentication (supported only on Japanese site)
324
     *
325
     * @param string $publicKeyId
326
     * @param string $privateKeyFile
327
     *
328
     * @return RsaKeyPairClient
329
     * @throws ClientException
330
     */
331
    public static function rsaKeyPairClient($publicKeyId, $privateKeyFile)
332
    {
333
        if (!$publicKeyId) {
334
            throw new ClientException(
335
                'The argument $publicKeyId cannot be empty',
336
                \ALIBABA_CLOUD_INVALID_ARGUMENT
337
            );
338
        }
339
340
        if (!$privateKeyFile) {
341
            throw new ClientException(
342
                'The argument $privateKeyFile cannot be empty',
343
                \ALIBABA_CLOUD_INVALID_ARGUMENT
344
            );
345
        }
346
347
        return new RsaKeyPairClient($publicKeyId, $privateKeyFile);
348
    }
349
}
350