Task::setPipeResult()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Zurbaev\PipelineTasks;
4
5
use Zurbaev\PipelineTasks\Exceptions\PipelineTaskFailedException;
6
7
abstract class Task
8
{
9
    /**
10
     * Pipe handlers results for current task.
11
     *
12
     * @var array
13
     */
14
    protected $results = [];
15
16
    /**
17
     * Method name to be executed on each pipe.
18
     *
19
     * @var string
20
     */
21
    protected $pipeMethod = 'process';
22
23
    /**
24
     * Get the pipes list.
25
     *
26
     * @return array
27
     */
28
    abstract public function pipes();
29
30
    /**
31
     * Completed task handler.
32
     *
33
     * @return mixed
34
     */
35
    public function completed()
36
    {
37
        return true;
38
    }
39
40
    /**
41
     * Failed task handler.
42
     *
43
     * @param PipelineTaskFailedException $exception
44
     *
45
     * @return mixed
46
     */
47
    public function failed(PipelineTaskFailedException $exception)
48
    {
49
        return false;
50
    }
51
52
    /**
53
     * Get the pipe method.
54
     *
55
     * @return string
56
     */
57
    public function pipeMethod()
58
    {
59
        return $this->pipeMethod;
60
    }
61
62
    /**
63
     * Store pipe result.
64
     *
65
     * @param string $name
66
     * @param mixed  $result
67
     *
68
     * @return $this
69
     */
70
    public function setPipeResult(string $name, $result)
71
    {
72
        $this->results[$name] = $result;
73
74
        return $this;
75
    }
76
77
    /**
78
     * Determines if result for the given pipe already exists.
79
     *
80
     * @param string $name
81
     *
82
     * @return bool
83
     */
84
    public function hasPipeResult(string $name)
85
    {
86
        return isset($this->results[$name]);
87
    }
88
89
    /**
90
     * Get pipe result.
91
     *
92
     * @param string $name
93
     * @param mixed  $default = null
94
     *
95
     * @return mixed
96
     */
97
    public function getPipeResult(string $name, $default = null)
98
    {
99
        return $this->hasPipeResult($name) ? $this->results[$name] : $default;
100
    }
101
102
    /**
103
     * Get all available results.
104
     *
105
     * @return array
106
     */
107
    public function results()
108
    {
109
        return $this->results;
110
    }
111
}
112