Passed
Push — main ( 195893...a2ce2d )
by Jonathan
06:26 queued 01:03
created

TaskSchedule::decrementRemainingOccurrences()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
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\Common\Tasks;
16
17
use Fisharebest\Webtrees\Contracts\TimestampInterface;
18
19
/**
20
 * Object to describe a schedule for a task.
21
 * Setters can be chained.
22
 *
23
 */
24
class TaskSchedule
25
{
26
    private int $id;
27
    private bool $enabled;
28
    private string $task_id;
29
    private TimestampInterface $last_run;
30
    private bool $last_result;
31
    private int $frequency;
32
    private int $nb_occurrences;
33
    private bool $is_running;
34
35
    /**
36
     * Constructor for TaskSchedule
37
     *
38
     * @param int $id Schedule ID
39
     * @param string $task_id Task ID
40
     * @param bool $enabled Is the schedule enabled
41
     * @param TimestampInterface $last_run Last successful run date/time
42
     * @param bool $last_result Result of the last run
43
     * @param int $frequency Schedule frequency in minutes
44
     * @param int $nb_occurrences Number of remaining occurrences to be run
45
     * @param bool $is_running Is the task currently running
46
     */
47
    public function __construct(
48
        int $id,
49
        string $task_id,
50
        bool $enabled,
51
        TimestampInterface $last_run,
52
        bool $last_result,
53
        int $frequency,
54
        int $nb_occurrences,
55
        bool $is_running
56
    ) {
57
        $this->id = $id;
58
        $this->task_id = $task_id;
59
        $this->enabled = $enabled;
60
        $this->last_run = $last_run;
61
        $this->last_result = $last_result;
62
        $this->frequency = $frequency;
63
        $this->nb_occurrences = $nb_occurrences;
64
        $this->is_running = $is_running;
65
    }
66
67
    /**
68
     * Get the schedule ID.
69
     *
70
     * @return int
71
     */
72
    public function id(): int
73
    {
74
        return $this->id;
75
    }
76
77
    /**
78
     * Get the task ID.
79
     *
80
     * @return string
81
     */
82
    public function taskId(): string
83
    {
84
        return $this->task_id;
85
    }
86
87
    /**
88
     * Returns whether the schedule is enabled
89
     *
90
     * @return bool
91
     */
92
    public function isEnabled(): bool
93
    {
94
        return $this->enabled;
95
    }
96
97
    /**
98
     * Enable the schedule
99
     *
100
     * @return $this
101
     */
102
    public function enable(): self
103
    {
104
        $this->enabled = true;
105
        return $this;
106
    }
107
108
    /**
109
     * Disable the schedule
110
     *
111
     * @return $this
112
     */
113
    public function disable(): self
114
    {
115
        $this->enabled = false;
116
        return $this;
117
    }
118
119
    /**
120
     * Get the frequency of the schedule
121
     *
122
     * @return int
123
     */
124
    public function frequency(): int
125
    {
126
        return $this->frequency;
127
    }
128
129
    /**
130
     * Set the frequency of the schedule
131
     *
132
     * @param int $frequency
133
     * @return $this
134
     */
135
    public function setFrequency(int $frequency): self
136
    {
137
        $this->frequency = $frequency;
138
        return $this;
139
    }
140
141
    /**
142
     * Get the date/time of the last successful run.
143
     *
144
     * @return TimestampInterface
145
     */
146
    public function lastRunTime(): TimestampInterface
147
    {
148
        return $this->last_run;
149
    }
150
151
    /**
152
     * Set the last successful run date/time
153
     *
154
     * @param TimestampInterface $last_run
155
     * @return $this
156
     */
157
    public function setLastRunTime(TimestampInterface $last_run): self
158
    {
159
        $this->last_run = $last_run;
160
        return $this;
161
    }
162
163
    /**
164
     * Returns whether the last run was successful
165
     *
166
     * @return bool
167
     */
168
    public function wasLastRunSuccess(): bool
169
    {
170
        return $this->last_result;
171
    }
172
173
    /**
174
     * Set the last run result
175
     *
176
     * @param bool $last_result
177
     * @return $this
178
     */
179
    public function setLastResult(bool $last_result): self
180
    {
181
        $this->last_result = $last_result;
182
        return $this;
183
    }
184
185
    /**
186
     * Get the number of remaining of occurrences of task runs.
187
     * Returns 0 if the tasks must be run indefinitely.
188
     *
189
     * @return int
190
     */
191
    public function remainingOccurrences(): int
192
    {
193
        return $this->nb_occurrences;
194
    }
195
196
    /**
197
     * Decrements the number of remaining occurrences by 1.
198
     * The task will be disabled when the number reaches 0.
199
     *
200
     * @return $this
201
     */
202
    public function decrementRemainingOccurrences(): self
203
    {
204
        if ($this->nb_occurrences > 0) {
205
            $this->nb_occurrences--;
206
            if ($this->nb_occurrences == 0) {
207
                $this->disable();
208
            }
209
        }
210
        return $this;
211
    }
212
213
    /**
214
     * Set the number of remaining occurrences of task runs.
215
     *
216
     * @param int $nb_occurrences
217
     * @return $this
218
     */
219
    public function setRemainingOccurrences(int $nb_occurrences): self
220
    {
221
        $this->nb_occurrences = $nb_occurrences;
222
        return $this;
223
    }
224
225
    /**
226
     * Returns whether the task is running
227
     * @return bool
228
     */
229
    public function isRunning(): bool
230
    {
231
        return $this->is_running;
232
    }
233
234
    /**
235
     * Informs the schedule that the task is going to run
236
     *
237
     * @return $this
238
     */
239
    public function startRunning(): self
240
    {
241
        $this->is_running = true;
242
        return $this;
243
    }
244
245
    /**
246
     * Informs the schedule that the task has stopped running.
247
     * @return $this
248
     */
249
    public function stopRunning(): self
250
    {
251
        $this->is_running = false;
252
        return $this;
253
    }
254
255
    /**
256
     * Returns the schedule details as an associate array
257
     *
258
     * @phpcs:ignore Generic.Files.LineLength.TooLong
259
     * @return array{id: int, task_id: string, enabled: bool, last_run: TimestampInterface, last_result: bool, frequency: int, nb_occurrences: int, is_running: bool}
260
     */
261
    public function toArray(): array
262
    {
263
        return [
264
            'id'            =>  $this->id,
265
            'task_id'       =>  $this->task_id,
266
            'enabled'       =>  $this->enabled,
267
            'last_run'      =>  $this->last_run,
268
            'last_result'   =>  $this->last_result,
269
            'frequency'     =>  $this->frequency,
270
            'nb_occurrences' =>  $this->nb_occurrences,
271
            'is_running'    =>  $this->is_running
272
        ];
273
    }
274
}
275