Scheduler::weekly()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 1
1
<?php namespace Indatus\Dispatcher\Drivers\DateTime;
2
3
/**
4
 * This file is part of Dispatcher
5
 *
6
 * (c) Ben Kuhl <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
use Indatus\Dispatcher\Day;
13
use Indatus\Dispatcher\Month;
14
use Indatus\Dispatcher\Scheduling\BadScheduleException;
15
use Indatus\Dispatcher\Scheduling\Schedulable;
16
17
class Scheduler extends Schedulable
18
{
19
    const ANY = '*';
20
21
    const NONE = '-';
22
23
    private $scheduleWeek = self::NONE;
24
    private $scheduleDayOfWeek = self::ANY;
25
    private $scheduleMonth = self::ANY;
26
    private $scheduleDayOfMonth = self::ANY;
27
    private $scheduleHour = self::ANY;
28
    private $scheduleMinute = self::ANY;
29
30
    /**
31
     * Get the scheduling definition in a readable way
32
     *
33
     * @return string
34
     */
35 23
    public function getSchedule()
36
    {
37 23
        return implode(' ', [
38 23
                $this->getScheduleMonth(),
39 23
                $this->getScheduleWeek(),
40 23
                $this->getScheduleDayOfMonth(),
41 23
                $this->getScheduleDayOfWeek(),
42 23
                $this->getScheduleHour(),
43 23
                $this->getScheduleMinute()
44 23
            ]);
45
    }
46
47
    /**
48
     * Get a valid cron schedule
49
     *
50
     * @return string
51
     */
52 1
    public function getCronSchedule()
53
    {
54 1
        $schedules = explode(' ', $this->getSchedule());
55
56
        //remove week
57 1
        unset($schedules[1]);
58
59 1
        return implode(' ', $schedules);
60
    }
61
62
    /**
63
     * Manually set a command's execution schedule.  Parameter
64
     * order follows standard cron syntax
65
     *
66
     * @param int|array        $minute
67
     * @param int|array        $hour
68
     * @param int|array        $dayOfMonth
69
     * @param int|array        $month
70
     * @param int|array        $dayOfWeek
71
     * @param string|int|array $week
72
     *
73
     * @return $this
74
     */
75 2
    public function setSchedule($minute, $hour, $dayOfMonth, $month, $dayOfWeek, $week = self::NONE)
76
    {
77 2
        $month = $this->parseTimeParameter($month);
78 2
        $week = $this->parseTimeParameter($week);
79 2
        $dayOfMonth = $this->parseTimeParameter($dayOfMonth);
80 2
        $dayOfWeek = $this->parseTimeParameter($dayOfWeek);
81 2
        $hour = $this->parseTimeParameter($hour);
82 2
        $minute = $this->parseTimeParameter($minute);
83
84 2
        $this->setScheduleMonth($month);
85 2
        $this->setScheduleWeek($week);
86 2
        $this->setScheduleDayOfMonth($dayOfMonth);
87 2
        $this->setScheduleDayOfWeek($dayOfWeek);
88 2
        $this->setScheduleHour($hour);
89 2
        $this->setScheduleMinute($minute);
90
91 2
        return $this;
92
    }
93
94
    /**
95
     * Run once a year at midnight in the morning of January 1
96
     *
97
     * @return $this
98
     */
99 1
    public function yearly()
100
    {
101 1
        $this->setScheduleMonth(1);
102 1
        $this->setScheduleWeek(self::NONE);
103 1
        $this->setScheduleDayOfMonth(1);
104 1
        $this->setScheduleDayOfWeek(self::ANY);
105 1
        $this->setScheduleHour(0);
106 1
        $this->setScheduleMinute(0);
107
108 1
        return $this;
109
    }
110
111
    /**
112
     * Run once a quarter at the beginning of the quarter
113
     *
114
     * @return $this
115
     */
116 1
    public function quarterly()
