1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* See class comment |
4
|
|
|
* |
5
|
|
|
* PHP Version 5 |
6
|
|
|
* |
7
|
|
|
* @category Netresearch |
8
|
|
|
* @package Netresearch\Kite |
9
|
|
|
* @subpackage Task |
10
|
|
|
* @author Christian Opitz <[email protected]> |
11
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
12
|
|
|
* @link http://www.netresearch.de |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Netresearch\Kite\Task; |
16
|
|
|
use Netresearch\Kite\Exception; |
17
|
|
|
use Netresearch\Kite\Task; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Catch exceptions while executing tasks |
22
|
|
|
* |
23
|
|
|
* @category Netresearch |
24
|
|
|
* @package Netresearch\Kite |
25
|
|
|
* @subpackage Task |
26
|
|
|
* @author Christian Opitz <[email protected]> |
27
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
28
|
|
|
* @link http://www.netresearch.de |
29
|
|
|
*/ |
30
|
|
|
class TryCatchTask extends SubTask |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var \Netresearch\Kite\Task |
34
|
|
|
*/ |
35
|
|
|
protected $catchTask; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var bool If the next fetched task should be the catch task |
39
|
|
|
*/ |
40
|
|
|
protected $catch = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Configure the options |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
protected function configureVariables() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
return array( |
50
|
|
|
'onCatch' => array( |
51
|
|
|
'type' => 'array', |
52
|
|
|
'label' => 'Task to execute when an exception was catched' |
53
|
|
|
), |
54
|
|
|
'errorMessage' => array( |
55
|
|
|
'type' => 'string', |
56
|
|
|
'label' => 'Message to display on error' |
57
|
|
|
), |
58
|
|
|
'--' |
59
|
|
|
) + parent::configureVariables(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set a variable or it's value |
64
|
|
|
* |
65
|
|
|
* @param string $name The name |
66
|
|
|
* @param mixed $value The value |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function offsetSet($name, $value) |
71
|
|
|
{ |
72
|
|
|
if ($name === 'onCatch') { |
73
|
|
|
$this->catchTask = $this->factory->createTask($value, $this); |
|
|
|
|
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
parent::offsetSet($name, $value); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set task as catchTask if $this->catch |
82
|
|
|
* |
83
|
|
|
* @param \Netresearch\Kite\Task $task The task |
84
|
|
|
* |
85
|
|
|
* @return $this|mixed $this or the task return value when this is running |
86
|
|
|
*/ |
87
|
|
|
public function addTask(Task $task) |
88
|
|
|
{ |
89
|
|
|
if ($this->catch) { |
90
|
|
|
$this->catchTask = $task; |
91
|
|
|
$this->catch = false; |
92
|
|
|
return $task; |
93
|
|
|
} |
94
|
|
|
return parent::addTask($task); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Fetch the next task as that task to execute when an exception occured while |
99
|
|
|
* executing the other tasks |
100
|
|
|
* |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
|
|
public function onCatch() |
104
|
|
|
{ |
105
|
|
|
if ($this->catchTask) { |
106
|
|
|
throw new Exception('Only one task may be executed on catch'); |
107
|
|
|
} |
108
|
|
|
$this->catch = true; |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Run an array of tasks |
114
|
|
|
* |
115
|
|
|
* @return mixed The ran tasks return value |
116
|
|
|
*/ |
117
|
|
|
public function run() |
118
|
|
|
{ |
119
|
|
|
try { |
120
|
|
|
return parent::run(); |
121
|
|
|
} catch (\Exception $e) { |
122
|
|
|
$message = $this->get('errorMessage'); |
123
|
|
|
if ($message) { |
124
|
|
|
$this->console->output($message); |
125
|
|
|
} |
126
|
|
|
if ($this->catchTask) { |
127
|
|
|
return $this->addTask($this->catchTask); |
128
|
|
|
} |
129
|
|
|
return null; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
?> |
134
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.