Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
73.68% covered (warning)
73.68%
28 / 38
72.22% covered (warning)
72.22%
13 / 18
CRAP
0.00% covered (danger)
0.00%
0 / 1
CategorizedStatus
73.68% covered (warning)
73.68%
28 / 38
72.22% covered (warning)
72.22%
13 / 18
55.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createFromString
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
11
 completed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 archived
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 cancelled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 failed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 created
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 inProgress
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isCreated
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isInProgress
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isCompleted
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isFailed
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isArchived
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isCancelled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 equals
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMappedStatuses
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLabel
60.00% covered (warning)
60.00%
9 / 15
0.00% covered (danger)
0.00%
0 / 1
10.14
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\tao\model\taskQueue\TaskLog;
23
24use Exception;
25use oat\tao\model\taskQueue\TaskLogInterface;
26
27class CategorizedStatus
28{
29    public const STATUS_CREATED = 'created';
30    public const STATUS_IN_PROGRESS = 'in_progress';
31    public const STATUS_COMPLETED = 'completed';
32    public const STATUS_FAILED = 'failed';
33    public const STATUS_ARCHIVED = 'archived';
34    public const STATUS_CANCELLED = 'cancelled';
35
36    /** @var  string */
37    private $status;
38
39    public static $categorizeMapping = [
40        self::STATUS_CREATED => [
41            TaskLogInterface::STATUS_ENQUEUED
42        ],
43        self::STATUS_IN_PROGRESS => [
44            TaskLogInterface::STATUS_DEQUEUED,
45            TaskLogInterface::STATUS_RUNNING,
46            TaskLogInterface::STATUS_CHILD_RUNNING
47        ],
48        self::STATUS_COMPLETED   => [
49            TaskLogInterface::STATUS_COMPLETED
50        ],
51        self::STATUS_FAILED      => [
52            TaskLogInterface::STATUS_FAILED,
53            TaskLogInterface::STATUS_UNKNOWN
54        ],
55        self::STATUS_ARCHIVED    => [
56            TaskLogInterface::STATUS_ARCHIVED,
57        ],
58        self::STATUS_CANCELLED    => [
59            TaskLogInterface::STATUS_CANCELLED,
60        ],
61    ];
62
63    /**
64     * @param $status
65     */
66    protected function __construct($status)
67    {
68        $this->status = $status;
69    }
70
71    /**
72     * @param string $status
73     * @return CategorizedStatus
74     *
75     * @throws Exception
76     */
77    public static function createFromString($status)
78    {
79        switch ($status) {
80            case TaskLogInterface::STATUS_ENQUEUED:
81                return self::created();
82                break;
83
84            case TaskLogInterface::STATUS_DEQUEUED:
85            case TaskLogInterface::STATUS_RUNNING:
86            case TaskLogInterface::STATUS_CHILD_RUNNING:
87                return self::inProgress();
88                break;
89
90            case TaskLogInterface::STATUS_COMPLETED:
91                return self::completed();
92                break;
93
94            case TaskLogInterface::STATUS_ARCHIVED:
95                return self::archived();
96                break;
97
98            case TaskLogInterface::STATUS_CANCELLED:
99                return self::cancelled();
100                break;
101
102            case TaskLogInterface::STATUS_FAILED:
103            case TaskLogInterface::STATUS_UNKNOWN:
104                return self::failed();
105                break;
106
107            default:
108                throw new \Exception('Invalid status provided');
109        }
110    }
111
112    /**
113     * @return CategorizedStatus
114     */
115    public static function completed()
116    {
117        return new self(self::STATUS_COMPLETED);
118    }
119
120    /**
121     * @return CategorizedStatus
122     */
123    public static function archived()
124    {
125        return new self(self::STATUS_ARCHIVED);
126    }
127
128    /**
129     * @return CategorizedStatus
130     */
131    public static function cancelled()
132    {
133        return new self(self::STATUS_CANCELLED);
134    }
135
136    /**
137     * @return CategorizedStatus
138     */
139    public static function failed()
140    {
141        return new self(self::STATUS_FAILED);
142    }
143
144    /**
145     * @return CategorizedStatus
146     */
147    public static function created()
148    {
149        return new self(self::STATUS_CREATED);
150    }
151
152    /**
153     * @return CategorizedStatus
154     */
155    public static function inProgress()
156    {
157        return new self(self::STATUS_IN_PROGRESS);
158    }
159
160    /**
161     * @return bool
162     */
163    public function isCreated()
164    {
165        return $this->equals(self::created());
166    }
167
168    /**
169     * @return bool
170     */
171    public function isInProgress()
172    {
173        return $this->equals(self::inProgress());
174    }
175
176    /**
177     * @return bool
178     */
179    public function isCompleted()
180    {
181        return $this->equals(self::completed());
182    }
183
184    /**
185     * @return bool
186     */
187    public function isFailed()
188    {
189        return $this->equals(self::failed());
190    }
191
192    /**
193     * @return bool
194     */
195    public function isArchived()
196    {
197        return $this->equals(self::archived());
198    }
199
200    /**
201     * @return bool
202     */
203    public function isCancelled()
204    {
205        return $this->equals(self::cancelled());
206    }
207
208    /**
209     * @param CategorizedStatus $logStatus
210     *
211     * @return bool
212     */
213    public function equals(CategorizedStatus $logStatus)
214    {
215        return $this->status === $logStatus->status;
216    }
217
218    /**
219     * @param string $status
220     * @return array
221     */
222    public static function getMappedStatuses($status)
223    {
224        return self::$categorizeMapping[$status];
225    }
226
227    /**
228     * @return string
229     */
230    public function __toString()
231    {
232        return (string) $this->status;
233    }
234
235    /**
236     * @return string
237     */
238    public function getLabel()
239    {
240        $label = '';
241
242        switch ($this->status) {
243            case self::STATUS_CREATED:
244                $label = __('Queued');
245                break;
246
247            case self::STATUS_IN_PROGRESS:
248                $label = __('In Progress');
249                break;
250
251            case self::STATUS_COMPLETED:
252                $label = __('Completed');
253                break;
254
255            case self::STATUS_FAILED:
256                $label = __('Failed');
257                break;
258
259            case self::STATUS_ARCHIVED:
260                $label = __('Archived');
261                break;
262
263            case self::STATUS_CANCELLED:
264                $label = __('Cancelled');
265                break;
266        }
267
268        return $label;
269    }
270}