ComposedReducer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 46
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A getReducers() 0 3 1
A __invoke() 0 6 2
A addReducer() 0 3 1
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
}