CombinedReducer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 50
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 10 3
A __construct() 0 4 2
A getReducers() 0 3 1
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 CombinedReducer reduces another part of the state.
10
 *
11
 * Which part is defined by its key in the reducer array.
12
 */
13
class CombinedReducer
14
{
15
    /**
16
     * @var array<string,callable>
17
     */
18
    protected $reducers = [];
19
20
    /**
21
     * CombinedReducer constructor.
22
     *
23
     * @param array<string,callable> $reducers
24
     */
25 1
    public function __construct(array $reducers)
26
    {
27 1
        foreach ($reducers as $key => $reducer) {
28 1
            $this->addReducer($key, $reducer);
29
        }
30 1
    }
31
32
    /**
33
     * @param array $state
34
     * @param ActionInterface $action
35
     * @return array
36
     */
37 1
    public function __invoke(array $state, ActionInterface $action): array
38
    {
39 1
        foreach ($this->getReducers() as $key => $reducer) {
40 1
            $state[$key] = call_user_func(
41 1
                $reducer,
42 1
                isset($state[$key]) ? $state[$key] : null,
43 1
                $action
44
            );
45
        }
46 1
        return $state;
47
    }
48
49
    /**
50
     * @param callable $reducer
51
     */
52 1
    protected function addReducer(string $key, callable $reducer): void
53
    {
54 1
        $this->reducers[$key] = $reducer;
55 1
    }
56
57
    /**
58
     * @return array<string,callable>
59
     */
60 1
    protected function getReducers(): array
61
    {
62 1
        return $this->reducers;
63
    }
64
}