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.
Test Failed
Pull Request — master (#73)
by Yong
08:31 queued 12s
created

ClientTrait::getDefaultClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\Filter;
18
use AlibabaCloud\Client\Signature\SignatureInterface;
19
20
/**
21
 * Trait of the manage clients.
22
 *
23
 * @package   AlibabaCloud\Client\Traits
24
 *
25
 * @mixin     AlibabaCloud
26
 */
27
trait ClientTrait
28
{
29
    /**
30
     * @var array Containers of Clients
31
     */
32
    protected static $clients = [];
33
34
    /**
35
     * Get the Client instance by name.
36
     *
37
     * @param string $clientName
38
     *
39
     * @return Client
40 113
     * @throws ClientException
41
     */
42 113
    public static function get($clientName)
43 112
    {
44
        Filter::clientName($clientName);
45 2
46 2
        if (self::has($clientName)) {
47
            return self::$clients[\strtolower($clientName)];
48 2
        }
49
50
        throw new ClientException(
51
            "Client '$clientName' not found",
52
            \ALIBABA_CLOUD_CLIENT_NOT_FOUND
53
        );
54
    }
55
56
    /**
57 117
     * @param string $clientName
58
     * @param Client $client
59 117
     *
60
     * @return Client
61
     * @throws ClientException
62
     */
63
    public static function set($clientName, Client $client)
64
    {
65
        Filter::clientName($clientName);
66
67 3
        return self::$clients[\strtolower($clientName)] = $client;
68
    }
69 3
70
    /**
71
     * Get all clients.
72
     *
73
     * @return array
74
     */
75
    public static function all()
76
    {
77 57
        return self::$clients;
78
    }
79 57
80 57
    /**
81
     * Delete the client by specifying name.
82
     *
83
     * @param string $clientName
84
     *
85
     * @throws ClientException
86
     */
87 4
    public static function del($clientName)
88
    {
89 4
        Filter::clientName($clientName);
90 4
91 4
        unset(self::$clients[\strtolower($clientName)]);
92
    }
93
94
    /**
95
     * Delete all clients.
96
     *
97
     * @return void
98
     */
99 11
    public static function flush()
100
    {
101 11
        self::$clients         = [];
102
        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...
103
    }
104
105
    /**
106
     * @codeCoverageIgnore
107
     * @deprecated
108
     * Get the global client.
109
     *
110
     * @return Client
111 120
     * @throws ClientException
112
     */
113 120
    public static function getGlobalClient()
114
    {
115
        return self::getDefaultClient();
116
    }
117
118
    /**
119
     * Get the default client.
120
     *
121
     * @return Client
122 26
     * @throws ClientException
123
     */
124 26
    public static function getDefaultClient()
125 1
    {
126
        return self::get(CredentialsProvider::getDefaultName());
127 25
    }
128 25
129 25
    /**
130 9
     * Determine whether there is a client.
131 9
     *
132
     * @param string $clientName
133
     *
134
     * @return bool
135
     * @throws ClientException
136
     */
137
    public static function has($clientName)
138
    {
139
        Filter::clientName($clientName);
140
141
        return isset(self::$clients[\strtolower($clientName)]);
142 10
    }
143
144 10
    /**
145
     * A list of additional files to load.
146
     *
147
     * @return array
148
     * @throws ClientException when a file has a syntax error or does not exist or is not readable
149
     */
150
    public static function load()
151
    {
152
        if (\func_get_args() === []) {
153
            return (new IniCredential())->load();
154
        }
155 70
        $list = [];
156
        foreach (\func_get_args() as $filename) {
157 70
            $list[$filename] = (new IniCredential($filename))->load();
158
        }
159
160
        return $list;
161
    }
162
163
    /**
164
     * Custom Client.
165
     *
166
     * @param CredentialsInterface $credentials
167
     * @param SignatureInterface   $signature
168
     *
169
     * @return Client
170 7
     */
171
    public static function client(CredentialsInterface $credentials, SignatureInterface $signature)
172 7
    {
173
        return new Client($credentials, $signature);
174
    }
175
176
    /**
177
     * Use the AccessKey to complete the authentication.
178
     *
179
     * @param string $accessKeyId
180
     * @param string $accessKeySecret
181
     *
182 8
     * @return AccessKeyClient
183
     * @throws ClientException
184 8
     */
185
    public static function accessKeyClient($accessKeyId, $accessKeySecret)
186
    {
187
        return new AccessKeyClient($accessKeyId, $accessKeySecret);
188
    }
189
190
    /**
191
     * Use the AssumeRole of the RAM account to complete  the authentication.
192
     *
193
     * @param string $accessKeyId
194 15
     * @param string $accessKeySecret
195
     * @param string $roleArn
196 15
     * @param string $roleSessionName
197
     *
198
     * @return RamRoleArnClient
199
     * @throws ClientException
200
     */
201
    public static function ramRoleArnClient($accessKeyId, $accessKeySecret, $roleArn, $roleSessionName)
202
    {
203
        return new RamRoleArnClient($accessKeyId, $accessKeySecret, $roleArn, $roleSessionName);
204
    }
205
206
    /**
207
     * Use the RAM role of an ECS instance to complete the authentication.
208 1
     *
209
     * @param string $roleName
210 1
     *
211
     * @return EcsRamRoleClient
212
     * @throws ClientException
213
     */
214
    public static function ecsRamRoleClient($roleName)
215
    {
216
        return new EcsRamRoleClient($roleName);
217
    }
218
219
    /**
220
     * Use the Bearer Token to complete the authentication.
221
     *
222 8
     * @param string $bearerToken
223
     *
224 8
     * @return BearerTokenClient
225
     * @throws ClientException
226
     */
227
    public static function bearerTokenClient($bearerToken)
228
    {
229
        return new BearerTokenClient($bearerToken);
230
    }
231
232
    /**
233
     * Use the STS Token to complete the authentication.
234
     *
235
     * @param string $accessKeyId     Access key ID
236
     * @param string $accessKeySecret Access Key Secret
237
     * @param string $securityToken   Security Token
238
     *
239
     * @return StsClient
240
     * @throws ClientException
241
     */
242
    public static function stsClient($accessKeyId, $accessKeySecret, $securityToken = '')
243
    {
244
        return new StsClient($accessKeyId, $accessKeySecret, $securityToken);
245
    }
246
247
    /**
248
     * Use the RSA key pair to complete the authentication (supported only on Japanese site)
249
     *
250
     * @param string $publicKeyId
251
     * @param string $privateKeyFile
252
     *
253
     * @return RsaKeyPairClient
254
     * @throws ClientException
255
     */
256
    public static function rsaKeyPairClient($publicKeyId, $privateKeyFile)
257
    {
258
        return new RsaKeyPairClient($publicKeyId, $privateKeyFile);
259
    }
260
}
261