Passed
Push — main ( 8a9455...e00516 )
by Michael
03:45
created

Pipeline::handleException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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