Completed
Push — master ( c15e27...f659ac )
by Alain
04:34
created

ConfigTrait::getConfigCallable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2
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 BrightNucleus\Config\Exception\FailedToProcessConfigException;
15
use Exception;
16
17
/**
18
 * Basic config processing that can be included within classes.
19
 *
20
 * @since   0.1.2
21
 *
22
 * @package BrightNucleus\Config
23
 * @author  Alain Schlesser <[email protected]>
24
 */
25
trait ConfigTrait
26
{
27
28
    /**
29
     * Reference to the Config object.
30
     *
31
     * @since 0.1.2
32
     *
33
     * @var ConfigInterface
34
     */
35
    protected $config;
36
37
    /**
38
     * Process the passed-in configuration file.
39
     *
40
     * @since 0.1.2
41
     *
42
     * @param ConfigInterface $config The Config to process.
43
     * @param                 string  ... List of keys.
44
     *
45
     * @throws FailedToProcessConfigException If the arguments could not be parsed into a Config.
46
     */
47 4
    protected function processConfig(ConfigInterface $config)
48
    {
49 4
        if (func_num_args() > 1) {
50
            try {
51 2
                $keys = func_get_args();
52 2
                array_shift($keys);
53 2
                $config = $config->getSubConfig($keys);
0 ignored issues
show
Documentation introduced by
$keys is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54 1
            } catch (Exception $exception) {
55 1
                throw new FailedToProcessConfigException(
56
                    sprintf(
57 1
                        _('Could not process the config with the arguments "%1$s".'),
58 1
                        print_r(func_get_args(), true)
59
                    )
60
                );
61
            }
62
        }
63 3
        $this->config = $config;
64 3
    }
65
66
    /**
67
     * Check whether the Config has a specific key.
68
     *
69
     * To get a value several levels deep, add the keys for each level as a comma-separated list.
70
     *
71
     * @since 0.1.2
72
     * @since 0.1.5 Accepts list of keys.
73
     *
74
     * @param string|array $_ List of keys.
75
     *
76
     * @return bool Whether the key is known.
77
     */
78 2
    protected function hasConfigKey($_)
79
    {
80 2
        $keys = func_get_args();
81
82 2
        return $this->config->hasKey($keys);
83
    }
84
85
    /**
86
     * Get the Config value for a specific key.
87
     *
88
     * To get a value several levels deep, add the keys for each level as a comma-separated list.
89
     *
90
     * @since 0.1.2
91
     * @since 0.1.5 Accepts list of keys.
92
     *
93
     * @param string|array $_ List of keys.
94
     *
95
     * @return mixed Value of the key.
96
     */
97 2
    protected function getConfigKey($_)
98
    {
99 2
        $keys = func_get_args();
100
101 2
        return $this->config->getKey($keys);
102
    }
103
104
    /**
105
     * Get the callable Config value for a specific key.
106
     *
107
     * If the fetched value is indeed a callable, it will be executed with the provided arguments, and the resultant
108
     * value will be returned instead.
109
     *
110
     * @since 0.4.8
111
     *
112
     * @param string|array $key  Key or array of nested keys.
113
     * @param array        $args Optional. Array of arguments to pass to the callable.
114
     *
115
     * @return mixed Resultant value of the key's callable.
116
     */
117 1
    protected function getConfigCallable($key, array $args = [])
118
    {
119 1
        $value = $this->config->getKey($key);
120
121 1
        if (is_callable($value)) {
122 1
            $value = $value(...$args);
123
        }
124
125 1
        return $value;
126
    }
127
128
    /**
129
     * Get a (multi-dimensional) array of all the configuration settings.
130
     *
131
     * @since 0.1.4
132
     *
133
     * @return array All the configuration settings.
134
     */
135 1
    protected function getConfigArray()
136
    {
137 1
        return $this->config->getAll();
138
    }
139
140
    /**
141
     * Get an array of all the keys that are known by the Config.
142
     *
143
     * @since 0.1.2
144
     *
145
     * @return array Array of strings containing all the keys.
146
     */
147 1
    protected function getConfigKeys()
148
    {
149 1
        return $this->config->getKeys();
150
    }
151
152
    /**
153
     * Get a default configuration in case none was injected into the constructor.
154
     *
155
     * The name and path of the configuration needs to be set as a const called DEFAULT_CONFIG within the class
156
     * containing the trait. The path needs to be relative to the location of the containing class file.
157
     *
158
     * @since 0.4.2
159
     *
160
     * @return ConfigInterface Configuration settings to use.
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...
161
     */
162 1
    protected function fetchDefaultConfig()
163
    {
164 1
        $configFile = method_exists($this, 'getDefaultConfigFile')
165 1
            ? $this->getDefaultConfigFile()
0 ignored issues
show
Bug introduced by
It seems like getDefaultConfigFile() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
166 1
            : __DIR__ . '/../config/defaults.php';
167
168 1
        return $this->fetchConfig($configFile);
169
    }
170
171
    /**
172
     * Get a configuration from a specified $file.
173
     *
174
     * If file is not accessible or readable, returns an empty Config.
175
     *
176
     * @since 0.4.2
177
     *
178
     * @return ConfigInterface Configuration settings to use.
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...
179
     */
180 2
    protected function fetchConfig($configFile)
181
    {
182 2
        if (is_string($configFile) && ! is_readable($configFile)) {
183 1
            $configFile = [];
184
        }
185
186 2
        return ConfigFactory::create($configFile);
187
    }
188
}
189