1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @license MIT |
7
|
|
|
* @copyright 2013 - 2019 Cross Solution <http://cross-solution.de> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
/** */ |
13
|
|
|
namespace Core\Queue\Job; |
14
|
|
|
|
15
|
|
|
use SlmQueue\Worker\Event\ProcessJobEvent; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ${CARET} |
19
|
|
|
* |
20
|
|
|
* @author Mathias Gelhausen <[email protected]> |
21
|
|
|
* @todo write test |
22
|
|
|
*/ |
23
|
|
|
class JobResult |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $result; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $reason; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $extra; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* |
48
|
|
|
* |
49
|
|
|
* @var string|int|\DateInterval |
50
|
|
|
*/ |
51
|
|
|
protected $delay; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* |
55
|
|
|
* |
56
|
|
|
* @var string|int|\DateTime |
57
|
|
|
*/ |
58
|
|
|
protected $scheduled; |
59
|
|
|
|
60
|
|
|
public static function success(?string $reason = null, ?array $extra = null) : self |
61
|
|
|
{ |
62
|
|
|
return (new static(ProcessJobEvent::JOB_STATUS_SUCCESS)) |
63
|
|
|
->withReason($reason) |
64
|
|
|
->withExtra($extra) |
|
|
|
|
65
|
|
|
; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function failure(string $reason, ?array $extra = null) : self |
69
|
|
|
{ |
70
|
|
|
return (new static(ProcessJobEvent::JOB_STATUS_FAILURE)) |
71
|
|
|
->withReason($reason) |
72
|
|
|
->withExtra($extra) |
|
|
|
|
73
|
|
|
; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public static function recoverable(string $reason, array $options = []) : self |
77
|
|
|
{ |
78
|
|
|
$result = (new static(ProcessJobEvent::JOB_STATUS_FAILURE_RECOVERABLE)) |
79
|
|
|
->withReason($reason); |
80
|
|
|
|
81
|
|
|
foreach ($options as $key => $value) { |
82
|
|
|
$callback = [$result, "with$key"]; |
83
|
|
|
if (is_callable($callback)) { |
84
|
|
|
$callback($value); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $result; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function __construct(int $result) |
92
|
|
|
{ |
93
|
|
|
$this->result = $result; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getResult() : int |
97
|
|
|
{ |
98
|
|
|
return $this->result; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function isSuccess() : bool |
102
|
|
|
{ |
103
|
|
|
return ProcessJobEvent::JOB_STATUS_SUCCESS == $this->result; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function isFailure() : bool |
107
|
|
|
{ |
108
|
|
|
return ProcessJobEvent::JOB_STATUS_FAILURE == $this->result; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function isRecoverable() : bool |
112
|
|
|
{ |
113
|
|
|
return ProcessJobEvent::JOB_STATUS_FAILURE_RECOVERABLE == $this->result; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function getReason(): ?string |
120
|
|
|
{ |
121
|
|
|
return $this->reason; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $message |
126
|
|
|
* |
127
|
|
|
* @return self |
128
|
|
|
*/ |
129
|
|
|
public function withReason($reason) : self |
130
|
|
|
{ |
131
|
|
|
$this->reason = $reason; |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
public function getExtra(): ?array |
140
|
|
|
{ |
141
|
|
|
return $this->extra; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param array $extra |
146
|
|
|
* |
147
|
|
|
* @return self |
148
|
|
|
*/ |
149
|
|
|
public function withExtra(array $extra) : self |
150
|
|
|
{ |
151
|
|
|
$this->extra = $extra; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return \DateInterval|int|string |
158
|
|
|
*/ |
159
|
|
|
public function getDelay() |
160
|
|
|
{ |
161
|
|
|
return $this->delay; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param \DateInterval|int|string $delay |
166
|
|
|
* |
167
|
|
|
* @return self |
168
|
|
|
*/ |
169
|
|
|
public function withDelay($delay) : self |
170
|
|
|
{ |
171
|
|
|
$this->delay = $delay; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return \DateTime|int|string |
178
|
|
|
*/ |
179
|
|
|
public function getDate() |
180
|
|
|
{ |
181
|
|
|
return $this->scheduled; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param \DateTime|int|string $scheduled |
186
|
|
|
* |
187
|
|
|
* @return self |
188
|
|
|
*/ |
189
|
|
|
public function withDate($scheduled) : self |
190
|
|
|
{ |
191
|
|
|
$this->scheduled = $scheduled; |
|
|
|
|
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|