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 ( 0ba6d7...8aa6f4 )
by Miles
03:04
created

Vars::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 23
ccs 17
cts 17
cp 1
rs 9.0856
cc 3
eloc 15
nc 4
nop 2
crap 3
1
<?php
2
3
/**
4
 * This file is part of the m1\vars library
5
 *
6
 * (c) m1 <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @package     m1/vars
12
 * @version     1.0.0
13
 * @author      Miles Croxford <[email protected]>
14
 * @copyright   Copyright (c) Miles Croxford <[email protected]>
15
 * @license     http://github.com/m1/vars/blob/master/LICENSE
16
 * @link        http://github.com/m1/vars/blob/master/README.MD Documentation
17
 */
18
19
namespace M1\Vars;
20
21
use M1\Vars\Cache\CacheProvider;
22
use M1\Vars\Loader\LoaderProvider;
23
use M1\Vars\Resource\AbstractResource;
24
use M1\Vars\Resource\ResourceProvider;
25
use M1\Vars\Traits\PathTrait;
26
use M1\Vars\Traits\TransformerTrait;
27
use M1\Vars\Variables\VariableProvider;
28
29
/**
30
 * Vars core class
31
 *
32
 * @since 0.1.0
33
 */
34
class Vars extends AbstractResource
35
{
36
    /**
37
     * Used for path functions and variables
38
     */
39
    use PathTrait;
40
41
    /**
42
     * Used for to* functions
43
     */
44
    use TransformerTrait;
45
46
    /**
47
     * The cache object if the cache is wanted, else false
48
     *
49
     * @var \M1\Vars\Cache\CacheProvider $cache
50
     */
51
    public $cache;
52
53
    /**
54
     * The default options for Vars
55
     *
56
     * @var array $default_options
57
     */
58
    private $default_options = array(
59
        'path' => null,
60
        'cache' => true,
61
        'cache_path' => null,
62
        'cache_expire' => 300, // 5 minutes
63
        'loaders' => array('env', 'ini', 'json', 'php', 'toml', 'yaml', 'xml',)
64
    );
65
66
    /**
67
     * The loaderProvider for Vars supplies the file loaders and the extensions that are supported
68
     *
69
     * @var \M1\Vars\Loader\LoaderProvider $loader
70
     */
71
    public $loader;
72
73
    /**
74
     * Have the base and cache paths been set
75
     *
76
     * @var bool $paths_loaded
77
     */
78
    private $paths_loaded = false;
79
80
    /**
81
     * The imported resources
82
     *
83
     * @var array $resources
84
     */
85
    private $resources = array();
86
87
    /**
88
     * The variable provider
89
     *
90
     * @var \M1\Vars\Variables\VariableProvider $variables
91
     */
92
    public $variables;
93
94
    /**
95
     * Creates a new instance of Vars
96
     *
97
     * @param string|array $resource The main configuration resource
98
     * @param array        $options  The options being used for Vars
99
     */
100 83
    public function __construct($resource, $options = array())
101
    {
102 83
        $options = $this->parseOptions($options);
103 83
        $this->makeCache($options, $resource);
104 82
        $this->makePaths($options);
105 81
        if (!$this->cache->checkCache()) {
106 81
            $this->makeLoader($options);
107 79
            $this->makeVariables($options);
108
109 78
            $resource = new ResourceProvider($this, $resource);
110 63
        }
111
112 63
        if ($this->cache->isHit()) {
113 3
            $this->loadFromCache();
114 3
        } else {
115 63
            $resource->mergeParentContent();
0 ignored issues
show
Bug introduced by
It seems like $resource is not always an object, but can also be of type string|array. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
116 63
            $this->content = $resource->getContent();
117
118 63
            $this->cache->setTime(time());
119 63
            $this->cache->makeCache($this);
120
121
        }
122 63
    }
123
124
    /**
125
     * Parses the options so Vars can use them
126
     *
127
     * @param array $options  The options being used for Vars
128
     *
129
     * @return array The parsed options
130
     */
131 83
    private function parseOptions(array $options)
132
    {
133 83
        $parsed_options = array_merge($this->default_options, $options);
134 83
        $parsed_options['loaders'] = (isset($options['loaders'])) ? $options['loaders'] : $this->default_options['loaders'];
135 83
        return $parsed_options;
136
    }
137
138
    /**
139
     * Makes the CacheProvider with the options
140
     *
141
     * @param array        $options  The options being used for Vars
142
     * @param array|string $resource The main configuration resource
143
     */
144 83
    private function makeCache($options, $resource)
145
    {
146 83
        $cache = new CacheProvider($resource, $options);
147 82
        $this->cache = $cache;
148 82
    }
149
150
    /**
151
     * Sets the base path if the options have been set and the cache path if the cache path has not been set but the
152
     * base path has
153
     *
154
     * @param array $options The options being used for Vars
155
     */
156 82
    private function makePaths($options)
157
    {
158 82
        $this->setPath($options['path']);
159
160 81
        if (is_null($options['cache_path']) && !is_null($options['path'])) {
161 1
            $this->cache->setPath($options['path']);
162 1
            $this->paths_loaded = true;
163 1
        }
164 81
    }
165
166
    /**
167
     * Makes the LoaderProvider with the options
168
     *
169
     * @param array $options  The options being used for Vars
170
     */
171 81
    private function makeLoader($options)
172
    {
173 81
        $loader = new LoaderProvider($options, $this->default_options['loaders']);
174 79
        $this->loader = $loader;
175 79
    }
176
177
    /**
178
     * Sets the replacement variables if the option has been set
179
     *
180
     * @param array|null $options The options being used for Vars
181
     */
182 79
    private function makeVariables($options)
183
    {
184 79
        $this->variables = new VariableProvider($this);
185
186 79
        if (isset($options['replacements'])) {
187 4
            $this->variables->rstore->load($options['replacements']);
188 3
        }
189 78
    }
190
191
    /**
192
     * Loads the cached file into the current class
193
     */
194 3
    private function loadFromCache()
195
    {
196 3
        $this->cache->load();
197
198
        $passed_keys = array(
199 3
            'path',
200 3
            'content',
201 3
            'extensions',
202 3
            'loaders',
203 3
            'resources',
204 3
            'replacements',
205 3
        );
206
207 3
        $loaded_vars = get_object_vars($this->cache->getLoadedVars());
208
209 3
        foreach ($loaded_vars as $key => $value) {
210 3
            if (in_array($key, $passed_keys)) {
211 3
                $this->$key = $value;
212 3
            }
213 3
        }
214
215 3
        $this->cache->setTime($loaded_vars['cache']->getTime());
216 3
    }
217
218
    /**
219
     * Checks if the base and cache paths have been set, if not set then will use the $resource as the base path
220
     *
221
     * @param string $resource The resource to use to set the paths if they haven't been set
222
     */
223 72
    public function pathsLoadedCheck($resource)
224
    {
225 72
        if (!$this->paths_loaded) {
226 71
            $path = $this->getPath();
227
228 71
            if (!$path) {
229 69
                $file = pathinfo(realpath($resource));
230 69
                $path = $file['dirname'];
231 69
                $this->setPath($path);
232 69
            }
233
234 71
            if ($this->cache->getProvide() && !$this->cache->getPath()) {
235 2
                $this->cache->setPath($path);
236 2
            }
237
238 71
            $this->paths_loaded = true;
239 71
        }
240 72
    }
241
242
    /**
243
     * Adds a resource to $this->resources
244
     *
245
     * @param string $resource Resource to add to the stack
246
     *
247
     * @return int The position of the added resource
248
     */
249 72
    public function addResource($resource)
250
    {
251 72
        $r = realpath($resource);
252 72
        $pos = count($this->resources);
253 72
        $this->resources[$pos] = $r;
254 72
        return $pos;
255
    }
256
257
    /**
258
     * Updates the string resource with the FileResource
259
     *
260
     * @param \M1\Vars\Resource\FileResource $resource The FileResource to add
261
     * @param int                            $pos      The position of the string resource
262
     *
263
     * @return \M1\Vars\Vars
264
     */
265 61
    public function updateResource($resource, $pos)
266
    {
267 61
        $this->resources[$pos] = $resource;
268 61
        return $this;
269
    }
270
271
    /**
272
     * Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop
273
     *
274
     * @param \M1\Vars\Resource\FileResource|string $resource Resource to check
275
     *
276
     * @return bool Has resource already been imported
277
     */
278 72
    public function resourceImported($resource)
279
    {
280 72
        $resource = realpath($resource);
281 72
        foreach ($this->getResources() as $r) {
282 49
            if ((is_a($r, 'M1\Vars\Resource\FileResource') && $resource === $r->getFile()) ||
283 49
                (is_string($r) && $resource === $r)) {
284 1
                return true;
285
            }
286 72
        }
287 72
        return false;
288
    }
289
290
    /**
291
     * Searches the resource stack for a certain resource
292
     *
293
     * @param string $resource The resource to search for
294
     *
295
     * @return bool Returns the resource if found
296
     */
297 3
    public function getResource($resource)
298
    {
299 3
        foreach ($this->getResources() as $r) {
300 3
            if ($resource === $r->getFilename()) {
301 2
                return $r;
302
            }
303 3
        }
304
305 1
        return false;
306
    }
307
308
    /**
309
     * Returns the imported resources
310
     *
311
     * @return array The Vars imported resources
312
     */
313 72
    public function getResources()
314
    {
315 72
        return $this->resources;
316
    }
317
318
    /**
319
     * Returns the CacheProvider if set, if not return false
320
     *
321
     * @return \M1\Vars\Cache\CacheProvider|false The CacheProvider or false
322
     */
323 7
    public function getCache()
324
    {
325 7
        return $this->cache;
326
    }
327
}
328