Passed
Push — master ( 6d9d43...921f98 )
by Alain
02:46
created

AbstractFinder::initializeFindables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Bright Nucleus View Component.
4
 *
5
 * @package   BrightNucleus\View
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\View\Support;
13
14
use BrightNucleus\Config\ConfigInterface;
15
use BrightNucleus\Config\ConfigTrait;
16
use BrightNucleus\Config\Exception\FailedToProcessConfigException;
17
use BrightNucleus\View\Exception\FailedToInstantiateFindableException;
18
19
/**
20
 * Abstract class AbstractFinder.
21
 *
22
 * @since   0.1.0
23
 *
24
 * @package BrightNucleus\View\Support
25
 * @author  Alain Schlesser <[email protected]>
26
 */
27
abstract class AbstractFinder implements FinderInterface
28
{
29
30
    use ConfigTrait;
31
32
    /**
33
     * Findable collection that the Finder can iterate through to find a match.
34
     *
35
     * @since 0.1.0
36
     *
37
     * @var FindableCollection
38
     */
39
    protected $findables;
40
41
    /**
42
     * NullObject that is returned if the Finder could not find a match.
43
     *
44
     * @since 0.1.0
45
     *
46
     * @var NullObject
47
     */
48
    protected $nullObject;
49
50
    /**
51
     * Instantiate an AbstractFinder object.
52
     *
53
     * @since 0.1.0
54
     *
55
     * @param ConfigInterface $config Configuration of the AbstractFinder.
56
     *
57
     * @throws FailedToProcessConfigException If the config could not be processed.
58
     */
59 8
    public function __construct(ConfigInterface $config)
60
    {
61 8
        $this->processConfig($config);
62 8
        $this->findables = new FindableCollection();
63 8
        $this->registerFindables($this->config);
64 8
        $this->registerNullObject($this->config);
65 8
    }
66
67
    /**
68
     * Register the Findables defined in the given configuration.
69
     *
70
     * @since 0.1.0
71
     *
72
     * @param ConfigInterface $config Configuration to register the Findables from.
73
     */
74 8
    public function registerFindables(ConfigInterface $config)
75
    {
76 8
        foreach ($config->getKey($this->getFindablesConfigKey()) as $findableKey => $findableObject) {
77 8
            $this->findables->set($findableKey, $findableObject);
78
        }
79 8
    }
80
81
    /**
82
     * Register the NullObject defined in the given configuration.
83
     *
84
     * @since 0.1.0
85
     *
86
     * @param ConfigInterface $config Configuration to register the NullObject from.
87
     */
88 8
    public function registerNullObject(ConfigInterface $config)
89
    {
90 8
        $this->nullObject = $config->getKey($this->getNullObjectConfigKey());
91 8
    }
92
93
    /**
94
     * Get the NullObject.
95
     *
96
     * @since 0.1.1
97
     *
98
     * @return NullObject NullObject for the current Finder.
99
     */
100 1
    public function getNullObject()
101
    {
102 1
        $this->initializeNullObject();
103
104 1
        return $this->nullObject;
105
    }
106
107
    /**
108
     * Get the config key for the Findables definitions.
109
     *
110
     * @since 0.1.0
111
     *
112
     * @return string Config key use to define the Findables.
113
     */
114
    protected function getFindablesConfigKey()
115
    {
116
        return 'Findables';
117
    }
118
119
    /**
120
     * Get the config key for the NullObject definitions.
121
     *
122
     * @since 0.1.0
123
     *
124
     * @return string Config key use to define the NullObject.
125
     */
126 8
    protected function getNullObjectConfigKey()
127
    {
128 8
        return 'NullObject';
129
    }
130
131
    /**
132
     * Initialize the NullObject.
133
     *
134
     * @param mixed $arguments Optional. Arguments to use.
135
     *
136
     * @since 0.1.1
137
     */
138 1
    protected function initializeNullObject($arguments = null)
139
    {
140 1
        if (! is_object($this->nullObject)) {
141 1
            $this->nullObject = new $this->nullObject(...$arguments);
142
        }
143 1
    }
144
145
    /**
146
     * Initialize the Findables that can be iterated.
147
     *
148
     * @param mixed $arguments Optional. Arguments to use.
149
     *
150
     * @since 0.1.0
151
     *
152
     */
153
    protected function initializeFindables($arguments = null)
154
    {
155 18
        $this->findables = $this->findables->map(function ($findable) use ($arguments) {
156 18
            return $this->initializeFindable($findable, $arguments);
157 18
        });
158 18
    }
159
160
    /**
161
     * Initialize a single findable by instantiating class name strings and calling closures.
162
     *
163
     * @since 0.1.0
164
     *
165
     * @param mixed $findable  Findable to instantiate.
166
     * @param mixed $arguments Optional. Arguments to use.
167
     *
168
     * @return Findable Instantiated findable.
169
     * @throws FailedToInstantiateFindableException If the findable could not be instantiated.
170
     */
171 18
    protected function initializeFindable($findable, $arguments = null)
172
    {
173 18
        $findable = $this->maybeInstantiateFindable($findable, $arguments);
174
175 18
        if (! $findable instanceof Findable) {
176
            throw new FailedToInstantiateFindableException(
177
                sprintf(
178
                    _('Could not instantiate Findable "%s".'),
179
                    serialize($findable)
180
                ),
181
                (array)$arguments
182
            );
183
        }
184
185 18
        return $findable;
186
    }
187
188
    /**
189
     * Maybe instantiate a Findable if it is not yet an object.
190
     *
191
     * @since 0.1.1
192
     *
193
     * @param mixed $findable  Findable to instantiate.
194
     * @param mixed $arguments Optional. Arguments to use.
195
     *
196
     * @return Findable Instantiated findable.
197
     */
198 18
    protected function maybeInstantiateFindable($findable, $arguments = null)
199
    {
200 18
        if (is_string($findable)) {
201 8
            $findable = $this->instantiateFindableFromString($findable, $arguments);
202
        }
203
204 18
        if (is_callable($findable)) {
205
            $findable = $this->instantiateFindableFromCallable($findable, $arguments);
206
        }
207
208 18
        return $findable;
209
    }
210
211
    /**
212
     * Instantiate a Findable from a string.
213
     *
214
     * @since 0.1.1
215
     *
216
     * @param string $string    String to use for instantiation.
217
     * @param mixed  $arguments Optional. Arguments to use for instantiation.
218
     *
219
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use object.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
220
     */
221 8
    protected function instantiateFindableFromString($string, $arguments = null)
222
    {
223 8
        return new $string(...$arguments);
224
    }
225
226
    /**
227
     * Instantiate a Findable from a callable.
228
     *
229
     * @since 0.1.1
230
     *
231
     * @param callable $callable  Callable to use for instantiation.
232
     * @param mixed    $arguments Optional. Arguments to use for instantiation.
233
     *
234
     * @return mixed
235
     */
236
    protected function instantiateFindableFromCallable($callable, $arguments = null)
237
    {
238
        return $callable(...$arguments);
239
    }
240
}
241