Passed
Push — main ( b79a6e...d9cecd )
by Michael
03:23
created

Pipeline::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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
            throw $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
            try {
195 12
                return $destination($passable);
196
            } catch (Throwable $e) {
197
                return $this->handleException($passable, $e);
198
            }
199
        };
200
    }
201
202
    /**
203
     * Get a Closure that represents a slice of the application onion.
204
     *
205
     * @return \Closure
206
     */
207 20
    protected function carry()
208
    {
209 20
        return function ($stack, $pipe) {
210 20
            return function ($passable) use ($stack, $pipe) {
211
                try {
212 20
                    if (is_callable($pipe)) {
213
                        // If the pipe is a callable, then we will call it directly, but otherwise we
214
                        // will resolve the pipes out of the dependency container and call it with
215
                        // the appropriate method and arguments, returning the results back out.
216 15
                        return $pipe($passable, $stack);
217 11
                    } elseif (! is_object($pipe)) {
218 10
                        [$name, $parameters] = $this->parsePipeString($pipe);
219
220
                        // If the pipe is a string we will parse the string and resolve the class out
221
                        // of the dependency injection container. We can then build a callable and
222
                        // execute the pipe function giving in the parameters that are required.
223 10
                        $pipe = $this->getContainer()->make($name);
224
225 8
                        $parameters = array_merge([$passable, $stack], $parameters);
226
                    } else {
227
                        // If the pipe is already an object we'll just make a callable and pass it to
228
                        // the pipe as-is. There is no need to do any extra parsing and formatting
229
                        // since the object we're given was already a fully instantiated object.
230 1
                        $parameters = [$passable, $stack];
231
                    }
232
233 9
                    $carry = method_exists($pipe, $this->method)
234 8
                                    ? $pipe->{$this->method}(...$parameters)
235 1
                                    : $pipe(...$parameters);
236
237 7
                    return $this->handleCarry($carry);
238 6
                } catch (Throwable $e) {
239 6
                    if ($this->onFailure) {
240 2
                        return ($this->onFailure)($passable, $e, $pipe);
241
                    }
242
243 4
                    return $this->handleException($passable, $e);
244
                }
245
            };
246
        };
247
    }
248
249
    /**
250
     * Parse full pipe string to get name and parameters.
251
     *
252
     * @param  string  $pipe
253
     * @return array
254
     */
255 10
    protected function parsePipeString($pipe)
256
    {
257 10
        [$name, $parameters] = array_pad(explode(':', $pipe, 2), 2, []);
258
259 10
        if (is_string($parameters)) {
260 1
            $parameters = explode(',', $parameters);
261
        }
262
263 10
        return [$name, $parameters];
264
    }
265
266
    /**
267
     * Get the array of configured pipes.
268
     *
269
     * @return array
270
     */
271 20
    protected function pipes()
272
    {
273
        return [
274 20
            ...$this->pipes,
275 20
            function ($passable, $next) {
276 12
                $this->commitTransaction();
277
278 12
                return $next($passable);
279
            },
280
        ];
281
    }
282
283
    /**
284
     * Get the container instance.
285
     *
286
     * @return \Illuminate\Contracts\Container\Container
287
     *
288
     * @throws \RuntimeException
289
     */
290 10
    protected function getContainer()
291
    {
292 10
        if (! $this->container) {
293 1
            throw new RuntimeException('A container instance has not been passed to the Pipeline.');
294
        }
295
296 9
        return $this->container;
297
    }
298
299
    /**
300
     * Set the container instance.
301
     *
302
     * @param  \Illuminate\Contracts\Container\Container  $container
303
     * @return $this
304
     */
305
    public function setContainer(Container $container)
306
    {
307
        $this->container = $container;
308
309
        return $this;
310
    }
311
312
    /**
313
     * Set callback to be executed on failure pipeline.
314
     *
315
     * @param Closure $callback
316
     *
317
     * @return $this
318
     */
319 2
    public function onFailure(Closure $callback)
320
    {
321 2
        $this->onFailure = $callback;
322
323 2
        return $this;
324
    }
325
326
    /**
327
     * Handle the value returned from each pipe before passing it to the next.
328
     *
329
     * @param  mixed  $carry
330
     * @return mixed
331
     */
332 7
    protected function handleCarry($carry)
333
    {
334 7
        return $carry;
335
    }
336
337
    /**
338
     * Handle the given exception.
339
     *
340
     * @param  mixed  $passable
341
     * @param  \Throwable  $e
342
     * @return mixed
343
     *
344
     * @throws \Throwable
345
     */
346 4
    protected function handleException($passable, Throwable $e)
347
    {
348 4
        throw $e;
349
    }
350
351
    /**
352
     * Begin the transaction if enabled.
353
     *
354
     * @return void
355
     */
356 20
    protected function beginTransaction(): void
357
    {
358 20
        if (! $this->useTransaction) {
359 18
            return;
360
        }
361
362 2
        DB::beginTransaction();
363
    }
364
365
    /**
366
     * Commit the transaction if enabled.
367
     *
368
     * @return void
369
     */
370 12
    protected function commitTransaction(): void
371
    {
372 12
        if (! $this->useTransaction) {
373 11
            return;
374
        }
375
376 1
        DB::commit();
377
    }
378
379
    /**
380
     * Rollback the transaction if enabled.
381
     *
382
     * @return void
383
     */
384 4
    protected function rollbackTransaction(): void
385
    {
386 4
        if (! $this->useTransaction) {
387 3
            return;
388
        }
389
390 1
        DB::rollBack();
391
    }
392
}
393