Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
85.71% |
54 / 63 |
|
71.43% |
15 / 21 |
CRAP | |
0.00% |
0 / 1 |
AbstractTask | |
85.71% |
54 / 63 |
|
71.43% |
15 / 21 |
38.57 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__clone | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
1.12 | |||
getId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setParentId | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setMasterStatus | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
hasParent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getParentId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isMasterStatus | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setMetadata | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
6 | |||
getMetadata | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
setParameter | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
6 | |||
getParameter | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getParameters | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setCreatedAt | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getCreatedAt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setOwner | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getOwner | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setLabel | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getLabel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
jsonSerialize | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | * |
20 | */ |
21 | |
22 | namespace oat\tao\model\taskQueue\Task; |
23 | |
24 | /** |
25 | * @author Gyula Szucs <gyula@taotesting.com> |
26 | */ |
27 | abstract class AbstractTask implements TaskInterface |
28 | { |
29 | use WorkerContextAwareTrait; |
30 | use ChildTaskAwareTrait; |
31 | |
32 | private $metadata = []; |
33 | private $parameters = []; |
34 | |
35 | /** |
36 | * @inheritdoc |
37 | */ |
38 | public function __construct($id, $owner) |
39 | { |
40 | $this->setMetadata(self::JSON_METADATA_ID_KEY, $id); |
41 | $this->setCreatedAt(new \DateTime('now', new \DateTimeZone('UTC'))); |
42 | $this->setOwner($owner); |
43 | } |
44 | |
45 | /** |
46 | * @return string |
47 | */ |
48 | public function __toString() |
49 | { |
50 | return 'TASK ' . get_called_class() . ' [' . $this->getId() . ']'; |
51 | } |
52 | |
53 | /** |
54 | * Set a new id and create date. |
55 | */ |
56 | public function __clone() |
57 | { |
58 | $this->setMetadata(self::JSON_METADATA_ID_KEY, \common_Utils::getNewUri()); |
59 | $this->setCreatedAt(new \DateTime('now', new \DateTimeZone('UTC'))); |
60 | } |
61 | |
62 | /** |
63 | * @return string |
64 | */ |
65 | public function getId() |
66 | { |
67 | return $this->getMetadata(self::JSON_METADATA_ID_KEY); |
68 | } |
69 | |
70 | /** |
71 | * @inheritdoc |
72 | */ |
73 | public function setParentId($taskId) |
74 | { |
75 | $this->setMetadata(self::JSON_METADATA_PARENT_ID_KEY, (string) $taskId); |
76 | |
77 | return $this; |
78 | } |
79 | |
80 | /** |
81 | * @inheritdoc |
82 | */ |
83 | public function setMasterStatus($masterStatus) |
84 | { |
85 | $this->setMetadata(self::JSON_METADATA_MASTER_STATUS_KEY, (int) $masterStatus); |
86 | |
87 | return $this; |
88 | } |
89 | |
90 | /** |
91 | * @inheritdoc |
92 | */ |
93 | public function hasParent() |
94 | { |
95 | return (bool) $this->getParentId(); |
96 | } |
97 | |
98 | /** |
99 | * @inheritdoc |
100 | */ |
101 | public function getParentId() |
102 | { |
103 | return $this->getMetadata(self::JSON_METADATA_PARENT_ID_KEY); |
104 | } |
105 | |
106 | /** |
107 | * @inheritdoc |
108 | */ |
109 | public function isMasterStatus() |
110 | { |
111 | return (bool) $this->getMetadata(self::JSON_METADATA_MASTER_STATUS_KEY); |
112 | } |
113 | |
114 | /** |
115 | * Set metadata |
116 | * |
117 | * @param string|array|\Traversable $spec |
118 | * @param mixed $value |
119 | * @throws \InvalidArgumentException |
120 | * @return TaskInterface |
121 | */ |
122 | public function setMetadata($spec, $value = null) |
123 | { |
124 | if (is_string($spec)) { |
125 | $this->metadata[$spec] = $value; |
126 | return $this; |
127 | } |
128 | |
129 | if (!is_array($spec) && !$spec instanceof \Traversable) { |
130 | throw new \InvalidArgumentException(sprintf( |
131 | 'Expected a string, array, or Traversable argument in first position; received "%s"', |
132 | (is_object($spec) ? get_class($spec) : gettype($spec)) |
133 | )); |
134 | } |
135 | |
136 | foreach ($spec as $key => $value) { |
137 | $this->metadata[$key] = $value; |
138 | } |
139 | |
140 | return $this; |
141 | } |
142 | |
143 | /** |
144 | * Retrieve a single metadata as specified by key |
145 | * |
146 | * @param string $key |
147 | * @param null|mixed $default |
148 | * @throws \InvalidArgumentException |
149 | * @return mixed |
150 | */ |
151 | public function getMetadata($key, $default = null) |
152 | { |
153 | if (!is_string($key)) { |
154 | throw new \InvalidArgumentException('Non-string argument provided as a metadata key'); |
155 | } |
156 | |
157 | if (array_key_exists($key, $this->metadata)) { |
158 | return $this->metadata[$key]; |
159 | } |
160 | |
161 | return $default; |
162 | } |
163 | |
164 | /** |
165 | * Set task parameter |
166 | * |
167 | * @param string|array|\Traversable $spec |
168 | * @param mixed $value |
169 | * @throws \InvalidArgumentException |
170 | * @return TaskInterface |
171 | */ |
172 | public function setParameter($spec, $value = null) |
173 | { |
174 | if (is_string($spec)) { |
175 | $this->parameters[$spec] = $value; |
176 | return $this; |
177 | } |
178 | |
179 | if (!is_array($spec) && !$spec instanceof \Traversable) { |
180 | throw new \InvalidArgumentException(sprintf( |
181 | 'Expected a string, array, or Traversable argument in first position; received "%s"', |
182 | (is_object($spec) ? get_class($spec) : gettype($spec)) |
183 | )); |
184 | } |
185 | |
186 | foreach ($spec as $key => $value) { |
187 | $this->parameters[$key] = $value; |
188 | } |
189 | |
190 | return $this; |
191 | } |
192 | |
193 | /** |
194 | * Retrieve a single parameter as specified by key |
195 | * |
196 | * @param string $key |
197 | * @param null|mixed $default |
198 | * @throws \InvalidArgumentException |
199 | * @return mixed |
200 | */ |
201 | public function getParameter($key, $default = null) |
202 | { |
203 | if (!is_string($key)) { |
204 | throw new \InvalidArgumentException('Non-string argument provided as a parameter key'); |
205 | } |
206 | |
207 | if (array_key_exists($key, $this->parameters)) { |
208 | return $this->parameters[$key]; |
209 | } |
210 | |
211 | return $default; |
212 | } |
213 | |
214 | /** |
215 | * @inheritdoc |
216 | */ |
217 | public function getParameters() |
218 | { |
219 | return $this->parameters; |
220 | } |
221 | |
222 | /** |
223 | * @param \DateTime $dateTime |
224 | * @return TaskInterface |
225 | */ |
226 | public function setCreatedAt(\DateTime $dateTime) |
227 | { |
228 | $this->setMetadata(self::JSON_METADATA_CREATED_AT_KEY, $dateTime); |
229 | |
230 | return $this; |
231 | } |
232 | |
233 | /** |
234 | * @return \DateTime |
235 | */ |
236 | public function getCreatedAt() |
237 | { |
238 | return $this->getMetadata(self::JSON_METADATA_CREATED_AT_KEY); |
239 | } |
240 | |
241 | /** |
242 | * @param string $owner |
243 | * @return TaskInterface |
244 | */ |
245 | public function setOwner($owner) |
246 | { |
247 | $this->setMetadata(self::JSON_METADATA_OWNER_KEY, (string) $owner); |
248 | |
249 | return $this; |
250 | } |
251 | |
252 | /** |
253 | * @return string |
254 | */ |
255 | public function getOwner() |
256 | { |
257 | return $this->getMetadata(self::JSON_METADATA_OWNER_KEY); |
258 | } |
259 | |
260 | /** |
261 | * @inheritdoc |
262 | */ |
263 | public function setLabel($label) |
264 | { |
265 | $this->setMetadata(self::JSON_METADATA_LABEL_KEY, (string) $label); |
266 | |
267 | return $this; |
268 | } |
269 | |
270 | /** |
271 | * @inheritdoc |
272 | */ |
273 | public function getLabel() |
274 | { |
275 | return $this->getMetadata(self::JSON_METADATA_LABEL_KEY); |
276 | } |
277 | |
278 | /** |
279 | * @return array |
280 | */ |
281 | public function jsonSerialize(): array |
282 | { |
283 | $cloneMetadata = $this->metadata; |
284 | |
285 | // use ISO 8601 format for serializing date |
286 | $cloneMetadata[self::JSON_METADATA_CREATED_AT_KEY] = $this->getCreatedAt()->format('c'); |
287 | |
288 | return [ |
289 | self::JSON_TASK_CLASS_NAME_KEY => get_called_class(), |
290 | self::JSON_METADATA_KEY => $cloneMetadata, |
291 | self::JSON_PARAMETERS_KEY => $this->getParameters() |
292 | ]; |
293 | } |
294 | } |