Completed
Push — master ( 753c96...c3156c )
by Sébastien
01:47
created

Pipeline::setNext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Bdf\Pipeline;
4
5
use Bdf\Pipeline\Processor\StackProcessor;
6
7
/**
8
 * Pipeline
9
 *
10
 * @author Sébastien Tanneux
11
 */
12
final class Pipeline
13
{
14
    /**
15
     * The pipes processor
16
     *
17
     * @var ProcessorInterface
18
     */
19
    private $processor;
20
21
    /**
22
     * The pipes
23
     *
24
     * @var callable[]
25
     */
26
    private $pipes;
27
28
    /**
29
     * The destination of the last pipe
30
     *
31
     * @var callable
32
     */
33
    private $outlet;
34
35
    /**
36
     * Pipeline constructor.
37
     *
38
     * @param array $pipes
39
     * @param ProcessorInterface $processor
40
     */
41
    public function __construct(array $pipes = [], ProcessorInterface $processor = null)
42
    {
43
        $this->processor = $processor ?: new StackProcessor();
44
        $this->pipes = $pipes;
45
    }
46
47
    /**
48
     * Add a pipe at the beginning of the chain
49
     *
50
     * @param callable $first
51
     */
52
    public function prepend($first)
53
    {
54
        array_unshift($this->pipes, $first);
55
    }
56
57
    /**
58
     * Add a pipe
59
     *
60
     * @param callable $last
61
     */
62
    public function pipe($last)
63
    {
64
        $this->pipes[] = $last;
65
    }
66
67
    /**
68
     * Set the pipeline outlet
69
     *
70
     * @param callable $outlet
71
     */
72
    public function outlet(callable $outlet)
73
    {
74
        $this->outlet = $outlet;
75
    }
76
77
    /**
78
     * Send the payload into the pipeline.
79
     *
80
     * @param array $payload
81
     *
82
     * @return mixed
83
     */
84
    public function send(...$payload)
85
    {
86
        return $this->processor->process($this->pipes, $payload, $this->outlet);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function __invoke(...$payload)
93
    {
94
        // TODO works only with PipeProcessor
95
        return $this->send(...$payload);
96
    }
97
98
    /**
99
     * Clone the processor and clear its cache
100
     */
101
    public function __clone()
102
    {
103
        $this->processor = clone $this->processor;
104
        $this->processor->clearCache();
105
    }
106
}
107