Passed
Push — main ( 1a645f...364049 )
by Michael
03:15
created

Pipeline   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 374
Duplicated Lines 0 %

Test Coverage

Coverage 96.7%

Importance

Changes 7
Bugs 0 Features 2
Metric Value
eloc 81
c 7
b 0
f 2
dl 0
loc 374
ccs 88
cts 91
cp 0.967
rs 9.6
wmc 35

21 Methods

Rating   Name   Duplication   Size   Complexity  
A withTransaction() 0 5 1
A then() 0 16 2
A through() 0 5 2
A make() 0 7 2
A send() 0 5 1
A commitTransaction() 0 7 2
A __construct() 0 3 1
A pipe() 0 5 2
A thenReturn() 0 4 1
A via() 0 5 1
A handleCarry() 0 3 1
A setContainer() 0 5 1
A getContainer() 0 7 2
A onFailure() 0 5 1
A rollbackTransaction() 0 7 2
A pipes() 0 8 1
A handleException() 0 3 1
A beginTransaction() 0 7 2
A carry() 0 37 6
A prepareDestination() 0 4 1
A parsePipeString() 0 9 2
1
<?php
2
3
namespace MichaelRubel\EnhancedPipeline;
4
5
use Closure;
6
use Illuminate\Container\Container as ContainerConcrete;
7
use Illuminate\Contracts\Container\Container;
8
use Illuminate\Contracts\Pipeline\Pipeline as PipelineContract;
9
use Illuminate\Support\Facades\DB;
10
use RuntimeException;
11
use Throwable;
12
13
class Pipeline implements PipelineContract
14
{
15
    /**
16
     * The container implementation.
17
     *
18
     * @var \Illuminate\Contracts\Container\Container|null
19
     */
20
    protected $container;
21
22
    /**
23
     * The object being passed through the pipeline.
24
     *
25
     * @var mixed
26
     */
27
    protected $passable;
28
29
    /**
30
     * The callback to be executed on failure pipeline.
31
     *
32
     * @var Closure|null
33
     */
34
    protected $onFailure;
35
36
    /**
37
     * The array of class pipes.
38
     *
39
     * @var array
40
     */
41
    protected $pipes = [];
42
43
    /**
44
     * The method to call on each pipe.
45
     *
46
     * @var string
47
     */
48
    protected $method = 'handle';
49
50
    /**
51
     * Determines whether pipeline uses transaction.
52
     *
53
     * @var bool
54
     */
55
    protected bool $useTransaction = false;
56
57
    /**
58
     * Create a new class instance.
59
     *
60
     * @param  \Illuminate\Contracts\Container\Container|null  $container
61
     * @return void
62
     */
63 20
    public function __construct(Container $container = null)
64
    {
65 20
        $this->container = $container;
66
    }
67
68
    /**
69
     * Create a new class instance.
70
     *
71
     * @param Container|null $container
72
     *
73
     * @return Pipeline
74
     */
75 6
    public static function make(Container $container = null): Pipeline
76
    {
77 6
        if (! $container) {
78 6
            $container = ContainerConcrete::getInstance();
79
        }
80
81 6
        return new Pipeline($container);
82
    }
83
84
    /**
85
     * Enable transaction in pipeline.
86
     *
87
     * @return Pipeline
88
     */
89 2
    public function withTransaction(): Pipeline
90
    {
91 2
        $this->useTransaction = true;
92
93 2
        return $this;
94
    }
95
96
    /**
97
     * Set the object being sent through the pipeline.
98
     *
99
     * @param  mixed  $passable
100
     * @return $this
101
     */
102 20
    public function send($passable)
103
    {
104 20
        $this->passable = $passable;
105
106 20
        return $this;
107
    }
108
109
    /**
110
     * Set the array of pipes.
111
     *
112
     * @param  array|mixed  $pipes
113
     * @return $this
114
     */
115 20
    public function through($pipes)
116
    {
117 20
        $this->pipes = is_array($pipes) ? $pipes : func_get_args();
118
119 20
        return $this;
120
    }
121
122
    /**
123
     * Push additional pipes onto the pipeline.
124
     *
125
     * @param  array|mixed  $pipes
126
     * @return $this
127
     */
128 1
    public function pipe($pipes)
129
    {
130 1
        array_push($this->pipes, ...(is_array($pipes) ? $pipes : func_get_args()));
131
132 1
        return $this;
133
    }
134
135
    /**
136
     * Set the method to call on the pipes.
137
     *
138
     * @param  string  $method
139
     * @return $this
140
     */
141 1
    public function via($method)
142
    {
143 1
        $this->method = $method;
144
145 1
        return $this;
146
    }
147
148
    /**
149
     * Run the pipeline with a final destination callback.
150
     *
151
     * @param  \Closure  $destination
152
     * @return mixed
153
     */
154 20
    public function then(Closure $destination)
155
    {
156
        try {
157 20
            $this->beginTransaction();
158
159 20
            $pipeline = array_reduce(
160 20
                array_reverse($this->pipes()),
161 20
                $this->carry(),
162 20
                $this->prepareDestination($destination)
163
            );
164
165 20
            return $pipeline($this->passable);
166 4
        } catch (Throwable $e) {
167 4
            $this->rollbackTransaction();
168
169 4
            return $this->handleException($this->passable, $e);
170
        }
171
    }
172
173
    /**
174
     * Run the pipeline and return the result.
175
     *
176
     * @return mixed
177
     */
178 8
    public function thenReturn()
179
    {
180 8
        return $this->then(function ($passable) {
181 4
            return $passable;
182
        });
183
    }
184
185
    /**
186
     * Get the final piece of the Closure onion.
187
     *
188
     * @param  \Closure  $destination
189
     * @return \Closure
190
     */
191 20
    protected function prepareDestination(Closure $destination)
192
    {
193 20
        return function ($passable) use ($destination) {
194 12
            return $destination($passable);
195
        };
196
    }
197
198
    /**
199
     * Get a Closure that represents a slice of the application onion.
200
     *
201
     * @return \Closure
202
     */
203 20
    protected function carry()
204
    {
205 20
        return function ($stack, $pipe) {
206 20
            return function ($passable) use ($stack, $pipe) {
207
                try {
208 20
                    if (is_callable($pipe)) {
209
                        // If the pipe is a callable, then we will call it directly, but otherwise we
210
                        // will resolve the pipes out of the dependency container and call it with
211
                        // the appropriate method and arguments, returning the results back out.
212 15
                        return $pipe($passable, $stack);
213 11
                    } elseif (! is_object($pipe)) {
214 10
                        [$name, $parameters] = $this->parsePipeString($pipe);
215
216
                        // If the pipe is a string we will parse the string and resolve the class out
217
                        // of the dependency injection container. We can then build a callable and
218
                        // execute the pipe function giving in the parameters that are required.
219 10
                        $pipe = $this->getContainer()->make($name);
220
221 8
                        $parameters = array_merge([$passable, $stack], $parameters);
222
                    } else {
223
                        // If the pipe is already an object we'll just make a callable and pass it to
224
                        // the pipe as-is. There is no need to do any extra parsing and formatting
225
                        // since the object we're given was already a fully instantiated object.
226 1
                        $parameters = [$passable, $stack];
227
                    }
228
229 9
                    $carry = method_exists($pipe, $this->method)
230 8
                                    ? $pipe->{$this->method}(...$parameters)
231 1
                                    : $pipe(...$parameters);
232
233 7
                    return $this->handleCarry($carry);
234 6
                } catch (Throwable $e) {
235 6
                    if ($this->onFailure) {
236 2
                        return ($this->onFailure)($passable, $e, $pipe);
237
                    }
238
239 4
                    return $this->handleException($passable, $e);
240
                }
241
            };
242
        };
243
    }
244
245
    /**
246
     * Parse full pipe string to get name and parameters.
247
     *
248
     * @param  string  $pipe
249
     * @return array
250
     */
251 10
    protected function parsePipeString($pipe)
252
    {
253 10
        [$name, $parameters] = array_pad(explode(':', $pipe, 2), 2, []);
254
255 10
        if (is_string($parameters)) {
256 1
            $parameters = explode(',', $parameters);
257
        }
258
259 10
        return [$name, $parameters];
260
    }
261
262
    /**
263
     * Get the array of configured pipes.
264
     *
265
     * @return array
266
     */
267 20
    protected function pipes()
268
    {
269
        return [
270 20
            ...$this->pipes,
271 20
            function ($passable, $next) {
272 12
                $this->commitTransaction();
273
274 12
                return $next($passable);
275
            },
276
        ];
277
    }
278
279
    /**
280
     * Get the container instance.
281
     *
282
     * @return \Illuminate\Contracts\Container\Container
283
     *
284
     * @throws \RuntimeException
285
     */
286 10
    protected function getContainer()
287
    {
288 10
        if (! $this->container) {
289 1
            throw new RuntimeException('A container instance has not been passed to the Pipeline.');
290
        }
291
292 9
        return $this->container;
293
    }
294
295
    /**
296
     * Set the container instance.
297
     *
298
     * @param  \Illuminate\Contracts\Container\Container  $container
299
     * @return $this
300
     */
301
    public function setContainer(Container $container)
302
    {
303
        $this->container = $container;
304
305
        return $this;
306
    }
307
308
    /**
309
     * Set callback to be executed on failure pipeline.
310
     *
311
     * @param Closure $callback
312
     *
313
     * @return $this
314
     */
315 2
    public function onFailure(Closure $callback)
316
    {
317 2
        $this->onFailure = $callback;
318
319 2
        return $this;
320
    }
321
322
    /**
323
     * Handle the value returned from each pipe before passing it to the next.
324
     *
325
     * @param  mixed  $carry
326
     * @return mixed
327
     */
328 7
    protected function handleCarry($carry)
329
    {
330 7
        return $carry;
331
    }
332
333
    /**
334
     * Handle the given exception.
335
     *
336
     * @param  mixed  $passable
337
     * @param  \Throwable  $e
338
     * @return mixed
339
     *
340
     * @throws \Throwable
341
     */
342 4
    protected function handleException($passable, Throwable $e)
343
    {
344 4
        throw $e;
345
    }
346
347
    /**
348
     * Begin the transaction if enabled.
349
     *
350
     * @return void
351
     */
352 20
    protected function beginTransaction(): void
353
    {
354 20
        if (! $this->useTransaction) {
355 18
            return;
356
        }
357
358 2
        DB::beginTransaction();
359
    }
360
361
    /**
362
     * Commit the transaction if enabled.
363
     *
364
     * @return void
365
     */
366 12
    protected function commitTransaction(): void
367
    {
368 12
        if (! $this->useTransaction) {
369 11
            return;
370
        }
371
372 1
        DB::commit();
373
    }
374
375
    /**
376
     * Rollback the transaction if enabled.
377
     *
378
     * @return void
379
     */
380 4
    protected function rollbackTransaction(): void
381
    {
382 4
        if (! $this->useTransaction) {
383 3
            return;
384
        }
385
386 1
        DB::rollBack();
387
    }
388
}
389