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
Push — master ( 3c112e...7b66c6 )
by Hong
02:53
created

ConfigFileLoader::setRootDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Config
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Config\Loader;
16
17
use Phossa2\Shared\Reader\Reader;
18
use Phossa2\Config\Message\Message;
19
use Phossa2\Shared\Base\ObjectAbstract;
20
use Phossa2\Config\Exception\LogicException;
21
use Phossa2\Config\Exception\InvalidArgumentException;
22
23
/**
24
 * ConfigFileLoader
25
 *
26
 * Read configs from file.
27
 *
28
 * @package Phossa2\Config
29
 * @author  Hong Zhang <[email protected]>
30
 * @version 2.0.0
31
 * @since   2.0.0 added
32
 */
33
class ConfigFileLoader extends ObjectAbstract implements ConfigLoaderInterface
34
{
35
    /**
36
     * Config root directory
37
     *
38
     * @var    string
39
     * @access protected
40
     */
41
    protected $root_dir;
42
43
    /**
44
     * config file type
45
     *
46
     * @var    string
47
     * @access protected
48
     */
49
    protected $file_type;
50
51
    /**
52
     * cached subdirs to load files
53
     *
54
     * @var    array
55
     * @access protected
56
     */
57
    protected $sub_dirs = [];
58
59
    /**
60
     * subdirs to search
61
     *
62
     * @var    string
63
     * @access protected
64
     */
65
    protected $environment;
66
67
    /**
68
     * Constructor
69
     *
70
     * @param  string $rootDir
71
     * @param  string $environment
72
     * @param  string $fileType
73
     * @throws InvalidArgumentException if any argument invalid
74
     * @access public
75
     * @api
76
     */
77
    public function __construct(
78
        /*# string */ $rootDir,
79
        /*# string */ $environment = '',
80
        /*# string */ $fileType = 'php'
81
    ) {
82
        $this
83
            ->setRootDir($rootDir)
84
            ->setFileType($fileType)
85
            ->setEnvironment($environment);
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91
    public function load(
92
        /*# string */ $group,
93
        $environment = null
94
    )/*# : array */ {
95
        $data = [];
96
        foreach ($this->globFiles($group, $environment) as $file) {
97
            $grp = basename($file, '.' . $this->file_type);
98
            if (!isset($data[$grp])) {
99
                $data[$grp] = [];
100
            }
101
            try {
102
                $data[$grp] = array_replace_recursive(
103
                    $data[$grp],
104
                    (array) Reader::readFile($file)
105
                );
106
            } catch (\Exception $e) {
107
                throw new LogicException($e->getMessage(), $e->getCode());
108
            }
109
        }
110
        return $data;
111
    }
112
113
    /**
114
     * Set config file root directory
115
     *
116
     * @param  string $rootDir
117
     * @return $this
118
     * @throws InvalidArgumentException if dir is bad
119
     * @access public
120
     * @api
121
     */
122
    public function setRootDir(/*# string */ $rootDir)
123
    {
124
        $dir = realpath($rootDir);
125
126
        if (false === $dir) {
127
            throw new InvalidArgumentException(
128
                Message::get(Message::CONFIG_ROOT_INVALID, $rootDir),
129
                Message::CONFIG_ROOT_INVALID
130
            );
131
        }
132
133
        $this->root_dir = $dir . \DIRECTORY_SEPARATOR;
134
135
        return $this;
136
    }
137
138
    /**
139
     * Set config file type
140
     *
141
     * @param  string $fileType
142
     * @return $this
143
     * @throws InvalidArgumentException if unsupported file type
144
     * @access public
145
     * @api
146
     */
147
    public function setFileType(/*# string */ $fileType)
148
    {
149
        if (!Reader::isSupported($fileType)) {
150
            throw new InvalidArgumentException(
151
                Message::get(Message::CONFIG_FILE_TYPE_UNKNOWN, $fileType),
152
                Message::CONFIG_FILE_TYPE_UNKNOWN
153
            );
154
        }
155
156
        $this->file_type = $fileType;
157
        return $this;
158
    }
159
160
    /**
161
     * Set environment
162
     *
163
     * @param  string $environment
164
     * @return $this
165
     * @throws InvalidArgumentException if environment unknown
166
     * @access public
167
     * @api
168
     */
169
    public function setEnvironment(/*# string */ $environment)
170
    {
171
        $this->environment = $environment;
172
        $this->getSearchDirs($environment);
173
        return $this;
174
    }
175
176
    /**
177
     * Returns an array of files to read from
178
     *
179
     * @param  string $group
180
     * @param  null|string $environment
181
     * @return array
182
     * @throws InvalidArgumentException if environment unknown
183
     * @access protected
184
     */
185
    protected function globFiles(
186
        /*# string */ $group,
187
        $environment
188
    )/*# : array */ {
189
        $files = [];
190
        $group = '' === $group ? '*' : $group;
191
        foreach($this->getSearchDirs($environment) as $dir) {
192
            $file  = $dir . $group . '.' . $this->file_type;
193
            $files = array_merge($files, glob($file));
194
        }
195
        return $files;
196
    }
197
198
    /**
199
     * Get the search directories
200
     *
201
     * @param  string|null $environment
202
     * @return array
203
     * @access protected
204
     */
205
    protected function getSearchDirs($environment)/*# : array */
206
    {
207
        // use default is NULL supplied
208
        $env = $environment ?: $this->environment;
209
210
        if (!isset($this->sub_dirs[$env])) {
211
            $this->sub_dirs[$env] = $this->buildSearchDirs($env);
212
        }
213
        return $this->sub_dirs[$env];
214
    }
215
216
    /**
217
     * Build search directories
218
     *
219
     * @param  null|string $environment
220
     * @return array
221
     * @throws InvalidArgumentException if environment unknown
222
     * @access protected
223
     */
224
    protected function buildSearchDirs(/*# string */ $environment)/*# : array */
225
    {
226
        $path = $this->root_dir;
227
        $subs = preg_split(
228
            '/[\/\\\]/', trim($environment, '/\\'), 0, \PREG_SPLIT_NO_EMPTY
229
        );
230
231
        $subdirs = [$path];
232
        foreach($subs as $dir) {
233
            $path .= $dir . \DIRECTORY_SEPARATOR;
234
            if (false === file_exists($path)) {
235
                throw new InvalidArgumentException(
236
                    Message::get(Message::CONFIG_ENV_UNKNOWN, $environment),
237
                    Message::CONFIG_ENV_UNKNOWN
238
                );
239
            }
240
            $subdirs[] = $path;
241
        }
242
243
        return $subdirs;
244
    }
245
}
246