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.

WrappedConfig   B
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 419
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 39
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 419
rs 8.2857

24 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfig() 0 4 1
A setConfig() 0 4 1
A emptyConfig() 0 4 1
A getName() 0 4 1
A setName() 0 4 1
A getFilename() 0 4 1
A setFilename() 0 4 1
A getExpandedConfig() 0 4 1
A getData() 0 4 1
A getArray() 0 4 1
A getObject() 0 4 1
A hasData() 0 4 1
A mergeData() 0 4 1
A unsetData() 0 4 1
A __construct() 0 10 2
A hasConfig() 0 12 3
B removeConfig() 0 24 4
A setData() 0 11 2
A saveConfig() 0 17 3
A makeConfigDir() 0 17 3
A __get() 0 4 1
A __set() 0 4 1
B loadConfigFromFile() 0 36 5
A setNameFromFilename() 0 4 1
1
<?php
2
3
/**
4
 * Copyright (c) 2011-present Mediasift Ltd
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *
14
 *   * Redistributions in binary form must reproduce the above copyright
15
 *     notice, this list of conditions and the following disclaimer in
16
 *     the documentation and/or other materials provided with the
17
 *     distribution.
18
 *
19
 *   * Neither the names of the copyright holders nor the names of his
20
 *     contributors may be used to endorse or promote products derived
21
 *     from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category  Libraries
37
 * @package   Storyplayer/ConfigLib
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2011-present Mediasift Ltd www.datasift.com
40
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
41
 * @link      http://datasift.github.io/storyplayer
42
 */
43
44
namespace DataSift\Storyplayer\ConfigLib;
45
46
use DataSift\Stone\ObjectLib\BaseObject;
47
48
/**
49
 * represents config loaded from a single file
50
 *
51
 * @category  Libraries
52
 * @package   Storyplayer/ConfigLib
53
 * @author    Stuart Herbert <[email protected]>
54
 * @copyright 2011-present Mediasift Ltd www.datasift.com
55
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
56
 * @link      http://datasift.github.io/storyplayer
57
 */
