Passed
Pull Request — master (#408)
by
unknown
03:22
created

Schedule   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 424
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 91
dl 0
loc 424
rs 9.2
c 1
b 0
f 0
wmc 40

37 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A daily() 0 4 1
A hourlyAt() 0 4 1
A everyFourHours() 0 4 1
A everyFiveMinutes() 0 4 1
A everyTenMinutes() 0 4 1
A everyThreeHours() 0 4 1
A everyThirtyMinutes() 0 4 1
A everyMinute() 0 4 1
A everySixHours() 0 4 1
A everyFifteenMinutes() 0 4 1
A everyTwoHours() 0 4 1
A hourly() 0 4 1
A fridays() 0 3 1
A twiceDaily() 0 4 1
A at() 0 13 1
A days() 0 5 2
A call() 0 4 1
A monthlyOn() 0 7 1
A build() 0 11 3
A yearly() 0 4 1
A monthly() 0 4 1
A wednesdays() 0 3 1
A cron() 0 4 1
A saturdays() 0 3 1
A weekdays() 0 4 1
A mondays() 0 3 1
A quarterly() 0 4 1
A weeklyOn() 0 7 1
A weekends() 0 4 1
A dailyAt() 0 7 1
A tuesdays() 0 3 1
A twiceMonthly() 0 7 1
A sundays() 0 3 1
A getExpression() 0 3 1
A weekly() 0 4 1
A thursdays() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Schedule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Schedule, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 3.0.0
13
 */
14
15
namespace Quantum\Libraries\Cron;
16
17
use Quantum\Libraries\Cron\Exceptions\CronException;
18
19
/**
20
 * Class Schedule
21
 * Fluent API for creating cron schedules
22
 * @package Quantum\Libraries\Cron
23
 */
24
class Schedule
25
{
26
    /**
27
     * Task name
28
     * @var string
29
     */
30
    private $name;
31
32
    /**
33
     * Cron expression
34
     * @var string
35
     */
36
    private $expression;
37
38
    /**
39
     * Task callback
40
     * @var callable|null
41
     */
42
    private $callback = null;
43
44
    /**
45
     * Schedule constructor
46
     * @param string $name
47
     */
48
    public function __construct(string $name)
49
    {
50
        $this->name = $name;
51
    }
52
53
    /**
54
     * Run the task every minute
55
     * @return self
56
     */
57
    public function everyMinute(): self
58
    {
59
        $this->expression = '* * * * *';
60
        return $this;
61
    }
62
63
    /**
64
     * Run the task every five minutes
65
     * @return self
66
     */
67
    public function everyFiveMinutes(): self
68
    {
69
        $this->expression = '*/5 * * * *';
70
        return $this;
71
    }
72
73
    /**
74
     * Run the task every ten minutes
75
     * @return self
76
     */
77
    public function everyTenMinutes(): self
78
    {
79
        $this->expression = '*/10 * * * *';
80
        return $this;
81
    }
82
83
    /**
84
     * Run the task every fifteen minutes
85
     * @return self
86
     */
87
    public function everyFifteenMinutes(): self
88
    {
89
        $this->expression = '*/15 * * * *';
90
        return $this;
91
    }
92
93
    /**
94
     * Run the task every thirty minutes
95
     * @return self
96
     */
97
    public function everyThirtyMinutes(): self
98
    {
99
        $this->expression = '*/30 * * * *';
100
        return $this;
101
    }
102
103
    /**
104
     * Run the task hourly
105
     * @return self
106
     */
107
    public function hourly(): self
108
    {
109
        $this->expression = '0 * * * *';
110
        return $this;
111
    }
112
113
    /**
114
     * Run the task hourly at a specific minute
115
     * @param int $minute
116
     * @return self
117
     */
118
    public function hourlyAt(int $minute): self
119
    {
120
        $this->expression = "{$minute} * * * *";
121
        return $this;
122
    }
123
124
    /**
125
     * Run the task every two hours
126
     * @return self
127
     */
128
    public function everyTwoHours(): self
129
    {
130
        $this->expression = '0 */2 * * *';
131
        return $this;
132
    }
133
134
    /**
135
     * Run the task every three hours
136
     * @return self
137
     */
138
    public function everyThreeHours(): self
139
    {
140
        $this->expression = '0 */3 * * *';
141
        return $this;
142
    }
143
144
    /**
145
     * Run the task every four hours
146
     * @return self
147
     */
148
    public function everyFourHours(): self
149
    {
150
        $this->expression = '0 */4 * * *';
151
        return $this;
152
    }
153
154
    /**
155
     * Run the task every six hours
156
     * @return self
157
     */
158
    public function everySixHours(): self
159
    {
160
        $this->expression = '0 */6 * * *';
161
        return $this;
162
    }
163
164
    /**
165
     * Run the task daily
166
     * @return self
167
     */
168
    public function daily(): self
169
    {
170
        $this->expression = '0 0 * * *';
171
        return $this;
172
    }
173
174
    /**
175
     * Run the task daily at a specific time
176
     * @param string $time Format: "HH:MM"
177
     * @return self
178
     */
179
    public function dailyAt(string $time): self
180
    {
181
        [$hour, $minute] = explode(':', $time);
182
        $hour = (int) $hour;
183
        $minute = (int) $minute;
184
        $this->expression = "{$minute} {$hour} * * *";
185
        return $this;
186
    }
187
188
    /**
189
     * Run the task twice daily
190
     * @param int $firstHour
191
     * @param int $secondHour
192
     * @return self
193
     */
194
    public function twiceDaily(int $firstHour = 1, int $secondHour = 13): self
195
    {
196
        $this->expression = "0 {$firstHour},{$secondHour} * * *";
197
        return $this;
198
    }
199
200
    /**
201
     * Run the task weekly
202
     * @return self
203
     */
204
    public function weekly(): self
205
    {
206
        $this->expression = '0 0 * * 0';
207
        return $this;
208
    }
209
210
    /**
211
     * Run the task weekly on a specific day and time
212
     * @param int $dayOfWeek 0-6 (Sunday = 0)
213
     * @param string $time Format: "HH:MM"
214
     * @return self
215
     */
216
    public function weeklyOn(int $dayOfWeek, string $time = '0:00'): self
217
    {
218
        [$hour, $minute] = explode(':', $time);
219
        $hour = (int) $hour;
220
        $minute = (int) $minute;
221
        $this->expression = "{$minute} {$hour} * * {$dayOfWeek}";
222
        return $this;
223
    }
224
225
    /**
226
     * Run the task monthly
227
     * @return self
228
     */
229
    public function monthly(): self
230
    {
231
        $this->expression = '0 0 1 * *';
232
        return $this;
233
    }
234
235
    /**
236
     * Run the task monthly on a specific day and time
237
     * @param int $dayOfMonth
238
     * @param string $time Format: "HH:MM"
239
     * @return self
240
     */
241
    public function monthlyOn(int $dayOfMonth = 1, string $time = '0:00'): self
242
    {
243
        [$hour, $minute] = explode(':', $time);
244
        $hour = (int) $hour;
245
        $minute = (int) $minute;
246
        $this->expression = "{$minute} {$hour} {$dayOfMonth} * *";
247
        return $this;
248
    }
249
250
    /**
251
     * Run the task twice monthly
252
     * @param int $firstDay
253
     * @param int $secondDay
254
     * @param string $time
255
     * @return self
256
     */
257
    public function twiceMonthly(int $firstDay = 1, int $secondDay = 16, string $time = '0:00'): self
258
    {
259
        [$hour, $minute] = explode(':', $time);
260
        $hour = (int) $hour;
261
        $minute = (int) $minute;
262
        $this->expression = "{$minute} {$hour} {$firstDay},{$secondDay} * *";
263
        return $this;
264
    }
265
266
    /**
267
     * Run the task quarterly
268
     * @return self
269
     */
270
    public function quarterly(): self
271
    {
272
        $this->expression = '0 0 1 1-12/3 *';
273
        return $this;
274
    }
275
276
    /**
277
     * Run the task yearly
278
     * @return self
279
     */
280
    public function yearly(): self
281
    {
282
        $this->expression = '0 0 1 1 *';
283
        return $this;
284
    }
285
286
    /**
287
     * Run the task on weekdays
288
     * @return self
289
     */
290
    public function weekdays(): self
291
    {
292
        $this->expression = '0 0 * * 1-5';
293
        return $this;
294
    }
295
296
    /**
297
     * Run the task on weekends
298
     * @return self
299
     */
300
    public function weekends(): self
301
    {
302
        $this->expression = '0 0 * * 0,6';
303
        return $this;
304
    }
305
306
    /**
307
     * Run the task on Mondays
308
     * @return self
309
     */
310
    public function mondays(): self
311
    {
312
        return $this->days(1);
313
    }
314
315
    /**
316
     * Run the task on Tuesdays
317
     * @return self
318
     */
319
    public function tuesdays(): self
320
    {
321
        return $this->days(2);
322
    }
323
324
    /**
325
     * Run the task on Wednesdays
326
     * @return self
327
     */
328
    public function wednesdays(): self
329
    {
330
        return $this->days(3);
331
    }
332
333
    /**
334
     * Run the task on Thursdays
335
     * @return self
336
     */
337
    public function thursdays(): self
338
    {
339
        return $this->days(4);
340
    }
341
342
    /**
343
     * Run the task on Fridays
344
     * @return self
345
     */
346
    public function fridays(): self
347
    {
348
        return $this->days(5);
349
    }
350
351
    /**
352
     * Run the task on Saturdays
353
     * @return self
354
     */
355
    public function saturdays(): self
356
    {
357
        return $this->days(6);
358
    }
359
360
    /**
361
     * Run the task on Sundays
362
     * @return self
363
     */
364
    public function sundays(): self
365
    {
366
        return $this->days(0);
367
    }
368
369
    /**
370
     * Run the task on specific days
371
     * @param int|array $days
372
     * @return self
373
     */
374
    public function days($days): self
375
    {
376
        $days = is_array($days) ? implode(',', $days) : $days;
377
        $this->expression = "0 0 * * {$days}";
378
        return $this;
379
    }
380
381
    /**
382
     * Set the time for the task
383
     * @param string $time Format: "HH:MM"
384
     * @return self
385
     */
386
    public function at(string $time): self
387
    {
388
        [$hour, $minute] = explode(':', $time);
389
        $hour = (int) $hour;
390
        $minute = (int) $minute;
391
392
        // Replace hour and minute in existing expression
393
        $parts = explode(' ', $this->expression);
394
        $parts[0] = (string) $minute;
395
        $parts[1] = (string) $hour;
396
        $this->expression = implode(' ', $parts);
397
398
        return $this;
399
    }
400
401
    /**
402
     * Set custom cron expression
403
     * @param string $expression
404
     * @return self
405
     */
406
    public function cron(string $expression): self
407
    {
408
        $this->expression = $expression;
409
        return $this;
410
    }
411
412
    /**
413
     * Set the callback for the task
414
     * @param callable $callback
415
     * @return self
416
     */
417
    public function call(callable $callback): self
418
    {
419
        $this->callback = $callback;
420
        return $this;
421
    }
422
423
    /**
424
     * Build and return the CronTask
425
     * @return CronTask
426
     * @throws CronException
427
     */
428
    public function build(): CronTask
429
    {
430
        if ($this->callback === null) {
431
            throw new CronException("Task '{$this->name}' must have a callback. Use call() method.");
432
        }
433
434
        if ($this->expression === null) {
435
            throw new CronException("Task '{$this->name}' must have a schedule. Use methods like daily(), hourly(), etc.");
436
        }
437
438
        return new CronTask($this->name, $this->expression, $this->callback);
439
    }
440
441
    /**
442
     * Get the cron expression
443
     * @return string|null
444
     */
445
    public function getExpression(): ?string
446
    {
447
        return $this->expression;
448
    }
449
}
450