Completed
Pull Request — master (#2)
by Evgenii
01:19
created

IndicatorManager::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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