1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Flipbox Factory |
5
|
|
|
* @copyright Copyright (c) 2017, Flipbox Digital |
6
|
|
|
* @link https://github.com/flipbox/queue/releases/latest |
7
|
|
|
* @license https://github.com/flipbox/queue/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace flipbox\queue\jobs; |
11
|
|
|
|
12
|
|
|
use flipbox\queue\events\AfterJobRun; |
13
|
|
|
use flipbox\queue\events\BeforeJobRun; |
14
|
|
|
use yii\base\Component; |
15
|
|
|
use yii\helpers\ArrayHelper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 1.0.0 |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractJob extends Component implements JobInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Event executed before a job is being executed. |
26
|
|
|
*/ |
27
|
|
|
const EVENT_BEFORE_RUN = 'beforeRun'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Event executed after a job is being executed. |
31
|
|
|
*/ |
32
|
|
|
const EVENT_AFTER_RUN = 'afterRun'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The ID of the message. This should be set on the job receive. |
36
|
|
|
* @var integer |
37
|
|
|
*/ |
38
|
|
|
protected $id; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Stores the header. |
42
|
|
|
* This can be different for each queue provider. |
43
|
|
|
* |
44
|
|
|
* @var mixed |
45
|
|
|
*/ |
46
|
|
|
protected $header = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
abstract protected function runInternal(); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
|
|
public function run() |
57
|
|
|
{ |
58
|
|
|
// Before event |
59
|
|
|
if (!$this->beforeRun()) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Run the actual job |
64
|
|
|
$value = $this->runInternal(); |
65
|
|
|
|
66
|
|
|
// After event |
67
|
|
|
$this->afterRun($value); |
68
|
|
|
|
69
|
|
|
return $value; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
75
|
|
|
public function getId() |
76
|
|
|
{ |
77
|
|
|
return $this->id; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritdoc |
82
|
|
|
*/ |
83
|
|
|
public function setId($id) |
84
|
|
|
{ |
85
|
|
|
$this->id = $id; |
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
|
|
public function getHeader($item, $default = null) |
93
|
|
|
{ |
94
|
|
|
return ArrayHelper::getValue($this->header, $item, $default); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
|
|
public function setHeader($item, $value) |
101
|
|
|
{ |
102
|
|
|
$this->header[$item] = $value; |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
protected function beforeRun() |
110
|
|
|
{ |
111
|
|
|
$this->trigger( |
112
|
|
|
self::EVENT_BEFORE_RUN, |
113
|
|
|
$beforeEvent = new BeforeJobRun() |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
return $beforeEvent->isValid; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $value |
121
|
|
|
*/ |
122
|
|
|
protected function afterRun($value) |
123
|
|
|
{ |
124
|
|
|
$this->trigger( |
125
|
|
|
self::EVENT_BEFORE_RUN, |
126
|
|
|
$beforeEvent = new AfterJobRun([ |
127
|
|
|
'value' => $value |
128
|
|
|
]) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function toConfig(): array |
136
|
|
|
{ |
137
|
|
|
return [ |
138
|
|
|
'class' => get_class($this) |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.