117
    {
118 1
        $months = [Month::JANUARY, Month::APRIL, Month::JULY, Month::OCTOBER];
119 1
        $this->setScheduleMonth($this->parseTimeParameter($months));
120 1
        $this->setScheduleWeek(self::NONE);
121 1
        $this->setScheduleDayOfMonth(1);
122 1
        $this->setScheduleDayOfWeek(self::ANY);
123 1
        $this->setScheduleHour(0);
124 1
        $this->setScheduleMinute(0);
125
126 1
        return $this;
127
    }
128
129
    /**
130
     * Set the days of the month under which this command will run
131
     * @param  int|array $daysOfTheMonth
132
     * @return $this
133
     */
134 2
    public function daysOfTheMonth($daysOfTheMonth)
135
    {
136 2
        $this->setScheduleDayOfMonth($this->parseTimeParameter($daysOfTheMonth));
137
138 2
        return $this;
139
    }
140
141
    /**
142
     * Run once a month at midnight in the morning of the first day of the month
143
     *
144
     * @return $this
145
     */
146 1 View Code Duplication
    public function monthly()
0 ignored issues
show
Duplication introduced by
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...
147
    {
148 1
        $this->setScheduleMonth(self::ANY);
149 1
        $this->setScheduleWeek(self::NONE);
150 1
        $this->setScheduleDayOfMonth(1);
151 1
        $this->setScheduleDayOfWeek(self::ANY);
152 1
        $this->setScheduleHour(0);
153 1
        $this->setScheduleMinute(0);
154
155 1
        return $this;
156
    }
157
158
    /**
159
     * Set the months under which this command will run
160
     * @param  int|array $monthsIntoTheYear
161
     * @return $this
162
     */
163 2
    public function months($monthsIntoTheYear)
164
    {
165 2
        $this->setScheduleMonth($this->parseTimeParameter($monthsIntoTheYear));
166
167 2
        return $this;
168
    }
169
170
    /**
171
     * Run a command every X months
172
     * @param  int   $months
173
     * @return $this
174
     */
175 1
    public function everyMonths($months)
176
    {
177 1
        $this->setScheduleMonth('*/'.$months);
178
179 1
        return $this;
180
    }
181
182
    /**
183
     * Run once every odd week at midnight on Sunday morning
184
     *
185
     * @return $this
186
     */
187 1 View Code Duplication
    public function everyOddWeek()
0 ignored issues
show
Duplication introduced by
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...
188
    {
189 1
        $this->setScheduleMonth(self::ANY);
190 1
        $this->setScheduleWeek('odd');
191 1
        $this->setScheduleDayOfMonth(self::ANY);
192 1
        $this->setScheduleDayOfWeek(self::ANY);
193 1
        $this->setScheduleHour(0);
194 1
        $this->setScheduleMinute(0);
195
196 1
        return $this;
197
    }
198
199
    /**
200
     * Run once every even week at midnight on Sunday morning
201
     *
202
     * @return $this
203
     */
204 1 View Code Duplication
    public function everyEvenWeek()
0 ignored issues
show
Duplication introduced by
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...
205
    {
206 1
        $this->setScheduleMonth(self::ANY);
207 1
        $this->setScheduleWeek('even');
208 1
        $this->setScheduleDayOfMonth(self::ANY);
209 1
        $this->setScheduleDayOfWeek(self::ANY);
210 1
        $this->setScheduleHour(0);
211 1
        $this->setScheduleMinute(0);
212
213 1
        return $this;
214
    }
215
216
    /**
217
     * Run once a week at midnight on Sunday morning
218
     *
219
     * @return $this
220
     */
221 1 View Code Duplication
    public function weekly()
0 ignored issues
show
Duplication introduced by
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...
222
    {
223 1
        $this->setScheduleMonth(self::ANY);
224 1
        $this->setScheduleWeek(self::ANY);
225 1
        $this->setScheduleDayOfMonth(self::ANY);
226 1
        $this->setScheduleDayOfWeek(Day::SUNDAY);
227 1
        $this->setScheduleHour(0);
228 1
        $this->setScheduleMinute(0);
229
230 1
        return $this;
231
    }
