ComposedReducer::addReducer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Ctefan\Redux\Reducer;
5
6
use Ctefan\Redux\Action\ActionInterface;
7
8
/**
9
 * Each reducer of the ComposedReducer reduces the full state.
10
 */
11
class ComposedReducer
12
{
13
    /**
14
     * @var array<callable>
15
     */
16
    protected $reducers = [];
17
18
    /**
19
     * ComposedReducer constructor.
20
     *
21
     * @param array<callable> $reducers
22
     */
23 1
    public function __construct(array $reducers)
24
    {
25 1
        foreach ($reducers as $reducer) {
26 1
            $this->addReducer($reducer);
27
        }
28 1
    }
29
30
    /**
31
     * @param mixed $state
32
     * @param ActionInterface $action
33
     * @return mixed
34
     */
35 1
    public function __invoke($state, ActionInterface $action)
36
    {
37 1
        foreach ($this->getReducers() as $reducer) {
38 1
            $state = $reducer($state, $action);
39
        }
40 1
        return $state;
41
    }
42
43
    /**
44
     * @param callable $reducer
45
     */
46 1
    protected function addReducer(callable $reducer): void
47
    {
48 1
        $this->reducers[] = $reducer;
49 1
    }
50
51
    /**
52
     * @return array<callable>
53
     */
54 1
    protected function getReducers(): array
55
    {
56 1
        return $this->reducers;
57
    }
58
}