Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.95% covered (warning)
80.95%
51 / 63
61.90% covered (warning)
61.90%
13 / 21
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractTask
80.95% covered (warning)
80.95%
51 / 63
61.90% covered (warning)
61.90%
13 / 21
43.47
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __clone
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setParentId
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setMasterStatus
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 hasParent
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getParentId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isMasterStatus
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setMetadata
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
6
 getMetadata
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 setParameter
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
6
 getParameter
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getParameters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCreatedAt
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getCreatedAt
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setOwner
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getOwner
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setLabel
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getLabel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 jsonSerialize
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
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
22namespace oat\taoTaskQueue\model\Task;
23
24/**
25 * Class AbstractTask
26 *
27 * @deprecated Use \oat\tao\model\taskQueue\Task\AbstractTask
28 *
29 * @author Gyula Szucs <gyula@taotesting.com>
30 */
31abstract class AbstractTask implements TaskInterface
32{
33    use WorkerContextAwareTrait;
34    use ChildTaskAwareTrait;
35
36    private $metadata = [];
37    private $parameters = [];
38
39    /**
40     * @inheritdoc
41     */
42    public function __construct($id, $owner)
43    {
44        $this->setMetadata(self::JSON_METADATA_ID_KEY, $id);
45        $this->setCreatedAt(new \DateTime('now', new \DateTimeZone('UTC')));
46        $this->setOwner($owner);
47    }
48
49    /**
50     * @return string
51     */
52    public function __toString()
53    {
54        return 'TASK ' . get_called_class() . ' [' . $this->getId() . ']';
55    }
56
57    /**
58     * Set a new id and create date.
59     */
60    public function __clone()
61    {
62        $this->setMetadata(self::JSON_METADATA_ID_KEY, \common_Utils::getNewUri());
63        $this->setCreatedAt(new \DateTime('now', new \DateTimeZone('UTC')));
64    }
65
66    /**
67     * @return string
68     */
69    public function getId()
70    {
71        return $this->getMetadata(self::JSON_METADATA_ID_KEY);
72    }
73
74    /**
75     * @inheritdoc
76     */
77    public function setParentId($taskId)
78    {
79        $this->setMetadata(self::JSON_METADATA_PARENT_ID_KEY, (string) $taskId);
80
81        return $this;
82    }
83
84    /**
85     * @inheritdoc
86     */
87    public function setMasterStatus($masterStatus)
88    {
89        $this->setMetadata(self::JSON_METADATA_MASTER_STATUS_KEY, (int) $masterStatus);
90
91        return $this;
92    }
93
94    /**
95     * @inheritdoc
96     */
97    public function hasParent()
98    {
99        return (bool) $this->getParentId();
100    }
101
102    /**
103     * @inheritdoc
104     */
105    public function getParentId()
106    {
107        return $this->getMetadata(self::JSON_METADATA_PARENT_ID_KEY);
108    }
109
110    /**
111     * @inheritdoc
112     */
113    public function isMasterStatus()
114    {
115        return (bool) $this->getMetadata(self::JSON_METADATA_MASTER_STATUS_KEY);
116    }
117
118    /**
119     * Set metadata
120     *
121     * @param  string|array|\Traversable $spec
122     * @param  mixed $value
123     * @throws \InvalidArgumentException
124     * @return TaskInterface
125     */
126    public function setMetadata($spec, $value = null)
127    {
128        if (is_string($spec)) {
129            $this->metadata[$spec] = $value;
130            return $this;
131        }
132
133        if (!is_array($spec) && !$spec instanceof \Traversable) {
134            throw new \InvalidArgumentException(sprintf(
135                'Expected a string, array, or Traversable argument in first position; received "%s"',
136                (is_object($spec) ? get_class($spec) : gettype($spec))
137            ));
138        }
139
140        foreach ($spec as $key => $value) {
141            $this->metadata[$key] = $value;
142        }
143
144        return $this;
145    }
146
147    /**
148     * Retrieve a single metadata as specified by key
149     *
150     * @param  string $key
151     * @param  null|mixed $default
152     * @throws \InvalidArgumentException
153     * @return mixed
154     */
155    public function getMetadata($key, $default = null)
156    {
157        if (!is_string($key)) {
158            throw new \InvalidArgumentException('Non-string argument provided as a metadata key');
159        }
160
161        if (array_key_exists($key, $this->metadata)) {
162            return $this->metadata[$key];
163        }
164
165        return $default;
166    }
167
168    /**
169     * Set task parameter
170     *
171     * @param  string|array|\Traversable $spec
172     * @param  mixed $value
173     * @throws \InvalidArgumentException
174     * @return TaskInterface
175     */
176    public function setParameter($spec, $value = null)
177    {
178        if (is_string($spec)) {
179            $this->parameters[$spec] = $value;
180            return $this;
181        }
182
183        if (!is_array($spec) && !$spec instanceof \Traversable) {
184            throw new \InvalidArgumentException(sprintf(
185                'Expected a string, array, or Traversable argument in first position; received "%s"',
186                (is_object($spec) ? get_class($spec) : gettype($spec))
187            ));
188        }
189
190        foreach ($spec as $key => $value) {
191            $this->parameters[$key] = $value;
192        }
193
194        return $this;
195    }
196
197    /**
198     * Retrieve a single parameter as specified by key
199     *
200     * @param  string $key
201     * @param  null|mixed $default
202     * @throws \InvalidArgumentException
203     * @return mixed
204     */
205    public function getParameter($key, $default = null)
206    {
207        if (!is_string($key)) {
208            throw new \InvalidArgumentException('Non-string argument provided as a parameter key');
209        }
210
211        if (array_key_exists($key, $this->parameters)) {
212            return $this->parameters[$key];
213        }
214
215        return $default;
216    }
217
218    /**
219     * @inheritdoc
220     */
221    public function getParameters()
222    {
223        return $this->parameters;
224    }
225
226    /**
227     * @param \DateTime $dateTime
228     * @return TaskInterface
229     */
230    public function setCreatedAt(\DateTime $dateTime)
231    {
232        $this->setMetadata(self::JSON_METADATA_CREATED_AT_KEY, $dateTime);
233
234        return $this;
235    }
236
237    /**
238     * @return \DateTime
239     */
240    public function getCreatedAt()
241    {
242        return $this->getMetadata(self::JSON_METADATA_CREATED_AT_KEY);
243    }
244
245    /**
246     * @param string $owner
247     * @return TaskInterface
248     */
249    public function setOwner($owner)
250    {
251        $this->setMetadata(self::JSON_METADATA_OWNER_KEY, (string) $owner);
252
253        return $this;
254    }
255
256    /**
257     * @return string
258     */
259    public function getOwner()
260    {
261        return $this->getMetadata(self::JSON_METADATA_OWNER_KEY);
262    }
263
264    /**
265     * @inheritdoc
266     */
267    public function setLabel($label)
268    {
269        $this->setMetadata(self::JSON_METADATA_LABEL_KEY, (string) $label);
270
271        return $this;
272    }
273
274    /**
275     * @inheritdoc
276     */
277    public function getLabel()
278    {
279        return $this->getMetadata(self::JSON_METADATA_LABEL_KEY);
280    }
281
282    /**
283     * @return array
284     */
285    public function jsonSerialize()
286    {
287        $cloneMetadata = $this->metadata;
288
289        // use ISO 8601 format for serializing date
290        $cloneMetadata[self::JSON_METADATA_CREATED_AT_KEY] = $this->getCreatedAt()->format('c');
291
292        return [
293            self::JSON_TASK_CLASS_NAME_KEY => get_called_class(),
294            self::JSON_METADATA_KEY => $cloneMetadata,
295            self::JSON_PARAMETERS_KEY => $this->getParameters()
296        ];
297    }
298}