AbstractMiddleware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 52
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatch() 0 3 1
A __invoke() 0 8 1
A getState() 0 3 1
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
}