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 (#64)
by Yong
05:04
created

IniCredential::inOpenBasedir()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 20.1953

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 7
nop 1
dl 0
loc 22
ccs 4
cts 15
cp 0.2667
crap 20.1953
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace AlibabaCloud\Client\Credentials\Ini;
4
5
use AlibabaCloud\Client\Clients\Client;
6
use AlibabaCloud\Client\Exception\ClientException;
7
8
/**
9
 * Class IniCredential
10
 *
11
 * @package   AlibabaCloud\Client\Credentials\Ini
12
 */
13
class IniCredential
14
{
15
    use CreateTrait;
16
    use OptionsTrait;
17
18
    /**
19
     * @var array
20
     */
21
    private static $hasLoaded;
22
23
    /**
24
     * @var string
25
     */
26
    protected $filename;
27
28
    /**
29
     * IniCredential constructor.
30
     *
31
     * @param string $filename
32
     */
33
    public function __construct($filename = '')
34 68
    {
35
        $this->filename = $filename ?: $this->getDefaultFile();
36 68
    }
37 68
38
    /**
39
     * Get the default credential file.
40
     *
41
     * @return string
42
     */
43
    public function getDefaultFile()
44 39
    {
45
        return self::getHomeDirectory() . '/.alibabacloud/credentials';
46 39
    }
47
48
    /**
49
     * Get the credential file.
50
     *
51
     * @return string
52
     */
53
    public function getFilename()
54 39
    {
55
        return $this->filename;
56 39
    }
57
58
    /**
59
     * Gets the environment's HOME directory.
60
     *
61
     * @return null|string
62
     */
63
    private static function getHomeDirectory()
64 40
    {
65
        if (getenv('HOME')) {
66 40
            return getenv('HOME');
67 39
        }
68
69
        return (getenv('HOMEDRIVE') && getenv('HOMEPATH'))
70 1
            ? getenv('HOMEDRIVE') . getenv('HOMEPATH')
71 1
            : null;
72 1
    }
73
74
    /**
75
     * @param array  $array
76
     * @param string $key
77
     *
78
     * @return bool
79
     */
80
    protected static function isNotEmpty(array $array, $key)
81 16
    {
82
        return isset($array[$key]) && !empty($array[$key]);
83 16
    }
84
85
    /**
86
     * @param string $key
87
     * @param string $clientName
88
     *
89
     * @throws ClientException
90
     */
91
    public function missingRequired($key, $clientName)
92 28
    {
93
        throw new ClientException(
94 28
            "Missing required '$key' option for '$clientName' in " . $this->getFilename(),
95 28
            \ALIBABA_CLOUD_INVALID_CREDENTIAL
96
        );
97 28
    }
98
99
    /**
100
     * Clear credential cache.
101
     *
102
     * @return void
103
     */
104
    public static function forgetLoadedCredentialsFile()
105 24
    {
106
        self::$hasLoaded = [];
107 24
    }
108 24
109
    /**
110
     * @return array|mixed
111
     * @throws ClientException
112
     */
113
    public function load()
114 28
    {
115
        // If it has been loaded, assign the client directly.
116
        if (isset(self::$hasLoaded[$this->filename])) {
117 28
            /**
118
             * @var $client Client
119
             */
120
            foreach (self::$hasLoaded[$this->filename] as $projectName => $client) {
121 3
                $client->name($projectName);
122 3
            }
123 3
124
            return self::$hasLoaded[$this->filename];
125 3
        }
126
127
        return $this->loadFile();
128 27
    }
129
130
    /**
131
     * Exceptions will be thrown if the file is unreadable and not the default file.
132
     *
133
     * @return array|mixed
134
     * @throws ClientException
135
     */
136
    private function loadFile()
137 29
    {
138
        if (!\AlibabaCloud\Client\inOpenBasedir($this->filename)) {
139 29
            return [];
140
        }
141
142
        if (!\is_readable($this->filename) || !\is_file($this->filename)) {
143 29
            if ($this->filename === $this->getDefaultFile()) {
144 6
                // @codeCoverageIgnoreStart
145
                return [];
146
                // @codeCoverageIgnoreEnd
147
            }
148
            throw new ClientException(
149 4
                'Credential file is not readable: ' . $this->getFilename(),
150 4
                \ALIBABA_CLOUD_INVALID_CREDENTIAL
151
            );
152 4
        }
153
154
        return $this->parseFile();
155 23
    }
156
157
    /**
158
     * Decode the ini file into an array.
159
     *
160
     * @return array|mixed
161
     * @throws ClientException
162
     */
163 29
    private function parseFile()
164
    {
165 29
        try {
166 29
            $file = \parse_ini_file($this->filename, true);
167 29
            if (\is_array($file) && $file !== []) {
168
                return $this->initClients($file);
169
            }
170
            throw new ClientException(
171
                'Format error: ' . $this->getFilename(),
172
                \ALIBABA_CLOUD_INVALID_CREDENTIAL
173
            );
174
        } catch (\Exception $e) {
175
            throw new ClientException(
176
                $e->getMessage(),
177
                \ALIBABA_CLOUD_INVALID_CREDENTIAL,
178
                $e
179
            );
180
        }
181
    }
182
183
    /**
184
     * Initialize clients.
185
     *
186
     * @param array $array
187
     *
188
     * @return array|mixed
189
     * @throws ClientException
190
     */
191
    private function initClients($array)
192
    {
193 27
        foreach (\array_change_key_case($array) as $clientName => $configures) {
194
            $configures     = \array_change_key_case($configures);
195
            $clientInstance = $this->createClient($clientName, $configures);
196 27
            if ($clientInstance instanceof Client) {
197 26
                self::$hasLoaded[$this->filename][$clientName] = $clientInstance;
198 23
                self::setClientAttributes($configures, $clientInstance);
199
                self::setCert($configures, $clientInstance);
200 3
                self::setProxy($configures, $clientInstance);
201 3
            }
202
        }
203 3
204 17
        return isset(self::$hasLoaded[$this->filename])
205 17
            ? self::$hasLoaded[$this->filename]
206 17
            : [];
207 17
    }
208
}
209