Passed
Push — master ( 497e2b...a46ae4 )
by Alain
02:30
created

EngineFinder::find()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
rs 9.2
cc 4
eloc 7
nc 4
nop 1
crap 4.0466
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\Engine;
13
14
use BrightNucleus\View\Exception\FailedToInstantiateEngineException;
15
use BrightNucleus\View\Support\AbstractFinder;
16
17
/**
18
 * Class EngineFinder.
19
 *
20
 * @since   0.1.0
21
 *
22
 * @package BrightNucleus\View\Engine
23
 * @author  Alain Schlesser <[email protected]>
24
 */
25
class EngineFinder extends AbstractFinder
26
{
27
28
    /**
29
     * Find a result based on a specific criteria.
30
     *
31
     * @since 0.1.0
32
     *
33
     * @param array $criteria Criteria to search for.
34
     *
35
     * @return mixed Result of the search.
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \BrightNucleus\View\Supp...View\Support\NullObject.

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...
36
     */
37 17
    public function find(array $criteria)
38
    {
39 17
        $this->initializeEngines();
40
41 17
        foreach ($criteria as $entry) {
42 17
            foreach ($this->findables as $engine) {
43 17
                if ($engine->canHandle($entry)) {
44 17
                    return $engine;
45
                }
46
            }
47
        }
48
49
        return $this->nullObject;
50
    }
51
52
    /**
53
     * Initialize the engines that can be iterated.
54
     *
55
     * @since 0.1.0
56
     *
57
     */
58 17 View Code Duplication
    protected function initializeEngines()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60 17
        foreach ($this->findables as &$engine) {
61 17
            $engine = $this->initializeEngine($engine);
62
        }
63
64 17
        if (! is_object($this->nullObject)) {
65 7
            $this->nullObject = new $this->nullObject();
66
        }
67 17
    }
68
69
    /**
70
     * Initialize a single engine by instantiating class name strings and calling closures.
71
     *
72
     * @since 0.1.0
73
     *
74
     * @param mixed $engine Engine to instantiate.
75
     *
76
     * @return EngineInterface Instantiated engine.
77
     * @throws FailedToInstantiateEngineException If the engine could not be instantiated.
78
     */
79 17
    protected function initializeEngine($engine)
80
    {
81 17
        if (is_string($engine)) {
82 7
            $engine = new $engine();
83
        }
84
85 17
        if (is_callable($engine)) {
86
            $engine = $engine();
87
        }
88
89 17
        if (! $engine instanceof EngineInterface) {
90
            throw new FailedToInstantiateEngineException(
91
                sprintf(
92
                    _('Could not instantiate engine "%s".'),
93
                    serialize($engine)
94
                )
95
            );
96
        }
97
98 17
        return $engine;
99
    }
100
101
    /**
102
     * Get the config key for the Findables definitions.
103
     *
104
     * @since 0.1.0
105
     *
106
     * @return string Config key use to define the Findables.
107
     */
108 7
    protected function getFindablesConfigKey()
109
    {
110 7
        return 'Engines';
111
    }
112
}
113