Passed
Branch feature/2.0 (ef99fd)
by Jonathan
11:25
created

TaskSchedule   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 287
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 55
c 2
b 0
f 0
dl 0
loc 287
rs 10
wmc 21

19 Methods

Rating   Name   Duplication   Size   Complexity  
A setLastRunTime() 0 4 1
A __construct() 0 18 1
A isRunning() 0 3 1
A taskId() 0 3 1
A stopRunning() 0 4 1
A frequency() 0 3 1
A enable() 0 4 1
A id() 0 3 1
A setFrequency() 0 4 1
A startRunning() 0 4 1
A toArray() 0 11 1
A setRemainingOccurences() 0 4 1
A wasLastRunSuccess() 0 3 1
A setLastResult() 0 4 1
A lastRunTime() 0 3 1
A decrementRemainingOccurences() 0 9 3
A remainingOccurences() 0 3 1
A disable() 0 4 1
A isEnabled() 0 3 1
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage AdminTasks
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2012-2016, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Module\AdminTasks\Model;
16
17
use Carbon\CarbonInterval;
18
use Fisharebest\Webtrees\Carbon;
19
20
/**
21
 * Object to describe a schedule for a task.
22
 * Setters can be chained.
23
 *
24
 */
25
class TaskSchedule
26
{
27
    
28
    /**
29
     * Task Schedule ID
30
     * @var int $id
31
     */
32
    private $id;
33
    
34
    /**
35
     * Task schedule status
36
     * @var bool $enabled
37
     */
38
    private $enabled;
39
    
40
    /**
41
     * ID of the task attached to schedule
42
     * @var string $task_id
43
     */
44
    private $task_id;
45
    
46
    /**
47
     * Last updated date
48
     * @var Carbon $last_run
49
     */
50
    private $last_run;
51
    
52
    /**
53
     * Last run result
54
     * @var bool $last_result
55
     */
56
    private $last_result;
57
    
58
    /**
59
     * Task run frequency
60
     * @var CarbonInterval $frequency
61
     */
62
    private $frequency;
63
    
64
    /**
65
     * Task remaining runs
66
     * @var int $nb_occurrences
67
     */
68
    private $nb_occurrences;
69
    
70
    /**
71
     * Current running status of the task
72
     * @var bool $is_running
73
     */
74
    private $is_running;
75
    
76
    /**
77
     * Constructor for TaskSchedule
78
     *
79
     * @param int $id Schedule ID
80
     * @param string $task_id Task ID
81
     * @param bool $enabled Is the schedule enabled
82
     * @param Carbon $last_run Last successful run date/time
83
     * @param bool $last_result Result of the last run
84
     * @param CarbonInterval $frequency Schedule frequency
85
     * @param int $nb_occurrences Number of remaining occurrences to be run
86
     * @param bool $is_running Is the task currently running
87
     */
88
    public function __construct(
89
        int $id,
90
        string $task_id,
91
        bool $enabled,
92
        Carbon $last_run,
93
        bool $last_result,
94
        CarbonInterval $frequency,
95
        int $nb_occurrences,
96
        bool $is_running
97
    ) {
98
        $this->id = $id;
99
        $this->task_id = $task_id;
100
        $this->enabled = $enabled;
101
        $this->last_run = $last_run;
102
        $this->last_result = $last_result;
103
        $this->frequency = $frequency;
104
        $this->nb_occurrences = $nb_occurrences;
105
        $this->is_running = $is_running;
106
    }
107
    
108
    /**
109
     * Get the schedule ID.
110
     *
111
     * @return int
112
     */
113
    public function id(): int
114
    {
115
        return $this->id;
116
    }
117
    
118
    /**
119
     * Get the task ID.
120
     *
121
     * @return string
122
     */
123
    public function taskId(): string
124
    {
125
        return $this->task_id;
126
    }
127
    
128
    /**
129
     * Returns whether the schedule is enabled
130
     *
131
     * @return bool
132
     */
133
    public function isEnabled(): bool
134
    {
135
        return $this->enabled;
136
    }
137
    
138
    /**
139
     * Enable the schedule
140
     *
141
     * @return self
142
     */
143
    public function enable(): self
144
    {
145
        $this->enabled = true;
146
        return $this;
147
    }
148
    
149
    /**
150
     * Disable the schedule
151
     *
152
     * @return self
153
     */
154
    public function disable(): self
155
    {
156
        $this->enabled = false;
157
        return $this;
158
    }
159
    
160
    /**
161
     * Get the frequency of the schedule
162
     *
163
     * @return CarbonInterval
164
     */
165
    public function frequency(): CarbonInterval
166
    {
167
        return $this->frequency;
168
    }
169
    
170
    /**
171
     * Set the frequency of the schedule
172
     *
173
     * @param CarbonInterval $frequency
174
     * @return self
175
     */
176
    public function setFrequency(CarbonInterval $frequency): self
177
    {
178
        $this->frequency = $frequency;
179
        return $this;
180
    }
181
    
182
    /**
183
     * Get the date/time of the last successful run.
184
     *
185
     * @return Carbon
186
     */
187
    public function lastRunTime(): Carbon
188
    {
189
        return $this->last_run;
190
    }
191
    
192
    /**
193
     * Set the last successful run date/time
194
     *
195
     * @param Carbon $last_run
196
     * @return self
197
     */
198
    public function setLastRunTime(Carbon $last_run): self
199
    {
200
        $this->last_run = $last_run;
201
        return $this;
202
    }
203
    
204
    /**
205
     * Returns whether the last run was successful
206
     *
207
     * @return bool
208
     */
209
    public function wasLastRunSuccess(): bool
210
    {
211
        return $this->last_result;
212
    }
213
214
    /**
215
     * Set the last run result
216
     *
217
     * @param bool $last_result
218
     * @return self
219
     */
220
    public function setLastResult(bool $last_result): self
221
    {
222
        $this->last_result = $last_result;
223
        return $this;
224
    }
225
    
226
    /**
227
     * Get the number of remaining of occurrences of task runs.
228
     * Returns 0 if the tasks must be run indefinitely.
229
     *
230
     * @return int
231
     */
232
    public function remainingOccurences(): int
233
    {
234
        return $this->nb_occurrences;
235
    }
236
    
237
    /**
238
     * Decrements the number of remaining occurences by 1.
239
     * The task will be disabled when the number reaches 0.
240
     *
241
     * @return self
242
     */
243
    public function decrementRemainingOccurences(): self
244
    {
245
        if ($this->nb_occurrences > 0) {
246
            $this->nb_occurrences--;
247
            if ($this->nb_occurrences == 0) {
248
                $this->disable();
249
            }
250
        }
251
        return $this;
252
    }
253
    
254
    /**
255
     * Set the number of remaining occurences of task runs.
256
     *
257
     * @param int $nb_occurrences
258
     * @return self
259
     */
260
    public function setRemainingOccurences(int $nb_occurrences): self
261
    {
262
        $this->nb_occurrences = $nb_occurrences;
263
        return $this;
264
    }
265
    
266
    /**
267
     * Returns whether the task is running
268
     * @return bool
269
     */
270
    public function isRunning(): bool
271
    {
272
        return $this->is_running;
273
    }
274
    
275
    /**
276
     * Informs the schedule that the task is going to run
277
     *
278
     * @return self
279
     */
280
    public function startRunning(): self
281
    {
282
        $this->is_running = true;
283
        return $this;
284
    }
285
    
286
    /**
287
     * Informs the schedule that the task has stopped running.
288
     * @return self
289
     */
290
    public function stopRunning(): self
291
    {
292
        $this->is_running = false;
293
        return $this;
294
    }
295
    
296
    /**
297
     * Returns the schedule details as an associate array
298
     *
299
     * @return array
300
     */
301
    public function toArray(): array
302
    {
303
        return [
304
            'id'            =>  $this->id,
305
            'task_id'       =>  $this->task_id,
306
            'enabled'       =>  $this->enabled,
307
            'last_run'      =>  $this->last_run,
308
            'last_result'   =>  $this->last_result,
309
            'frequency'     =>  $this->frequency,
310
            'nb_occurrences' =>  $this->nb_occurrences,
311
            'is_running'    =>  $this->is_running
312
        ];
313
    }
314
}
315