Total Complexity | 11 |
Total Lines | 143 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
12 | final class Pipeline |
||
13 | { |
||
14 | /** |
||
15 | * The callable factory |
||
16 | * |
||
17 | * @var CallableFactoryInterface |
||
18 | */ |
||
19 | private $factory; |
||
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 | * The built callable |
||
37 | * |
||
38 | * @var callable |
||
39 | */ |
||
40 | private $callable; |
||
41 | |||
42 | /** |
||
43 | * Pipeline constructor. |
||
44 | * |
||
45 | * @param CallableFactoryInterface $factory |
||
46 | */ |
||
47 | public function __construct(CallableFactoryInterface $factory = null) |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Set the pipes |
||
54 | * |
||
55 | * @param callable[] $pipes |
||
56 | * |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function setPipes(array $pipes) |
||
60 | { |
||
61 | $this->clear(); |
||
62 | $this->pipes = $pipes; |
||
63 | |||
64 | return $this; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Add a pipe at the beginning of the chain |
||
69 | * |
||
70 | * @param callable $first |
||
71 | * |
||
72 | * @return $this |
||
73 | */ |
||
74 | public function prepend($first) |
||
75 | { |
||
76 | $this->clear(); |
||
77 | array_unshift($this->pipes, $first); |
||
78 | |||
79 | return $this; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Add a pipe |
||
84 | * |
||
85 | * @param callable $last |
||
86 | * |
||
87 | * @return $this |
||
88 | */ |
||
89 | public function pipe($last) |
||
90 | { |
||
91 | $this->clear(); |
||
92 | $this->pipes[] = $last; |
||
93 | |||
94 | return $this; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Set the pipeline outlet |
||
99 | * |
||
100 | * @param callable $outlet |
||
101 | * |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function outlet(callable $outlet) |
||
105 | { |
||
106 | $this->clear(); |
||
107 | $this->outlet = $outlet; |
||
108 | |||
109 | return $this; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Send the payload into the pipeline. |
||
114 | * |
||
115 | * @param array $payload |
||
116 | * |
||
117 | * @return mixed |
||
118 | */ |
||
119 | public function send(...$payload) |
||
120 | { |
||
121 | if ($this->callable === null) { |
||
122 | $this->callable = $this->factory->createCallable($this->pipes, $this->outlet); |
||
123 | } |
||
124 | |||
125 | $callable = $this->callable; |
||
126 | return $callable(...$payload); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Pipeline invokation |
||
131 | * |
||
132 | * @param array $payload |
||
133 | * |
||
134 | * @return mixed |
||
135 | */ |
||
136 | public function __invoke(...$payload) |
||
137 | { |
||
138 | return $this->factory->callPipeline($this, $payload); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Clear the callable |
||
143 | */ |
||
144 | public function __clone() |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Clear the callable |
||
151 | */ |
||
152 | private function clear() |
||
153 | { |
||
157 |