Total Complexity | 3 |
Total Lines | 52 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 0 |
1 | <?php |
||
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 |
||
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 | } |