Total Complexity | 8 |
Total Lines | 93 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
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() |
||
105 | } |
||
106 | } |
||
107 |