Test Failed
Push — master ( a22b19...089bc3 )
by Justin
04:06
created

Tasks   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 4
c 2
b 0
f 1
dl 0
loc 31
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getOverduedTasks() 0 15 2
A schedule() 0 11 2
1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
class Tasks {
20
21
	public static function schedule (int $limit = 3) {
22
		//execute overdued tasks
23
		foreach (self::getOverduedTasks($limit) as $task) {
24
			//cast task
25
			$task = Task::cast($task);
26
27
			//execute task
28
			$task->execute();
29
30
			//update last execution timestamp
31
			$task->setLastExecution();
32
		}
33
	}
34
35
	public static function getOverduedTasks (int $limit = 10) : array {
36
		$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}tasks` WHERE (DATE_ADD(`last_execution`, INTERVAL `interval` MINUTE) < NOW() OR `last_execution` = '0000-00-00 00:00:00') AND `activated` = '1' LIMIT 0, :limit; ", array(
37
			'limit' => array(
38
				'type' => PDO::PARAM_INT,
39
				'value' => $limit
40
			)
41
		));
42
43
		$tasks = array();
44
45
		foreach ($rows as $row) {
46
			$tasks[] = new Task($row);
47
		}
48
49
		return $tasks;
50
	}
51
52
}
53
54
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
55