Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
64.79% covered (warning)
64.79%
46 / 71
61.54% covered (warning)
61.54%
16 / 26
CRAP
0.00% covered (danger)
0.00%
0 / 1
TaskLogFilter
64.79% covered (warning)
64.79%
46 / 71
61.54% covered (warning)
61.54%
16 / 26
88.48
0.00% covered (danger)
0.00%
0 / 1
 getColumns
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 deselect
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getSortBy
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSortBy
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getSortOrder
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSortOrder
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getLimit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setLimit
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getOffset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setOffset
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFilters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addFilter
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 addAvailableFilters
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 availableForArchived
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 applyFilters
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
 eq
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 neq
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 lt
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 lte
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 gt
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 gte
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 like
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 notLike
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 in
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 notIn
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 assertValidOperator
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
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\TaskLog;
23
24use Doctrine\DBAL\Connection;
25use Doctrine\DBAL\Query\QueryBuilder;
26use oat\taoTaskQueue\model\TaskLogBroker\TaskLogBrokerInterface;
27use oat\taoTaskQueue\model\TaskLogInterface;
28
29/**
30 * @deprecated Use \oat\tao\model\taskQueue\TaskLog\TaskLogFilter
31 */
32class TaskLogFilter
33{
34    public const OP_EQ  = '=';
35    public const OP_NEQ = '!=';
36    public const OP_LT  = '<';
37    public const OP_LTE = '<=';
38    public const OP_GT  = '>';
39    public const OP_GTE = '>=';
40    public const OP_LIKE = 'LIKE';
41    public const OP_NOT_LIKE = 'NOT LIKE';
42    public const OP_IN = 'IN';
43    public const OP_NOT_IN = 'NOT IN';
44
45    private $filters = [];
46    private $limit;
47    private $offset;
48    private $sortBy;
49    private $sortOrder;
50
51    private $baseColumns = [
52        TaskLogBrokerInterface::COLUMN_ID,
53        TaskLogBrokerInterface::COLUMN_PARENT_ID,
54        TaskLogBrokerInterface::COLUMN_TASK_NAME,
55        TaskLogBrokerInterface::COLUMN_STATUS,
56        TaskLogBrokerInterface::COLUMN_MASTER_STATUS,
57        TaskLogBrokerInterface::COLUMN_REPORT
58    ];
59
60    private $optionalColumns = [
61        TaskLogBrokerInterface::COLUMN_PARAMETERS,
62        TaskLogBrokerInterface::COLUMN_LABEL,
63        TaskLogBrokerInterface::COLUMN_OWNER,
64        TaskLogBrokerInterface::COLUMN_CREATED_AT,
65        TaskLogBrokerInterface::COLUMN_UPDATED_AT
66    ];
67
68    private $deselectedColumns = [];
69
70    /**
71     * @return array
72     */
73    public function getColumns()
74    {
75        return array_merge($this->baseColumns, array_diff($this->optionalColumns, $this->deselectedColumns));
76    }
77
78    /**
79     * @param string $column
80     * @return $this
81     */
82    public function deselect($column)
83    {
84        if (!in_array($column, $this->optionalColumns)) {
85            throw new \InvalidArgumentException('Column "' . $column . '"" is not valid column or not unselectable.');
86        }
87
88        $this->deselectedColumns[] = $column;
89
90        return $this;
91    }
92
93    /**
94     * @return mixed
95     */
96    public function getSortBy()
97    {
98        return $this->sortBy;
99    }
100
101    /**
102     * @param mixed $sortBy
103     * @return TaskLogFilter
104     */
105    public function setSortBy($sortBy)
106    {
107        $this->sortBy = $sortBy;
108
109        return $this;
110    }
111
112    /**
113     * @return mixed
114     */
115    public function getSortOrder()
116    {
117        return $this->sortOrder;
118    }
119
120    /**
121     * @param mixed $sortOrder
122     * @return TaskLogFilter
123     */
124    public function setSortOrder($sortOrder)
125    {
126        $this->sortOrder = $sortOrder;
127
128        return $this;
129    }
130
131    /**
132     * @return int
133     */
134    public function getLimit()
135    {
136        return $this->limit;
137    }
138
139    /**
140     * @param int $limit
141     * @return TaskLogFilter
142     */
143    public function setLimit($limit)
144    {
145        $this->limit = max(0, $limit);
146
147        return $this;
148    }
149
150    /**
151     * @return int
152     */
153    public function getOffset()
154    {
155        return $this->offset;
156    }
157
158    /**
159     * @param int $offset
160     * @return TaskLogFilter
161     */
162    public function setOffset($offset)
163    {
164        $this->offset = max(0, $offset);
165
166        return $this;
167    }
168
169    /**
170     * @return array
171     */
172    public function getFilters()
173    {
174        return $this->filters;
175    }
176
177    /**
178     * @param string $field
179     * @param string $operator
180     * @param mixed $value
181     * @return TaskLogFilter
182     */
183    public function addFilter($field, $operator, $value)
184    {
185        $this->assertValidOperator($operator);
186
187        $this->filters[] =  [
188            'column' => (string) $field,
189            'columnSqlTranslate' => ':' . $field . uniqid(), // we need a unique placeholder
190            'operator' => $operator,
191            'value' => $value
192        ];
193
194        return $this;
195    }
196
197    /**
198     * Add a basic filter to query only rows belonging to a given user and not having status ARCHIVED.
199     *
200     * @param $userId
201     * @return $this
202     */
203    public function addAvailableFilters($userId)
204    {
205        $this->neq(TaskLogBrokerInterface::COLUMN_STATUS, TaskLogInterface::STATUS_ARCHIVED);
206
207        if ($userId !== TaskLogInterface::SUPER_USER) {
208            $this->eq(TaskLogBrokerInterface::COLUMN_OWNER, $userId);
209        }
210
211        return $this;
212    }
213
214    /**
215     * @param $userId
216     * @return $this
217     */
218    public function availableForArchived($userId)
219    {
220        $this->in(
221            TaskLogBrokerInterface::COLUMN_STATUS,
222            [TaskLogInterface::STATUS_FAILED, TaskLogInterface::STATUS_COMPLETED]
223        );
224
225        if ($userId !== TaskLogInterface::SUPER_USER) {
226            $this->eq(TaskLogBrokerInterface::COLUMN_OWNER, $userId);
227        }
228
229        return $this;
230    }
231
232    /**
233     * @param QueryBuilder $qb
234     * @return QueryBuilder
235     */
236    public function applyFilters(QueryBuilder $qb)
237    {
238        foreach ($this->getFilters() as $filter) {
239            $withParentheses = is_array($filter['value']) ? true : false;
240            $type = is_array($filter['value']) ? Connection::PARAM_STR_ARRAY : null;
241
242            $qb
243                ->andWhere(
244                    $filter['column'] . ' ' . $filter['operator'] . ' '
245                        . ($withParentheses ? '(' : '') . $filter['columnSqlTranslate'] . ($withParentheses ? ')' : '')
246                )
247                ->setParameter($filter['columnSqlTranslate'], $filter['value'], $type);
248        }
249
250        return $qb;
251    }
252
253    /**
254     * @param string $field
255     * @param mixed $value
256     * @return TaskLogFilter
257     */
258    public function eq($field, $value)
259    {
260        return $this->addFilter($field, self::OP_EQ, $value);
261    }
262
263    /**
264     * @param string $field
265     * @param mixed $value
266     * @return TaskLogFilter
267     */
268    public function neq($field, $value)
269    {
270        return $this->addFilter($field, self::OP_NEQ, $value);
271    }
272
273    /**
274     * @param string $field
275     * @param mixed $value
276     * @return TaskLogFilter
277     */
278    public function lt($field, $value)
279    {
280        return $this->addFilter($field, self::OP_LT, $value);
281    }
282
283    /**
284     * @param string $field
285     * @param mixed $value
286     * @return TaskLogFilter
287     */
288    public function lte($field, $value)
289    {
290        return $this->addFilter($field, self::OP_LTE, $value);
291    }
292
293    /**
294     * @param string $field
295     * @param mixed $value
296     * @return TaskLogFilter
297     */
298    public function gt($field, $value)
299    {
300        return $this->addFilter($field, self::OP_GT, $value);
301    }
302
303    /**
304     * @param string $field
305     * @param mixed $value
306     * @return TaskLogFilter
307     */
308    public function gte($field, $value)
309    {
310        return $this->addFilter($field, self::OP_GTE, $value);
311    }
312
313    /**
314     * @param string $field
315     * @param mixed $value
316     * @return TaskLogFilter
317     */
318    public function like($field, $value)
319    {
320        return $this->addFilter($field, self::OP_LIKE, $value);
321    }
322
323    /**
324     * @param string $field
325     * @param mixed $value
326     * @return TaskLogFilter
327     */
328    public function notLike($field, $value)
329    {
330        return $this->addFilter($field, self::OP_NOT_LIKE, $value);
331    }
332
333    /**
334     * @param string $field
335     * @param array $value
336     * @return TaskLogFilter
337     */
338    public function in($field, array $value)
339    {
340        return $this->addFilter($field, self::OP_IN, $value);
341    }
342
343    /**
344     * @param string $field
345     * @param array $value
346     * @return TaskLogFilter
347     */
348    public function notIn($field, array $value)
349    {
350        return $this->addFilter($field, self::OP_NOT_IN, $value);
351    }
352
353    /**
354     * @param $op
355     * @throws \InvalidArgumentException
356     */
357    private function assertValidOperator($op)
358    {
359        $operators = [
360            self::OP_EQ,
361            self::OP_NEQ,
362            self::OP_LT,
363            self::OP_LTE,
364            self::OP_GT,
365            self::OP_GTE,
366            self::OP_LIKE,
367            self::OP_NOT_LIKE,
368            self::OP_IN,
369            self::OP_NOT_IN,
370        ];
371
372        if (!in_array($op, $operators)) {
373            throw new \InvalidArgumentException('Operator "' . $op . '"" is not a valid operator.');
374        }
375    }
376}