Skip to content

Commit

Permalink
Adds repeatingAt functionality to hourly
Browse files Browse the repository at this point in the history
  • Loading branch information
garethellis36 committed Aug 31, 2016
1 parent 080feaf commit aa92dd3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ can be used to create a new instance of `Hourly`.

If you call `hourly()` by itself, you'll get a crontab schedule of "run hourly on the hour". You can specify
the minutes past the hour to run at by using the `at()` method. This method takes either a number (0-59) or
one of the following text strings: `quarter past`, `half past` or `quarter to`.
one of the following text strings: `on the hour`, `quarter past`, `half past` or `quarter to`.

You can use the `repeatingAt` method to run a task multiple times in an hour. This method takes the same
argument as `at()`, i.e. a number (0-59) or a text string from the above list.

``` php
use function Garethellis\CrontabScheduleGenerator\hourly;
Expand All @@ -35,11 +38,11 @@ If you call `hourly()` by itself, you'll get a crontab schedule of "run hourly o
echo hourly()->at("20");
//outputs "20 * * * *" (i.e. run hourly at twenty past the hour)

echo daily()->at("half past");
echo hourly()->at("half past");
//outputs "30 * * * *" (i.e. run hourly at half past the hour)

echo daily()->at("9:30")->repeatingAt("10")->repeatingAt("11");
//outputs "30 9,10,11 * * *" (i.e. run daily at 9:30, 10:30 and 11:30)
echo hourly()->at("on the hour")->repeatingAt("quarter past")->repeatingAt("half past")->repeatingAt("quarter to");
//outputs "0,15,30,45 * * * *" (i.e. run every 15 minutes)
```

### Daily
Expand Down
2 changes: 1 addition & 1 deletion src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ function monthly()
function hourly()
{
return new Hourly();
}
}
30 changes: 27 additions & 3 deletions src/Hourly.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,49 @@ class Hourly
private $mins = 0;

private $textDescriptions = [
"on the hour" => "0",
"quarter past" => "15",
"half past" => "30",
"quarter to" => "45",
];

public function __toString()
{
return sprintf("%s * * * *", $this->mins);
if (!is_array($this->mins)) {
return sprintf("%s * * * *", $this->mins);
}
return sprintf("%s * * * *", implode(",", $this->mins));
}

public function at($minutes)
{
if (!is_numeric($minutes)) {
Assertion::choice($minutes, array_keys($this->textDescriptions));
$minutes = $this->textDescriptions[$minutes];
} else {
Assertion::range($minutes, 0, 59);
}

$this->mins = $minutes;
return $this;
}

public function repeatingAt($minutes)
{
if (!is_numeric($minutes)) {
Assertion::choice($minutes, array_keys($this->textDescriptions));
$minutes = $this->textDescriptions[$minutes];
} else {
Assertion::range($minutes, 0, 59);
}

$this->mins = $this->textDescriptions[$minutes];
if (!is_array($this->mins)) {
$this->mins = [$this->mins];
}

if (!in_array($minutes, $this->mins)) {
$this->mins[] = $minutes;
}
return $this;
}
}
}
11 changes: 11 additions & 0 deletions tests/HourlyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,15 @@ public function test_it_can_return_an_hourly_crontab_with_minutes_specified_nume
is(equalTo("23 * * * *"))
);
}

/**
* @return void
*/
public function test_it_can_repeat_after_a_given_number_of_minutes()
{
assertThat(
(string)hourly()->at("quarter past")->repeatingAt("half past")->repeatingAt("quarter to")->repeatingAt("50"),
is(equalTo("15,30,45,50 * * * *"))
);
}
}

0 comments on commit aa92dd3

Please sign in to comment.