Total Complexity | 6 |
Total Lines | 56 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
12 | class StackProcessor implements ProcessorInterface |
||
13 | { |
||
14 | /** |
||
15 | * The pipes callable |
||
16 | * |
||
17 | * @var callable |
||
18 | */ |
||
19 | private $callable; |
||
20 | |||
21 | /** |
||
22 | * {@inheritdoc} |
||
23 | */ |
||
24 | public function process(array $pipes, array $payload, callable $outlet = null) |
||
25 | { |
||
26 | $callable = $this->getCallable($pipes, $outlet); |
||
27 | |||
28 | return $callable(...$payload); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Build the chain callable |
||
33 | * |
||
34 | * @param array $pipes |
||
35 | * @param callable|null $outlet |
||
36 | * |
||
37 | * @return \Closure |
||
38 | */ |
||
39 | private function getCallable($pipes, $outlet) |
||
40 | { |
||
41 | if ($this->callable !== null) { |
||
42 | return $this->callable; |
||
43 | } |
||
44 | |||
45 | if ($outlet !== null) { |
||
46 | $callable = $outlet; |
||
47 | } else { |
||
48 | $callable = function ($payload) { |
||
49 | return $payload; |
||
50 | }; |
||
51 | }; |
||
52 | |||
53 | while ($pipe = array_pop($pipes)) { |
||
54 | $callable = function (...$payload) use ($pipe, $callable) { |
||
55 | return $pipe($callable, ...$payload); |
||
56 | }; |
||
57 | } |
||
58 | |||
59 | return $callable; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function clearCache() |
||
68 | } |
||
69 | } |
||
70 |