58
class WrappedConfig
59
{
60
    /**
61
     * used when we do not know the name of the config we are wrapping
62
     */
63
    const NO_NAME = "UNKNOWN";
64
65
    const ROOT_IS_OBJECT = false;
66
    const ROOT_IS_ARRAY = true;
67
68
    /**
69
     * the config settings that this object wraps
70
     * @var BaseObject|array
71
     */
72
    private $config;
73
74
    /**
75
     * remember where we were loaded from
76
     * @var string|null
77
     */
78
    private $filename = null;
79
80
    /**
81
     * the name assigned to this config container
82
     * @var string|null
83
     */
84
    private $name = null;
85
86
    /**
87
     * constructor
88
     */
89
    public function __construct($isArray = false)
90
    {
91
        $this->setName("UNKNOWN");
92
        if (!$isArray) {
93
            $this->setConfig(new BaseObject);
94
        }
95
        else {
96
            $this->setConfig([]);
97
        }
98
    }
99
100
    /**
101
     * get the config that we wrap
102
     *
103
     * @return array|object
104
     */
105
    public function &getConfig()
106
    {
107
        return $this->config;
108
    }
109
110
    /**
111
     * store the config that we wrap
112
     *
113
     * @param array|object $config
114
     *        the config to store
115
     */
116
    public function setConfig($config)
117
    {
118
        $this->config = $config;
119
    }
120
121
    /**
122
     * do we have any config?
123
     *
124
     * @return boolean
125
     */
126
    public function hasConfig()
127
    {
128
        if ($this->config instanceof BaseObject) {
129
            return $this->config->hasProperties();
130
        }
131
132
        if (is_array($this->config)) {
133
            return empty($this->config);
134
        }
135
136
        return false;
137
    }
138
139
    /**
140
     * load a config file from disk, and store the config in $this
141
     *
142
     * @param  string $pathToFile
143
     *         the filename to load from
144
     * @return void
145
     */
146
    public function loadConfigFromFile($pathToFile)
147
    {
148
        if (!file_exists($pathToFile)) {
149
            throw new E4xx_ConfigFileNotFound($pathToFile);
150
        }
151
152
        $raw = @file_get_contents($pathToFile);
153
        if (false === $raw) {
154
            throw new E4xx_ConfigFileNotFound($pathToFile);
155
        }
156
157
        // special case - file is empty
158
        if (strlen(rtrim($raw)) === 0) {
159
            // nothing to see, move along :)
160
            $this->setConfig(new BaseObject);
161
            $this->setNameFromFilename($pathToFile);
162
            return;
163
        }
164
165
        // if we get here, we expect the file to contain valid JSON
166
        $json = @json_decode($raw);
167
        if (null === $json) {
168
            throw new E4xx_ConfigFileContainsInvalidJson($pathToFile);
169
        }
170
171
        // convert from stdClass to our useful BaseObject
172
        $config = new BaseObject;
173
        $config->mergeFrom($json);
174
175
        // store the config
176
        $this->setConfig($config);
177
        $this->setNameFromFilename($pathToFile);
178
        $this->setFilename($pathToFile);
179
180
        // all done
181
    }
182
183
    /**
184
     * set the name of this config by looking at the filename
185
     *
186
     * the 'name' is used as an array key elsewhere. if we get this wrong,
187
     * then Storyplayer isn't going to be able to find our config later on
188
     *
189
     * override this in more specialist config types
190
     *
191
     * @param  string $filename
192
     *         the path/to/file/name.ext where we found this config
193
     * @return void
194
     */
195
    protected function setNameFromFilename($filename)
196
    {
197
        $this->setName(basename($filename, '.json'));
198
    }
199
200
    /**
201
     * save the config to disk, as a JSON file
202
     *
203
     * @return void
204
     */
205
    public function saveConfig()
206
    {
207
        // do we have a filename?
208
        $filename = $this->getFilename();
209
        if ($filename === null) {
210
            throw new E4xx_ConfigNeedsAFilename($this->getName());
211
        }
212
213
        // make sure that the parent folder exists
214
        $this->makeConfigDir(dirname($filename));
215
216
        // let's get this saved
217
        $data = json_encode($this->getConfig(), JSON_PRETTY_PRINT);
218
        if (!file_put_contents($filename, $data)) {
219
            throw new E4xx_ConfigCannotBeSaved($name, $filename);
220
        }
221
    }
222
223
    /**
224
     * @return void
225
     */
226
    protected function makeConfigDir($configDir)
227
    {
228
        if (file_exists($configDir)) {
229
            // nothing to do
230
            return;
231
        }
232
233
        // can we make the folder?
234
        //
235
        // if this fails, we do not know why
236
        $success = mkdir($configDir, 0700, true);
237
        if (!$success)
238
        {
239
            // cannot create it - bail out now
240
            throw new E4xx_ConfigPathCannotBeCreated($configDir);
241
        }
242
    }
243
244
    /**
245
     * forget all of the config we (may) currently have
246
     *
247
     * @return void
248
     */
249
    public function emptyConfig()
250
    {
251
        $this->config = new BaseObject();
252
    }
253
254
    /**
255
     * @return void
256
     */
257
    public function removeConfig()
258
    {
259
        // do we have a filename?
260
        $filename = $this->getFilename();
261
        if ($filename === null) {
262
            throw new E4xx_ConfigNeedsAFilename($this->getName());
263
        }
264
265
        // does the file exist?
266
        if (!file_exists($filename)) {
267
            // nothing to do
268
            return;
269
        }
270
271
        // remove the file
272
        $deleted = @unlink($filename);
273
        if ($deleted) {
274
            // all done
275
            return;
276
        }
277
278
        // if we get here, we could not remove the file
279
        throw new E4xx_ConfigCannotBeRemoved($this->getName(), $filename);
280
    }
281
282
    /**
283
     * returns the name assigned to this config
284
     * @return string|null
285
     */
286
    public function getName()
287
    {
288
        return $this->name;
289
    }
290
291
    /**
292
     * assigns a name to this config
293
     *
294
     * @param string $name
295
     *        the name to assign
296
     */
297
    public function setName($name)
298
    {
299
        $this->name = $name;
300
    }
301
302
    /**
303
     * returns the filename we loaded this config from
304
     *
305
     * @return string|null
306
     */
307
    public function getFilename()
308
    {
309
        return $this->filename;
310
    }
311
312
    /**
313
     * sets the filename that we loaded this config from
314
     *
315
     * @param string $filename
316
     *        the filename to store
317
     */
318
    public function setFilename($filename)
319
    {
320
        $this->filename = $filename;
321
    }
322
323
    // ==================================================================
324
    //
325
    // dot.notation.support
326
    //
327
    // ------------------------------------------------------------------
328
329
    /**
330
     * expand any variables in $this
331
     *
332
     * @param  array|object|null $baseConfig
333
     *         the config to use for expanding variables (optional)
334
     * @return array|object
335
     *         a copy of the config stored in $this, with any Twig
336
     *         variables expanded
337
     */
338
    public function getExpandedConfig($baseConfig = null)
339
    {
340
        return $this->config->getExpandedData($baseConfig);
341
    }
342
343
    /**
344
     * retrieve data using a dot.notation.path
345
     *
346
     * NOTE that you should treat any data returned from here as READ-ONLY
347
     *
348
     * @param  string $path
349
     *         the dot.notation.path to use to navigate
350
     *
351
     * @return mixed
352
     */
353
    public function getData($path)
354
    {
355
        return $this->config->getData($path);
356
    }
357
358
    /**
359
     * retrieve data from a dot.notation.path
360
     *
361
     * throws an exception if the path does not point to an array
362
     *
363
     * @param string $path
364
     *        the dot.notation.path to the data to return
365
     * @return array
366
     */
367
    public function getArray($path)
368
    {
369
        return $this->config->getArray($path);
370
    }
371
372
    /**
373
     * retrieve data from a dot.notation.path
374
     *
375
     * throws an exception if the path does not point to an object
376
     *
377
     * @param string $path
378
     *        the dot.notation.path to the data to return
379
     * @return object
380
     */
381
    public function getObject($path)
382
    {
383
        return $this->config->getObject($path);
384
    }
385
386
    /**
387
     * check for existence of data using a dot.notation.path
388
     *
389
     * @param  string $path
390
     *         the dot.notation.path to use to navigate
391
     *
392
     * @return boolean
393
     */
394
    public function hasData($path)
395
    {
396
        return $this->config->hasData($path);
397
    }
398
399
    /**
400
     * merge data into this config
401
     *
402
     * @param  string $path
403
     *         path.to.merge.to
404
     * @param  mixed $dataToMerge
405
     *         the data to merge at $path
406
     * @return void
407
     */
408
    public function mergeData($path, $dataToMerge)
409
    {
410
        return $this->config->mergeData($path, $dataToMerge);
411
    }
412
413
    /**
414
     * assigns data to a specific path
415
     *
416
     * @param string $path
417
     *        the path to assign to
418
     * @param mixed $data
419
     *        the data to assign
420
     *
421
     * @return void
422
     */
423
    public function setData($path, $data)
424
    {
425
        // special case
426
        if ($path == "") {
427
            $this->config = $data;
428
            return;
429
        }
430
431
        // general case
432
        $this->config->setData($path, $data);
433
    }
434
435
    /**
436
     * support for arrow->notation support on wrapped configs
437
     *
438
     * NOTE: objects returned by arrow->notation are READ-WRITE
439
     *
440
     * @param  string $path
441
     *         the variable to be retrieved
442
     * @return mixed
443
     *         the fake attribute
444
     */
445
    public function __get($path)
446
    {
447
        return $this->config->$path;
448
    }
449
450
    /**
451
     * support for arrow->notation on wrapped configs
452
     *
453
     * @param string $path
454
     *        the name of the attribute to save
455
     * @param mixed $data
456
     *        the data to save
457
     * @return void
458
     */
459
    public function __set($path, $data)
460
    {
461
        return $this->setData($path, $data);
462
    }
463
464
    /**
465
     * remove data using a dot.notation.path
466
     *
467
     * @param  string $path
468
     *         the dot.notation.path to use to navigate
469
     *
470
     * @return void
471
     */
472
    public function unsetData($path)
473
    {
474
        return $this->config->unsetData($path);
475
    }
476
}
477