Completed
Push — master ( 8a4ca9...acd23c )
by Marco
02:46
created

src/Scheduler/Schedule.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php namespace Comodojo\Extender\Scheduler;
2
3
use \Exception;
4
5
/**
6
 * Scheduler table (Schedule)
7
 *
8
 * @package     Comodojo extender
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @license     GPL-3.0+
11
 *
12
 * LICENSE:
13
 * 
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26
 */
27
28
class Schedule {
29
30
    /**
31
     * Tasks database (a simple array!).
32
     *
33
     * @var     array
34
     */
35
    private $schedules = array();
36
37
    /**
38
     * Override whole jobs list (schedule)
39
     *
40
     * @param   array     $schedules
41
     *
42
     * @return  Object    $this
43
     */
44 6
    final public function setSchedules(array $schedules) {
45
46 6
        $this->schedules = $schedules;
47
48 6
        return $this;
49
50
    }
51
52
    /**
53
     * Register a schedule (job)
54
     *
55
     * @param   string    $name         Job name (unique)
56
     * @param   string    $task         Target task
57
     * @param   string    $expression   A valid cron expression
58
     * @param   string    $description  (optional) A brief description for the job
59
     * @param   bool      $parameters   (optional) array of parameters to pass to task
60
     *
61
     * @return  bool
62
     */
63 3
    final public function addSchedule($name, $task, $expression, $description = null, $parameters = array()) {
64
65 3
        if ( empty($name) OR empty($task) OR empty($expression) ) return false;
66
67
        try {
68
                
69 3
            Scheduler::validateExpression($expression);
70
71 3
        } catch (Exception $e) {
72
            
73
            return false;
74
75
        }
76
77 3
        list($min, $hour, $dayofmonth, $month, $dayofweek, $year) = explode(" ", trim($expression));
78
79 3
        array_push($this->schedules, array(
80 3
            "name" => $name,
81 3
            "task" => $task,
82 3
            "description" => is_null($description) ? '' : $description,
83 3
            "min" => $min,
84 3
            "hour" => $hour,
85 3
            "dayofmonth" => $dayofmonth,
86 3
            "month" => $month,
87 3
            "dayofweek" => $dayofweek,
88 3
            "year" => $year,
89
            "params" => $parameters
90 3
        ));
91
92 3
        return true;
93
94
    }
95
96
    /**
97
     * Get schedules as array
98
     *
99
     * @return  array
100
     */
101 3
    final public function getSchedules() {
102
103 3
        return $this->schedules;
104
105
    }
106
107
    /**
108
     * Check if job is scheduled
109
     *
110
     * @param   string    $job          Job name (unique)
111
     *
112
     * @return  bool
113
     */
114 3 View Code Duplication
    final public function isScheduled($job) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
116 3
        if ( empty($job) ) return false;
117
118 3
        foreach ( $this->schedules as $schedule ) {
119
120 3
            if ( $schedule['name'] == $job ) return true;
121
            
122 3
        }
123
124 3
        return false;
125
126
    }
127
128
    /**
129
     * Get job schedule details
130
     *
131
     * @param   string    $job          Job name (unique)
132
     *
133
     * @return  array
134
     */
135 3 View Code Duplication
    final public function getSchedule($job) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
137 3
        if ( empty($job) ) return null;
138
139 3
        foreach ( $this->schedules as $schedule ) {
140
141 3
            if ( $schedule['name'] == $job ) return $schedule;
142
            
143
        }
144
145
        return null;
146
147
    }
148
149
    /**
150
     * Get number or jobs to be executed
151
     *
152
     * @return  integer
153
     */
154 6
    final public function howMany() {
155
156 6
        return sizeof($this->schedules);
157
158
    }
159
160
}
161