Passed
Push — master ( 4598b7...aecae1 )
by Sébastien
01:43
created

StackProcessor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 56
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A clearCache() 0 3 1
A process() 0 5 1
A getCallable() 0 21 4
1
<?php
2
3
namespace Bdf\Pipeline\Processor;
4
5
use Bdf\Pipeline\ProcessorInterface;
6
7
/**
8
 * StackProcessor
9
 *
10
 * @author Sébastien Tanneux
11
 */
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()
66
    {
67
        $this->callable = null;
68
    }
69
}
70