IndicatorManager   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
dl 0
loc 51
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1
ccs 7
cts 10
cp 0.7
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extend() 0 4 1
A resolve() 0 8 2
A __call() 0 6 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 8
    public function extend(string $indicator, Closure $resolver)
26
    {
27 8
        $this->indicators[$indicator] = $resolver;
28 8
    }
29
30
    /**
31
     * Resolve an indicator.
32
     *
33
     * @param string $indicator
34
     *
35
     * @return Indicator
36
     */
37 6
    public function resolve(string $indicator)
38
    {
39 6
        if (!isset($this->indicators[$indicator])) {
40 2
            throw new IndicatorNotFoundException;
41
        }
42
43 4
        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