232
233
    /**
234
     * Run on the given week of each month
235
     *
236
     * @param int|array $week
237
     *
238
     * @throws BadScheduleException
239
     *
240
     * @return $this
241
     */
242 1
    public function week($week)
243
    {
244 1
        $this->setScheduleWeek($this->parseTimeParameter($week));
245
246 1
        return $this;
247
    }
248
249
    /**
250
     * Run on the given week of each month
251
     *
252
     * @param int|array $week
253
     *
254
     * @throws BadScheduleException
255
     * @return $this
256
     */
257 1 View Code Duplication
    public function weekOfYear($week)
0 ignored issues
show
Duplication introduced by
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...
258
    {
259 1
        $this->setScheduleMonth(self::NONE);
260 1
        $this->setScheduleWeek($this->parseTimeParameter($week));
261 1
        $this->setScheduleDayOfMonth(self::ANY);
262 1
        $this->setScheduleDayOfWeek(Day::SUNDAY);
263 1
        $this->setScheduleHour(0);
264 1
        $this->setScheduleMinute(0);
265
266 1
        return $this;
267
    }
268
269
    /**
270
     * Run once a day at midnight
271
     *
272
     * @return $this
273
     */
274 1 View Code Duplication
    public function daily()
0 ignored issues
show
Duplication introduced by
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...
275
    {
276 1
        $this->setScheduleMinute(0);
277 1
        $this->setScheduleHour(0);
278 1
        $this->setScheduleDayOfMonth(self::ANY);
279 1
        $this->setScheduleMonth(self::ANY);
280 1
        $this->setScheduleDayOfWeek(self::ANY);
281
282 1
        return $this;
283
    }
284
285
    /**
286
     * Set the days of the week under which this command will run
287
     * @param  int|array $daysOfTheWeek
288
     * @return $this
289
     */
290 5
    public function daysOfTheWeek($daysOfTheWeek)
291
    {
292 5
        $this->setScheduleDayOfWeek($this->parseTimeParameter($daysOfTheWeek));
293
294 5
        return $this;
295
    }
296
297
    /**
298
     * Run every weekday
299
     * @return $this
300
     */
301 3
    public function everyWeekday()
302
    {
303 3
        $this->daysOfTheWeek(Day::MONDAY.'-'.Day::FRIDAY);
304
305 3
        return $this;
306
    }
307
308
    /**
309
     * Run once an hour at the beginning of the hour
310
     *
311
     * @return $this
312
     */
313 1 View Code Duplication
    public function hourly()
0 ignored issues
show
Duplication introduced by
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...
314
    {
315 1
        $this->setScheduleMinute(0);
316 1
        $this->setScheduleHour(self::ANY);
317 1
        $this->setScheduleDayOfMonth(self::ANY);
318 1
        $this->setScheduleMonth(self::ANY);
319 1
        $this->setScheduleDayOfWeek(self::ANY);
320
321 1
        return $this;
322
    }
323
324
    /**
325
     * Set the hours under which this command will run
326
     * @param  int|array $hoursIntoTheDay
327
     * @return $this
328
     */
329 2
    public function hours($hoursIntoTheDay)
330
    {
331 2
        $this->setScheduleHour($this->parseTimeParameter($hoursIntoTheDay));
332
333 2
        return $this;
334
    }
335
336
    /**
337
     * Run a command every X hours
338
     * @param  int   $hours
339
     * @return $this
340
     */
341 1
    public function everyHours($hours)
342
    {
343 1
        $this->setScheduleHour('*/'.$hours);
344
345 1
        return $this;
346
    }
347
348
    /**
349
     * Set the minutes under which this command will run
350
     * @param  int|array $minutesIntoTheHour
351
     * @return $this
352
     */
353 2
    public function minutes($minutesIntoTheHour)
