Passed
Push — master ( 19f840...c2c535 )
by Alain
02:35
created

ConfigFactory::createSubConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2.0116
1
<?php
2
/**
3
 * Bright Nucleus Config Component.
4
 *
5
 * @package   BrightNucleus\Config
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   MIT
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\Config;
13
14
use Exception;
15
16
/**
17
 * Create new object instances that implement ConfigInterface.
18
 *
19
 * @since   0.3.0
20
 *
21
 * @package BrightNucleus\Config
22
 * @author  Alain Schlesser <[email protected]>
23
 */
24
class ConfigFactory
25
{
26
27
    /**
28
     * Cached contents of the config files.
29
     *
30
     * @since 0.4.3
31
     *
32
     * @var array
33
     */
34
    protected static $configFilesCache = [];
35
36
    /**
37
     * Create a new ConfigInterface object from a file.
38
     *
39
     * If a comma-separated list of files is provided, they are checked in sequence until the first one could be loaded
40
     * successfully.
41
     *
42
     * @since 0.3.0
43
     *
44
     * @param string|array $_ List of files.
45
     *
46
     * @return ConfigInterface Instance of a ConfigInterface implementation.
0 ignored issues
show
Documentation introduced by
Should the return type not be Config|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
47
     */
48 2
    public static function createFromFile($_)
0 ignored issues
show
Unused Code introduced by
The parameter $_ is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50 2
        $files = array_reverse(func_get_args());
51
52 2
        if (is_array($files[0])) {
53 1
            $files = $files[0];
54
        }
55
56 2
        while (count($files) > 0) {
57
            try {
58 2
                $file = array_pop($files);
59
60 2
                if (! is_readable($file)) {
61 1
                    continue;
62
                }
63
64 2
                $config = static::createFromArray(
65 2
                    static::getFromCache($file, function ($file) {
66 1
                        return Loader::load($file);
67 2
                    })
68
                );
69
70 2
                if (null === $config) {
71
                    continue;
72
                }
73
74 2
                return $config;
75
            } catch (Exception $exception) {
76
                // Fail silently and try next file.
77
            }
78
        }
79
80
        return static::createFromArray([]);
81
    }
82
83
    /**
84
     * Create a new ConfigInterface object from an array.
85
     *
86
     * @since 0.3.0
87
     *
88
     * @param array $array Array with configuration values.
89
     *
90
     * @return ConfigInterface Instance of a ConfigInterface implementation.
0 ignored issues
show
Documentation introduced by
Should the return type not be Config|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
91
     */
92 2
    public static function createFromArray(array $array)
93
    {
94
        try {
95 2
            return new Config($array);
96
        } catch (Exception $exception) {
97
            // Fail silently and try next file.
98
        }
99
100
        return null;
101
    }
102
103
    /**
104
     * Create a new ConfigInterface object.
105
     *
106
     * Tries to deduce the correct creation method by inspecting the provided arguments.
107
     *
108
     * @since 0.3.0
109
     *
110
     * @param mixed $_ Array with configuration values.
111
     *
112
     * @return ConfigInterface Instance of a ConfigInterface implementation.
0 ignored issues
show
Documentation introduced by
Should the return type not be Config|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
113
     */
114 4
    public static function create($_)
0 ignored issues
show
Unused Code introduced by
The parameter $_ is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115
    {
116 4
        if (func_num_args() < 1) {
117
            return static::createFromArray([]);
118
        }
119
120 4
        $arguments = func_get_args();
121
122 4
        if (is_array($arguments[0]) && func_num_args() === 1) {
123 1
            return static::createFromArray($arguments[0]);
124
        }
125
126 3
        return static::createFromFile($arguments);
127
    }
128
129
    /**
130
     * Create a new ConfigInterface object from a file and return a sub-portion of it.
131
     *
132
     * The first argument needs to be the file name to load, and the subsequent arguments will be passed on to
133
     * `Config::getSubConfig()`.
134
     *
135
     * @since 0.4.5
136
     *
137
     * @param mixed $_ File name of the config to load as a string, followed by an array of keys to pass to
138
     *                 `Config::getSubConfig()`.
139
     *
140
     * @return ConfigInterface Instance of a ConfigInterface implementation.
141
     */
142 1
    public static function createSubConfig($_)
1 ignored issue
show
Unused Code introduced by
The parameter $_ is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
143
    {
144 1
        if (func_num_args() < 2) {
145
            return static::createFromArray([]);
146
        }
147
148 1
        $arguments = func_get_args();
149 1
        $file      = array_shift($arguments);
150
151 1
        $config = static::createFromFile($file);
152
153 1
        return $config->getSubConfig($arguments);
154
    }
155
156
    /**
157
     * Get a config file from the config file cache.
158
     *
159
     * @since 0.4.4
160
     *
161
     * @param string $identifier Identifier to look for in the cache.
162
     * @param mixed  $fallback   Fallback to use to fill the cache. If $fallback is a callable, it will be executed
163
     *                           with $identifier as an argument.
164
     *
165
     * @return mixed The latest content of the cache for the given identifier.
166
     */
167 1
    protected static function getFromCache($identifier, $fallback)
168
    {
169 1
        if (! array_key_exists($identifier, static::$configFilesCache)) {
170 1
            static::$configFilesCache[$identifier] = is_callable($fallback)
171 1
                ? $fallback($identifier)
172
                : $fallback;
173
        }
174
175 1
        return static::$configFilesCache[$identifier];
176
    }
177
}
178