1 | <?php |
||
14 | class Message implements MessageInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $type; |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $body; |
||
25 | |||
26 | /** |
||
27 | * @var int |
||
28 | */ |
||
29 | protected $state; |
||
30 | |||
31 | /** |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $restartCount = 0; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $group; |
||
40 | |||
41 | /** |
||
42 | * @var \DateTime |
||
43 | */ |
||
44 | protected $createdAt; |
||
45 | |||
46 | /** |
||
47 | * @var \DateTime |
||
48 | */ |
||
49 | protected $updatedAt; |
||
50 | |||
51 | /** |
||
52 | * @var \DateTime |
||
53 | */ |
||
54 | protected $startedAt; |
||
55 | |||
56 | /** |
||
57 | * @var \DateTime |
||
58 | */ |
||
59 | protected $completedAt; |
||
60 | |||
61 | /** |
||
62 | * Constructor. |
||
63 | */ |
||
64 | public function __construct() |
||
65 | { |
||
66 | $this->createdAt = new \DateTime(); |
||
67 | $this->updatedAt = new \DateTime(); |
||
68 | $this->state = self::STATE_OPEN; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | public function __clone() |
||
75 | { |
||
76 | $this->state = self::STATE_OPEN; |
||
77 | $this->startedAt = null; |
||
78 | $this->completedAt = null; |
||
79 | |||
80 | $this->createdAt = new \DateTime(); |
||
81 | $this->updatedAt = new \DateTime(); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function setBody(array $body) |
||
88 | { |
||
89 | $this->body = $body; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function getBody() |
||
96 | { |
||
97 | return $this->body; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function getValue($names, $default = null) |
||
104 | { |
||
105 | if (!is_array($names)) { |
||
106 | $names = array($names); |
||
107 | } |
||
108 | |||
109 | $body = $this->getBody(); |
||
110 | foreach ($names as $name) { |
||
111 | if (!isset($body[$name])) { |
||
112 | return $default; |
||
113 | } |
||
114 | |||
115 | $body = $body[$name]; |
||
116 | } |
||
117 | |||
118 | return $body; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function setCompletedAt(\DateTime $completedAt = null) |
||
125 | { |
||
126 | $this->completedAt = $completedAt; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | public function getCompletedAt() |
||
133 | { |
||
134 | return $this->completedAt; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function setCreatedAt(\DateTime $createdAt = null) |
||
141 | { |
||
142 | $this->createdAt = $createdAt; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | public function getCreatedAt() |
||
149 | { |
||
150 | return $this->createdAt; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | public function setGroup($group) |
||
157 | { |
||
158 | $this->group = $group; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function getGroup() |
||
165 | { |
||
166 | return $this->group; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function setType($type) |
||
173 | { |
||
174 | $this->type = $type; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function getType() |
||
181 | { |
||
182 | return $this->type; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | public function setState($state) |
||
189 | { |
||
190 | $this->state = $state; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | public function getState() |
||
197 | { |
||
198 | return $this->state; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | public function setRestartCount($restartCount) |
||
205 | { |
||
206 | $this->restartCount = $restartCount; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | public function getRestartCount() |
||
213 | { |
||
214 | return $this->restartCount; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | public function setUpdatedAt(\DateTime $updatedAt = null) |
||
221 | { |
||
222 | $this->updatedAt = $updatedAt; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | public function getUpdatedAt() |
||
229 | { |
||
230 | return $this->updatedAt; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @return string[] |
||
235 | */ |
||
236 | public static function getStateList() |
||
237 | { |
||
238 | return array( |
||
239 | self::STATE_OPEN => 'open', |
||
240 | self::STATE_IN_PROGRESS => 'in_progress', |
||
241 | self::STATE_DONE => 'done', |
||
242 | self::STATE_ERROR => 'error', |
||
243 | self::STATE_CANCELLED => 'cancelled', |
||
244 | ); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | public function setStartedAt(\DateTime $startedAt = null) |
||
251 | { |
||
252 | $this->startedAt = $startedAt; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * {@inheritdoc} |
||
257 | */ |
||
258 | public function getStartedAt() |
||
259 | { |
||
260 | return $this->startedAt; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * {@inheritdoc} |
||
265 | */ |
||
266 | public function getStateName() |
||
267 | { |
||
268 | $list = self::getStateList(); |
||
269 | |||
270 | return isset($list[$this->getState()]) ? $list[$this->getState()] : ''; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | public function isRunning() |
||
277 | { |
||
278 | return $this->state == self::STATE_IN_PROGRESS; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * {@inheritdoc} |
||
283 | */ |
||
284 | public function isCancelled() |
||
288 | |||
289 | /** |
||
290 | * {@inheritdoc} |
||
291 | */ |
||
292 | public function isError() |
||
293 | { |
||
294 | return $this->state == self::STATE_ERROR; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * {@inheritdoc} |
||
299 | */ |
||
300 | public function isOpen() |
||
301 | { |
||
302 | return $this->state == self::STATE_OPEN; |
||
303 | } |
||
304 | } |
||
305 |