354
    {
355 2
        $this->setScheduleMinute($this->parseTimeParameter($minutesIntoTheHour));
356
357 2
        return $this;
358
    }
359
360
    /**
361
     * Run a command every X minutes
362
     * @param  int   $minutes
363
     * @return $this
364
     */
365 1
    public function everyMinutes($minutes)
366
    {
367 1
        $minutesSchedule = self::ANY;
368 1
        if ($minutes != 1) {
369 1
            $minutesSchedule .= '/'.$minutes;
370 1
        }
371
372 1
        $this->setScheduleMinute($minutesSchedule);
373
374 1
        return $this;
375
    }
376
377
    /**
378
     * @inheritDoc
379
     * @return Scheduler
380
     */
381 3
    public function args(array $arguments)
382
    {
383 3
        return parent::args($arguments);
384
    }
385
386
    /**
387
     * @inheritDoc
388
     * @return Scheduler
389
     */
390 2
    public function opts(array $options)
391
    {
392 2
        return parent::opts($options);
393
    }
394
395
    /**
396
     * If an array of values is used, convert it
397
     * to a comma separated value.
398
     */
399 14
    protected function parseTimeParameter($parameter)
400
    {
401 14
        if (is_array($parameter)) {
402 8
            return implode(',', $parameter);
403
        }
404
405 10
        return $parameter;
406
    }
407
408
    /**
409
     * @param string $scheduleWeek
410
     */
411 10
    protected function setScheduleWeek($scheduleWeek)
412
    {
413 10
        $this->scheduleWeek = $scheduleWeek;
414 10
    }
415
416
    /**
417
     * @return string
418
     */
419 23
    public function getScheduleWeek()
420
    {
421 23
        return $this->scheduleWeek;
422
    }
423
424
    /**
425
     * @param string $scheduleDayOfMonth
426
     */
427 13
    protected function setScheduleDayOfMonth($scheduleDayOfMonth)
428
    {
429 13
        $this->scheduleDayOfMonth = $scheduleDayOfMonth;
430 13
    }
431
432
    /**
433
     * @return string
434
     */
435 23
    public function getScheduleDayOfMonth()
436
    {
437 23
        return $this->scheduleDayOfMonth;
438
    }
439
440
    /**
441
     * @param string $scheduleDayOfWeek
442
     */
443 16
    protected function setScheduleDayOfWeek($scheduleDayOfWeek)
444
    {
445 16
        $this->scheduleDayOfWeek = $scheduleDayOfWeek;
446 16
    }
447
448
    /**
449
     * @return string
450
     */
451 24
    public function getScheduleDayOfWeek()
452
    {
453 24
        return $this->scheduleDayOfWeek;
454
    }
455
456
    /**
457
     * @param string $scheduleHour
458
     */
459 14
    protected function setScheduleHour($scheduleHour)
460
    {
461 14
        $this->scheduleHour = $scheduleHour;
462 14
    }
463
464
    /**
465
     * @return string
466
     */
467 23
    public function getScheduleHour()
468
    {
469 23
        return $this->scheduleHour;
470
    }
471
472
    /**
473
     * @param string $scheduleMinute
474
     */
475 14
    protected function setScheduleMinute($scheduleMinute)
476
    {
477 14
        $this->scheduleMinute = $scheduleMinute;
478 14
    }
479
480
    /**
481
     * @return string
482
     */
483 23
    public function getScheduleMinute()
484
    {
485 23
        return $this->scheduleMinute;
486
    }
487
488
    /**
489
     * @param string $scheduleMonth
490
     */
491 14
    protected function setScheduleMonth($scheduleMonth)
492
    {
493 14
        $this->scheduleMonth = $scheduleMonth;
494 14
    }
495
496
    /**
497
     * @return string
498
     */
499 23
    public function getScheduleMonth()
500
    {
501 23
        return $this->scheduleMonth;
502
    }
503
504
    /**
505
     * @return string
506
     */
507 1
    public function __toString()
508
    {
509 1
        return $this->getSchedule();
510
    }
511
}
512