Passed
Push — master ( 215600...914e6e )
by Alain
02:30
created

EngineFinder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 81.82%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 11
c 4
b 0
f 1
lcom 1
cbo 3
dl 0
loc 84
ccs 18
cts 22
cp 0.8182
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 14 4
A initializeEngines() 0 6 2
A initializeEngine() 0 21 4
A getFindablesConfigKey() 0 4 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\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 EngineInterface Result of the search.
1 ignored issue
show
Documentation introduced by
Should the return type not be \BrightNucleus\View\Supp...View\Support\NullObject?

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