Pipe::process()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 1
nop 2
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Zurbaev\PipelineTasks;
4
5
use Illuminate\Contracts\Container\Container;
6
use Zurbaev\PipelineTasks\Exceptions\PipelineTaskFailedException;
7
8
abstract class Pipe
9
{
10
    /**
11
     * Task that is currently processing.
12
     *
13
     * @var Task
14
     */
15
    protected $task;
16
17
    /**
18
     * Get the pipe name.
19
     *
20
     * @return string
21
     */
22
    public function name()
23
    {
24
        return basename(
25
            str_replace('\\', '/', static::class)
26
        );
27
    }
28
29
    /**
30
     * Call pipe handler and detect any errors.
31
     *
32
     * @param array    $payload
33
     * @param \Closure $next
34
     *
35
     * @throws PipelineTaskFailedException
36
     *
37
     * @return mixed
38
     */
39
    public function process(array $payload, \Closure $next)
40
    {
41
        /**
42
         * @var Container $container
43
         * @var Task      $task
44
         */
45
        list($container, $task) = $payload;
46
47
        $this->task = $task;
48
49
        try {
50
            $result = $container->call([$this, 'handle']);
51
        } catch (\Exception $e) {
52
            throw PipelineTaskFailedException::exceptionThrown($this->name(), $e);
53
        }
54
55
        if ($result === false) {
56
            throw PipelineTaskFailedException::rejected($this->name());
57
        }
58
59
        return $next(
60
            [$container, $task->setPipeResult($this->name(), $result)]
61
        );
62
    }
63
}
64