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

StackProcessor::clearCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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