From df42f660cf62758ff20e8b1ef6458b091e113ddb Mon Sep 17 00:00:00 2001 From: Mat Fish Date: Sat, 27 Jan 2024 07:36:34 +0200 Subject: [PATCH] refactor --- CHANGELOG.md | 3 + README.md | 13 ++-- src/Schedule/Frequencies.php | 143 ++++++++++++++++------------------- 3 files changed, 76 insertions(+), 83 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8d70dc..49de93f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Release Notes for Console Scheduler +## 1.0.2 - 2024-01-27 +- Refactor + ## 1.0.1 - 2024-01-26 - Pass explicit DateTime object from Craft - Allow testing locally diff --git a/README.md b/README.md index 47a7033..06ae7a3 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,10 @@ Supported frequencies: * `everySixHours()` * `daily()` * `dailyAt($time)` +* `twiceDaily($hour1 = 1,$hour2 = 13)` +* `twiceDailyAt($hour1 = 1,$hour2 = 13, $offset = 0)` * `weekdays()` +* `weedends()` * `days($day1,$day2, $day3, etc)` - specific days of week * `sundays()` * `mondays()` @@ -82,15 +85,15 @@ Supported frequencies: * `fridays()` * `satrudays()` * `weekly()` -* `weeklyOn($dayOfWeek, $time)` +* `weeklyOn($dayOfWeek, $time = '0:0')` * `monthly()` * `monthlyOn($dayOfMonth, $time)` -* `twiceMonthly($firstDay, $secondDay, $time)` -* `lastDayOfMonth($time)` +* `twiceMonthly($firstDay = 1, $secondDay = 16, $time = '0:0')` +* `lastDayOfMonth($time = '0:0')` * `quarterly()` -* `quarterlyOn($dayOfQuarter, $time)` +* `quarterlyOn($dayOfQuarter = 1, $time = '0:0')` * `yearly()` -* `yearlyOn($month, $dayOfMonth, $time)` +* `yearlyOn($month = 1, $dayOfMonth = 1, $time = '0:0')` Methods without a specified time can be followed by an `at` method using fluent syntax. diff --git a/src/Schedule/Frequencies.php b/src/Schedule/Frequencies.php index de641d7..2e84507 100644 --- a/src/Schedule/Frequencies.php +++ b/src/Schedule/Frequencies.php @@ -10,10 +10,10 @@ trait Frequencies /** * The Cron expression representing the event's frequency. * - * @param string $expression + * @param string $expression * @return $this */ - public function cron($expression) + public function cron(string $expression): static { $this->expression = new CronExpression($expression); @@ -25,7 +25,7 @@ public function cron($expression) * * @return $this */ - public function everyMinute() + public function everyMinute(): static { return $this->spliceIntoPosition(1, '*'); } @@ -35,7 +35,7 @@ public function everyMinute() * * @return $this */ - public function everyTwoMinutes() + public function everyTwoMinutes(): static { return $this->spliceIntoPosition(1, '*/2'); } @@ -45,7 +45,7 @@ public function everyTwoMinutes() * * @return $this */ - public function everyThreeMinutes() + public function everyThreeMinutes(): static { return $this->spliceIntoPosition(1, '*/3'); } @@ -55,7 +55,7 @@ public function everyThreeMinutes() * * @return $this */ - public function everyFourMinutes() + public function everyFourMinutes(): static { return $this->spliceIntoPosition(1, '*/4'); } @@ -65,7 +65,7 @@ public function everyFourMinutes() * * @return $this */ - public function everyFiveMinutes() + public function everyFiveMinutes(): static { return $this->spliceIntoPosition(1, '*/5'); } @@ -75,7 +75,7 @@ public function everyFiveMinutes() * * @return $this */ - public function everyTenMinutes() + public function everyTenMinutes(): static { return $this->spliceIntoPosition(1, '*/10'); } @@ -85,7 +85,7 @@ public function everyTenMinutes() * * @return $this */ - public function everyFifteenMinutes() + public function everyFifteenMinutes(): static { return $this->spliceIntoPosition(1, '*/15'); } @@ -95,7 +95,7 @@ public function everyFifteenMinutes() * * @return $this */ - public function everyThirtyMinutes() + public function everyThirtyMinutes(): static { return $this->spliceIntoPosition(1, '0,30'); } @@ -105,7 +105,7 @@ public function everyThirtyMinutes() * * @return $this */ - public function hourly() + public function hourly(): static { return $this->spliceIntoPosition(1, 0); } @@ -113,10 +113,10 @@ public function hourly() /** * Schedule the event to run hourly at a given offset in the hour. * - * @param array|int $offset + * @param int|array $offset * @return $this */ - public function hourlyAt($offset) + public function hourlyAt(int|array $offset): static { $offset = is_array($offset) ? implode(',', $offset) : $offset; @@ -128,7 +128,7 @@ public function hourlyAt($offset) * * @return $this */ - public function everyOddHour() + public function everyOddHour(): static { return $this->spliceIntoPosition(1, 0)->spliceIntoPosition(2, '1-23/2'); } @@ -138,7 +138,7 @@ public function everyOddHour() * * @return $this */ - public function everyTwoHours() + public function everyTwoHours(): static { return $this->spliceIntoPosition(1, 0) ->spliceIntoPosition(2, '*/2'); @@ -149,7 +149,7 @@ public function everyTwoHours() * * @return $this */ - public function everyThreeHours() + public function everyThreeHours(): static { return $this->spliceIntoPosition(1, 0) ->spliceIntoPosition(2, '*/3'); @@ -160,7 +160,7 @@ public function everyThreeHours() * * @return $this */ - public function everyFourHours() + public function everyFourHours(): static { return $this->spliceIntoPosition(1, 0) ->spliceIntoPosition(2, '*/4'); @@ -171,7 +171,7 @@ public function everyFourHours() * * @return $this */ - public function everySixHours() + public function everySixHours(): static { return $this->spliceIntoPosition(1, 0) ->spliceIntoPosition(2, '*/6'); @@ -182,7 +182,7 @@ public function everySixHours() * * @return $this */ - public function daily() + public function daily(): static { return $this->spliceIntoPosition(1, 0) ->spliceIntoPosition(2, 0); @@ -191,10 +191,10 @@ public function daily() /** * Schedule the command at a given time. * - * @param string $time + * @param string $time * @return $this */ - public function at($time) + public function at(string $time): static { return $this->dailyAt($time); } @@ -202,10 +202,10 @@ public function at($time) /** * Schedule the event to run daily at a given time (10:00, 19:30, etc). * - * @param string $time + * @param string $time * @return $this */ - public function dailyAt($time) + public function dailyAt(string $time): static { $segments = explode(':', $time); @@ -216,11 +216,11 @@ public function dailyAt($time) /** * Schedule the event to run twice daily. * - * @param int $first - * @param int $second + * @param int $first + * @param int $second * @return $this */ - public function twiceDaily($first = 1, $second = 13) + public function twiceDaily(int $first = 1, int $second = 13): static { return $this->twiceDailyAt($first, $second, 0); } @@ -228,12 +228,12 @@ public function twiceDaily($first = 1, $second = 13) /** * Schedule the event to run twice daily at a given offset. * - * @param int $first - * @param int $second - * @param int $offset + * @param int $first + * @param int $second + * @param int $offset * @return $this */ - public function twiceDailyAt($first = 1, $second = 13, $offset = 0) + public function twiceDailyAt(int $first = 1, int $second = 13, int $offset = 0): static { $hours = $first.','.$second; @@ -246,7 +246,7 @@ public function twiceDailyAt($first = 1, $second = 13, $offset = 0) * * @return $this */ - public function weekdays() + public function weekdays(): static { return $this->days(Schedule::MONDAY.'-'.Schedule::FRIDAY); } @@ -256,7 +256,7 @@ public function weekdays() * * @return $this */ - public function weekends() : Schedule + public function weekends(): static { return $this->days(Schedule::SATURDAY.','.Schedule::SUNDAY); } @@ -266,7 +266,7 @@ public function weekends() : Schedule * * @return $this */ - public function mondays() : Schedule + public function mondays() : static { return $this->days(Schedule::MONDAY); } @@ -276,7 +276,7 @@ public function mondays() : Schedule * * @return $this */ - public function tuesdays() : Schedule + public function tuesdays() : static { return $this->days(Schedule::TUESDAY); } @@ -286,7 +286,7 @@ public function tuesdays() : Schedule * * @return $this */ - public function wednesdays() : Schedule + public function wednesdays() : static { return $this->days(Schedule::WEDNESDAY); } @@ -296,7 +296,7 @@ public function wednesdays() : Schedule * * @return $this */ - public function thursdays() : Schedule + public function thursdays() : static { return $this->days(Schedule::THURSDAY); } @@ -306,7 +306,7 @@ public function thursdays() : Schedule * * @return $this */ - public function fridays() : Schedule + public function fridays() : static { return $this->days(Schedule::FRIDAY); } @@ -316,7 +316,7 @@ public function fridays() : Schedule * * @return $this */ - public function saturdays() : Schedule + public function saturdays() : static { return $this->days(Schedule::SATURDAY); } @@ -326,7 +326,7 @@ public function saturdays() : Schedule * * @return $this */ - public function sundays() : Schedule + public function sundays() : static { return $this->days(Schedule::SUNDAY); } @@ -336,7 +336,7 @@ public function sundays() : Schedule * * @return $this */ - public function weekly() : Schedule + public function weekly() : static { return $this->spliceIntoPosition(1, 0) ->spliceIntoPosition(2, 0) @@ -347,10 +347,10 @@ public function weekly() : Schedule * Schedule the event to run weekly on a given day and time. * * @param array|mixed $dayOfWeek - * @param string $time + * @param string $time * @return $this */ - public function weeklyOn($dayOfWeek, $time = '0:0') : Schedule + public function weeklyOn(mixed $dayOfWeek, string $time = '0:0') : static { $this->dailyAt($time); @@ -362,7 +362,7 @@ public function weeklyOn($dayOfWeek, $time = '0:0') : Schedule * * @return $this */ - public function monthly() : Schedule + public function monthly() : static { return $this->spliceIntoPosition(1, 0) ->spliceIntoPosition(2, 0) @@ -372,11 +372,11 @@ public function monthly() : Schedule /** * Schedule the event to run monthly on a given day and time. * - * @param int $dayOfMonth - * @param string $time + * @param int $dayOfMonth + * @param string $time * @return $this */ - public function monthlyOn($dayOfMonth = 1, $time = '0:0') : Schedule + public function monthlyOn(int $dayOfMonth = 1, string $time = '0:0') : static { $this->dailyAt($time); @@ -386,12 +386,12 @@ public function monthlyOn($dayOfMonth = 1, $time = '0:0') : Schedule /** * Schedule the event to run twice monthly at a given time. * - * @param int $first - * @param int $second - * @param string $time + * @param int $first + * @param int $second + * @param string $time * @return $this */ - public function twiceMonthly($first = 1, $second = 16, $time = '0:0') : Schedule + public function twiceMonthly(int $first = 1, int $second = 16, string $time = '0:0') : static { $daysOfMonth = $first.','.$second; @@ -403,10 +403,10 @@ public function twiceMonthly($first = 1, $second = 16, $time = '0:0') : Schedule /** * Schedule the event to run on the last day of the month. * - * @param string $time + * @param string $time * @return $this */ - public function lastDayOfMonth($time = '0:0') : Schedule + public function lastDayOfMonth(string $time = '0:0') : static { $this->dailyAt($time); @@ -418,7 +418,7 @@ public function lastDayOfMonth($time = '0:0') : Schedule * * @return $this */ - public function quarterly() : Schedule + public function quarterly() : static { return $this->spliceIntoPosition(1, 0) ->spliceIntoPosition(2, 0) @@ -429,11 +429,11 @@ public function quarterly() : Schedule /** * Schedule the event to run quarterly on a given day and time. * - * @param int $dayOfQuarter - * @param int $time + * @param int $dayOfQuarter + * @param int|string $time * @return $this */ - public function quarterlyOn($dayOfQuarter = 1, $time = '0:0') : Schedule + public function quarterlyOn(int $dayOfQuarter = 1, int|string $time = '0:0') : static { $this->dailyAt($time); @@ -446,7 +446,7 @@ public function quarterlyOn($dayOfQuarter = 1, $time = '0:0') : Schedule * * @return $this */ - public function yearly() : Schedule + public function yearly() : static { return $this->spliceIntoPosition(1, 0) ->spliceIntoPosition(2, 0) @@ -457,12 +457,12 @@ public function yearly() : Schedule /** * Schedule the event to run yearly on a given month, day, and time. * - * @param int $month - * @param int|string $dayOfMonth - * @param string $time + * @param int $month + * @param int|string $dayOfMonth + * @param string $time * @return $this */ - public function yearlyOn($month = 1, $dayOfMonth = 1, $time = '0:0') : Schedule + public function yearlyOn(int $month = 1, int|string $dayOfMonth = 1, string $time = '0:0') : static { $this->dailyAt($time); @@ -476,34 +476,21 @@ public function yearlyOn($month = 1, $dayOfMonth = 1, $time = '0:0') : Schedule * @param array|mixed $days * @return $this */ - public function days($days) : Schedule + public function days(mixed $days) : static { $days = is_array($days) ? $days : func_get_args(); return $this->spliceIntoPosition(5, implode(',', $days)); } - /** - * Set the timezone the date should be evaluated on. - * - * @param \DateTimeZone|string $timezone - * @return $this - */ - public function timezone($timezone) : Schedule - { - $this->timezone = $timezone; - - return $this; - } - /** * Splice the given value into the given position of the expression. * - * @param int $position - * @param string $value + * @param int $position + * @param string $value * @return $this */ - protected function spliceIntoPosition($position, $value) : Schedule + protected function spliceIntoPosition(int $position, string $value) : static { $segments = preg_split("/\s+/", $this->expression);