Completed
Push — master ( 06e05d...762814 )
by Evgenii
9s
created

IndicatorManager::extend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Laratrade\Indicators;
4
5
use Closure;
6
use Laratrade\Indicators\Contracts\Indicator;
7
use Laratrade\Indicators\Contracts\IndicatorManager as IndicatorManagerContract;
8
use Laratrade\Indicators\Exceptions\IndicatorNotFoundException;
9
10
class IndicatorManager implements IndicatorManagerContract
11
{
12
    /**
13
     * The indicators collection.
14
     *
15
     * @var array
16
     */
17
    protected $indicators = [];
18
19
    /**
20
     * Add an indicator resolver.
21
     *
22
     * @param string  $indicator
23
     * @param Closure $resolver
24
     */
25 10
    public function extend(string $indicator, Closure $resolver)
26
    {
27 10
        $this->indicators[$indicator] = $resolver;
28 10
    }
29
30
    /**
31
     * Resolve an indicator.
32
     *
33
     * @param string $indicator
34
     *
35
     * @return Indicator
36
     */
37 8
    public function resolve(string $indicator)
38
    {
39 8
        if (!isset($this->indicators[$indicator])) {
40 2
            throw new IndicatorNotFoundException;
41
        }
42
43 6
        return call_user_func($this->indicators[$indicator]);
44
    }
45
46
    /**
47
     * Dynamically handle the indicator calls.
48
     *
49
     * @param string $indicator
50
     * @param array  $parameters
51
     *
52
     * @return int
53
     */
54
    public function __call(string $indicator, array $parameters): int
55
    {
56
        $function = $this->resolve($indicator);
57
58
        return $function($parameters[0]);
59
    }
60
}
61