AbstractMiddleware::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Ctefan\Redux\Middleware;
5
6
use Ctefan\Redux\Action\ActionInterface;
7
8
abstract class AbstractMiddleware implements MiddlewareInterface
9
{
10
    /**
11
     * @var callable
12
     */
13
    private $getStateFunction;
14
15
    /**
16
     * @var callable
17
     */
18
    private $dispatchFunction;
19
20
    /**
21
     * @param callable $getState
22
     * @param callable $dispatch
23
     * @return callable
24
     */
25
    public function __invoke(callable $getState, callable $dispatch): callable
26
    {
27
        $this->getStateFunction = $getState;
28
        $this->dispatchFunction = $dispatch;
29
30
        return function(callable $next) {
31
            return function(ActionInterface $action) use ($next) {
32
                return $this->handleAction($action, $next);
33
            };
34
        };
35
    }
36
37
    /**
38
     * @return mixed
39
     */
40
    protected function getState()
41
    {
42
        return call_user_func($this->getStateFunction);
43
    }
44
45
    /**
46
     * @param ActionInterface $action
47
     * @return ActionInterface
48
     */
49
    protected function dispatch(ActionInterface $action): ActionInterface
50
    {
51
        return call_user_func($this->dispatchFunction, $action);
52
    }
53
54
    /**
55
     * @param ActionInterface $action
56
     * @param callable $next
57
     * @return ActionInterface
58
     */
59
    abstract protected function handleAction(ActionInterface $action, callable $next): ActionInterface;
60
}