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 ( c5e9e2...3c112e )
by Hong
03:20
created

ConfigFileLoader::buildSearchDirs()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.6845
cc 4
eloc 16
nc 4
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
     * Constructor
61
     *
62
     * @param  string $rootDir
63
     * @param  string $environment
64
     * @param  string $fileType
65
     * @throws InvalidArgumentException if any argument invalid
66
     * @access public
67
     * @api
68
     */
69
    public function __construct(
70
        /*# string */ $rootDir,
71
        /*# string */ $environment = '',
72
        /*# string */ $fileType = 'php'
73
    ) {
74
        $this
75
            ->setRootDir($rootDir)
76
            ->setFileType($fileType)
77
            ->setEnvironment($environment);
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function load(
84
        /*# string */ $group,
85
        $environment = null
86
    )/*# : array */ {
87
        $data = [];
88
        foreach ($this->globFiles($group, $environment) as $file) {
89
            $grp = basename($file, '.' . $this->file_type);
90
            if (!isset($data[$grp])) {
91
                $data[$grp] = [];
92
            }
93
            try {
94
                $data[$grp] = array_replace_recursive(
95
                    $data[$grp],
96
                    (array) Reader::readFile($file)
97
                );
98
            } catch (\Exception $e) {
99
                throw new LogicException($e->getMessage(), $e->getCode());
100
            }
101
        }
102
        return $data;
103
    }
104
105
    /**
106
     * Set config file root directory
107
     *
108
     * @param  string $rootDir
109
     * @return $this
110
     * @throws InvalidArgumentException if dir is bad
111
     * @access public
112
     * @api
113
     */
114
    public function setRootDir(/*# string */ $rootDir)
115
    {
116
        $dir = realpath($rootDir);
117
        if (false === $dir) {
118
            throw new InvalidArgumentException(
119
                Message::get(Message::CONFIG_ROOT_INVALID, $rootDir),
120
                Message::CONFIG_ROOT_INVALID
121
            );
122
        }
123
124
        $this->root_dir = $dir . \DIRECTORY_SEPARATOR;
125
126
        return $this;
127
    }
128
129
    /**
130
     * Set config file type
131
     *
132
     * @param  string $fileType
133
     * @return $this
134
     * @throws InvalidArgumentException if unsupported file type
135
     * @access public
136
     * @api
137
     */
138
    public function setFileType(/*# string */ $fileType)
139
    {
140
        if (!Reader::isSupported($fileType)) {
141
            throw new InvalidArgumentException(
142
                Message::get(Message::CONFIG_FILE_TYPE_UNKNOWN, $fileType),
143
                Message::CONFIG_FILE_TYPE_UNKNOWN
144
            );
145
        }
146
147
        $this->file_type = $fileType;
148
        return $this;
149
    }
150
151
    /**
152
     * Set environment
153
     *
154
     * @param  string $environment
155
     * @return $this
156
     * @throws InvalidArgumentException if environment unknown
157
     * @access public
158
     * @api
159
     */
160
    public function setEnvironment(/*# string */ $environment)
161
    {
162
        $this->sub_dirs = $this->buildSearchDirs($environment);
163
        return $this;
164
    }
165
166
    /**
167
     * Returns an array of files to read from
168
     *
169
     * @param  string $group
170
     * @param  null|string $environment
171
     * @return array
172
     * @throws InvalidArgumentException if environment unknown
173
     * @access protected
174
     */
175
    protected function globFiles(
176
        /*# string */ $group,
177
        $environment
178
    )/*# : array */ {
179
        $files = [];
180
        $group = '' === $group ? '*' : $group;
181
        foreach($this->buildSearchDirs($environment) as $dir) {
182
            $file = $dir . $group . '.' . $this->file_type;
183
            $files = array_merge($files, glob($file));
184
        }
185
        return $files;
186
    }
187
188
    /**
189
     * Build search directories
190
     *
191
     * @param  null|string $environment
192
     * @return array
193
     * @throws InvalidArgumentException if environment unknown
194
     * @access protected
195
     */
196
    protected function buildSearchDirs($environment)/*# : array */
197
    {
198
        if (null === $environment) {
199
            return $this->sub_dirs;
200
        } else {
201
            $path = $this->root_dir;
202
            $subdirs = [$path];
203
            $subs = preg_split('/[\/\\\]/', trim($environment, '/\\'), 0,
204
                \PREG_SPLIT_NO_EMPTY);
205
206
            foreach($subs as $dir) {
207
                $path .= $dir . \DIRECTORY_SEPARATOR;
208
                if (false === file_exists($path)) {
209
                    throw new InvalidArgumentException(
210
                        Message::get(Message::CONFIG_ENV_UNKNOWN, $environment),
211
                        Message::CONFIG_ENV_UNKNOWN
212
                    );
213
                }
214
                $subdirs[] = $path;
215
            }
216
217
            return $subdirs;
218
        }
219
    }
220
}
221