Skip to content

Commit 7ca395d

Browse files
ddevsrlonnieezell
andauthored
feat: frequency of month (#159)
* feat: frequency of month * refactor: change method betweenMonths * docs: added frequency option everyMonth and betweenMonths * Update docs/basic-usage.md --------- Co-authored-by: Lonnie Ezell <lonnieje@gmail.com>
1 parent 5843d52 commit 7ca395d

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

docs/basic-usage.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ There are a number of ways available to specify how often the task is called.
110110
| `->saturdays('3:15am')` | Runs every Saturday at midnight, unless time passed in. |
111111
| `->monthly('12:21pm')` | Runs the first day of every month at 12:00am unless time passed in. |
112112
| `->daysOfMonth([1,15])` | Runs only on days 1 and 15. |
113+
| `->everyMonth(4)` | Runs every 4 months. |
114+
| `->betweenMonths(4,7)` | Runs between months 4 and 7. |
113115
| `->months([1,7])` | Runs only on January and July. |
114116
| `->quarterly('5:00am')` | Runs the first day of each quarter (Jan 1, Apr 1, July 1, Oct 1) |
115117
| `->yearly('12:34am')` | Runs the first day of the year. |

src/FrequenciesTrait.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,35 @@ public function daysOfMonth($days)
352352
return $this;
353353
}
354354

355+
/**
356+
* Set the execution time to every month or every x months.
357+
*
358+
* @param int|string|null $month When set, specifies that the job will be run every $month months
359+
*
360+
* @return $this
361+
*/
362+
public function everyMonth($month = null)
363+
{
364+
$this->expression['month'] = null === $month ? '*' : '*/' . $month;
365+
366+
return $this;
367+
}
368+
369+
/**
370+
* Runs on specific range of months
371+
*
372+
* @param int $from Month [1-12]
373+
* @param int $to Month [1-12]
374+
*
375+
* @return $this
376+
*/
377+
public function betweenMonths(int $from, int $to)
378+
{
379+
$this->expression['month'] = $from . '-' . $to;
380+
381+
return $this;
382+
}
383+
355384
/**
356385
* Runs on specific months
357386
*
@@ -381,7 +410,8 @@ public function quarterly(?string $time = null)
381410
$this->expression['min'] = $min;
382411
$this->expression['hour'] = $hour;
383412
$this->expression['dayOfMonth'] = 1;
384-
$this->expression['month'] = '*/3';
413+
414+
$this->everyMonth(3);
385415

386416
return $this;
387417
}

tests/unit/FrequenciesTraitTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,20 @@ public function testHours()
298298
$this->assertSame('* 12,16 * * *', $this->class->getExpression());
299299
}
300300

301+
public function testEveryMonth()
302+
{
303+
$this->class->everyMonth();
304+
305+
$this->assertSame('* * * * *', $this->class->getExpression());
306+
}
307+
308+
public function testEveryMonthWithParameter()
309+
{
310+
$this->class->everyMonth(4);
311+
312+
$this->assertSame('* * * */4 *', $this->class->getExpression());
313+
}
314+
301315
public function testEveryMinute()
302316
{
303317
$this->class->everyMinute();
@@ -346,4 +360,11 @@ public function testMonths()
346360

347361
$this->assertSame('* * * 1,7 *', $this->class->getExpression());
348362
}
363+
364+
public function testBetweenMonths()
365+
{
366+
$this->class->betweenMonths(1, 7);
367+
368+
$this->assertSame('* * * 1-7 *', $this->class->getExpression());
369+
}
349370
}

0 commit comments

Comments
 (0)