From 757a79e90d077b6bf9ebad895adcadb69b68ff11 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 9 Mar 2016 23:14:46 +0100 Subject: [PATCH] Draft restructure for better OS usage --- Utils/TaskSchedule/Cron.php | 5 ++ Utils/TaskSchedule/CronJob.php | 5 +- Utils/TaskSchedule/Interval.php | 85 +++++++++++------------- Utils/TaskSchedule/Schedule.php | 2 +- Utils/TaskSchedule/ScheduleInterface.php | 2 + Utils/TaskSchedule/SchedulerFactory.php | 20 ++++++ Utils/TaskSchedule/TaskFactory.php | 20 ++++++ Utils/TaskSchedule/TaskScheduler.php | 5 ++ 8 files changed, 94 insertions(+), 50 deletions(-) create mode 100644 Utils/TaskSchedule/SchedulerFactory.php create mode 100644 Utils/TaskSchedule/TaskFactory.php diff --git a/Utils/TaskSchedule/Cron.php b/Utils/TaskSchedule/Cron.php index 11bce8655..e6a5785df 100644 --- a/Utils/TaskSchedule/Cron.php +++ b/Utils/TaskSchedule/Cron.php @@ -58,4 +58,9 @@ class Cron implements ScheduleInterface { // TODO: Implement set() method. } + + public function save() + { + + } } diff --git a/Utils/TaskSchedule/CronJob.php b/Utils/TaskSchedule/CronJob.php index ba621f634..c03a511b5 100644 --- a/Utils/TaskSchedule/CronJob.php +++ b/Utils/TaskSchedule/CronJob.php @@ -38,9 +38,10 @@ class CronJob implements TaskInterface private $interval = null; private $command = ''; - public function __construct(string $cronjob) + public function __construct(Interval $interval = null, $cmd = '') { - + $this->interval = $interval; + $this->cmd = $cmd; } public function setInterval(Interval $interval) diff --git a/Utils/TaskSchedule/Interval.php b/Utils/TaskSchedule/Interval.php index 596554623..08f21699b 100644 --- a/Utils/TaskSchedule/Interval.php +++ b/Utils/TaskSchedule/Interval.php @@ -29,6 +29,10 @@ namespace phpOMS\Utils\TaskSchedule; class Interval { + private $start = null; + + private $end = null; + /** * Minute. * @@ -87,6 +91,8 @@ class Interval */ public function __construct(string $interval = null) { + $this->start = new \DateTime('now'); + if (isset($interval)) { $this->parse($interval); } @@ -102,7 +108,14 @@ class Interval */ private function parse(string $interval) { - $elements = explode(' ', $interval); + $elements = explode(' ', trim($interval)); + + $this->minute = $this->parseMinute($elements[0]); + $this->hour = $this->parseHour($elements[1]); + $this->dayOfMonth = $this->parseDayOfMonth($elements[2]); + $this->month = $this->parseMonth($elements[3]); + $this->dayOfWeek = $this->parseDayOfWeek($elements[4]); + $this->year = $this->parseYear($elements[5]); } /** @@ -195,6 +208,24 @@ class Interval } + public function setStart(\DateTime $start) { + $this->start = $start; + } + + public function getStart() : \DateTime + { + return $this->start; + } + + public function getEnd() + { + return $this->end; + } + + public function setEnd(\DateTime $end) { + $this->end = $end; + } + /** * Set mintue. * @@ -209,12 +240,10 @@ class Interval * @since 1.0.0 * @author Dennis Eichhorn */ - public function setMinute(array $minute, int $start = 0, int $end = 0, int $step = 0, bool $any = false) + public function setMinute(array $minute, int $step = 0, bool $any = false) { if ($this->validateMinute($arr = [ 'minutes' => $minute, - 'start' => $start, - 'end' => $end, 'step' => $step, 'any' => $any, ]) @@ -239,12 +268,10 @@ class Interval * @since 1.0.0 * @author Dennis Eichhorn */ - public function setHour(array $hour, int $start = 0, int $end = 0, int $step = 0, bool $any = false) + public function setHour(array $hour, int $step = 0, bool $any = false) { if ($this->validateHour($arr = [ 'hours' => $hour, - 'start' => $start, - 'end' => $end, 'step' => $step, 'any' => $any, ]) @@ -271,12 +298,10 @@ class Interval * @since 1.0.0 * @author Dennis Eichhorn */ - public function setDayOfMonth(array $dayOfMonth, int $start = 0, int $end = 0, int $step = 0, bool $any = false, bool $last = false, int $nearest = 0) + public function setDayOfMonth(array $dayOfMonth, int $step = 0, bool $any = false, bool $last = false, int $nearest = 0) { if ($this->validateDayOfMonth($arr = [ 'dayOfMonth' => $dayOfMonth, - 'start' => $start, - 'end' => $end, 'step' => $step, 'any' => $any, 'last' => $last, @@ -303,12 +328,10 @@ class Interval * @since 1.0.0 * @author Dennis Eichhorn */ - public function setMonth(array $month, int $start = 0, int $end = 0, int $step = 0, bool $any = false) + public function setMonth(array $month, int $step = 0, bool $any = false) { if ($this->validateMonth($arr = [ 'month' => $month, - 'start' => $start, - 'end' => $end, 'step' => $step, 'any' => $any, ]) @@ -334,12 +357,10 @@ class Interval * @since 1.0.0 * @author Dennis Eichhorn */ - public function setDayOfWeek(array $dayOfWeek, int $start = 0, int $end = 0, int $step = 0, bool $any = false, bool $last = false) + public function setDayOfWeek(array $dayOfWeek, int $step = 0, bool $any = false, bool $last = false) { if ($this->validateDayOfWeek($arr = [ 'dayOfWeek' => $dayOfWeek, - 'start' => $start, - 'end' => $end, 'step' => $step, 'any' => $any, 'last' => $last, @@ -365,12 +386,10 @@ class Interval * @since 1.0.0 * @author Dennis Eichhorn */ - public function setYear(array $year, int $start = 0, int $end = 0, int $step = 0, bool $any = false) + public function setYear(array $year, int $step = 0, bool $any = false) { if ($this->validateYear($arr = [ 'year' => $year, - 'start' => $start, - 'end' => $end, 'step' => $step, 'any' => $any, ]) @@ -475,8 +494,6 @@ class Interval if ($minute > 59 || $minute < 0) return false; } - if ($array['start'] > 59 || $array['start'] < 0) return false; - if ($array['end'] > 59 || $array['end'] < 0) return false; if ($array['step'] > 59 || $array['step'] < 0) return false; return true; @@ -498,8 +515,6 @@ class Interval if ($hour > 23 || $hour < 0) return false; } - if ($array['start'] > 23 || $array['start'] < 0) return false; - if ($array['end'] > 23 || $array['end'] < 0) return false; if ($array['step'] > 23 || $array['step'] < 0) return false; return true; @@ -521,8 +536,6 @@ class Interval if ($dayOfMonth > 31 || $dayOfMonth < 1) return false; } - if ($array['start'] > 31 || $array['start'] < 1) return false; - if ($array['end'] > 31 || $array['end'] < 1) return false; if ($array['step'] > 31 || $array['step'] < 1) return false; if ($array['nearest'] > 31 || $array['nearest'] < 1) return false; @@ -545,8 +558,6 @@ class Interval if ($month > 12 || $month < 1) return false; } - if ($array['start'] > 12 || $array['start'] < 1) return false; - if ($array['end'] > 12 || $array['end'] < 1) return false; if ($array['step'] > 12 || $array['step'] < 1) return false; return true; @@ -568,8 +579,6 @@ class Interval if ($dayOfWeek > 7 || $dayOfWeek < 1) return false; } - if ($array['start'] > 7 || $array['start'] < 1) return false; - if ($array['end'] > 7 || $array['end'] < 1) return false; if ($array['step'] > 5 || $array['step'] < 1) return false; return true; @@ -603,9 +612,6 @@ class Interval /* Parsing minutes */ if (($count = count($this->minute['minutes'])) > 0) { $minute = implode(',', $this->minute['minutes']); - } elseif ($this->minute['start'] !== 0 && $this->minute['end']) { - $minute = $this->minute['start'] . '-' . $this->minute['end']; - $count = 2; } else { $minute = '*'; $count = 1; @@ -618,9 +624,6 @@ class Interval /* Parsing hours */ if (($count = count($this->hour['hours'])) > 0) { $hour = implode(',', $this->hour['hours']); - } elseif ($this->hour['start'] !== 0 && $this->hour['end']) { - $hour = $this->hour['start'] . '-' . $this->hour['end']; - $count = 2; } else { $hour = '*'; $count = 1; @@ -633,9 +636,6 @@ class Interval /* Parsing day of month */ if (($count = count($this->dayOfMonth['dayOfMonth'])) > 0) { $dayOfMonth = implode(',', $this->dayOfMonth['dayOfMonth']); - } elseif ($this->dayOfMonth['start'] !== 0 && $this->dayOfMonth['end']) { - $dayOfMonth = $this->dayOfMonth['start'] . '-' . $this->dayOfMonth['end']; - $count = 2; } else { $dayOfMonth = '*'; $count = 1; @@ -652,9 +652,6 @@ class Interval /* Parsing month */ if (($count = count($this->month['month'])) > 0) { $month = implode(',', $this->month['month']); - } elseif ($this->month['start'] !== 0 && $this->month['end']) { - $month = $this->month['start'] . '-' . $this->month['end']; - $count = 2; } else { $month = '*'; $count = 1; @@ -667,9 +664,6 @@ class Interval /* Parsing day of week */ if (($count = count($this->dayOfWeek['dayOfWeek'])) > 0) { $dayOfWeek = implode(',', $this->dayOfWeek['dayOfWeek']); - } elseif ($this->dayOfWeek['start'] !== 0 && $this->dayOfWeek['end']) { - $dayOfWeek = $this->dayOfWeek['start'] . '-' . $this->dayOfWeek['end']; - $count = 2; } else { $dayOfWeek = '*'; $count = 1; @@ -686,9 +680,6 @@ class Interval /* Parsing year */ if (($count = count($this->year['year'])) > 0) { $year = implode(',', $this->year['year']); - } elseif ($this->year['start'] !== 0 && $this->year['end']) { - $year = $this->year['start'] . '-' . $this->year['end']; - $count = 2; } else { $year = '*'; $count = 1; diff --git a/Utils/TaskSchedule/Schedule.php b/Utils/TaskSchedule/Schedule.php index 7df7c3f9b..08172c4dc 100644 --- a/Utils/TaskSchedule/Schedule.php +++ b/Utils/TaskSchedule/Schedule.php @@ -38,7 +38,7 @@ class Schedule implements TaskInterface private $interval = null; private $command = ''; - public function __construct(string $cronjob) + public function __construct(Interval $interval = null, $cmd = '') { } diff --git a/Utils/TaskSchedule/ScheduleInterface.php b/Utils/TaskSchedule/ScheduleInterface.php index 3cbaba75c..57e4b3983 100644 --- a/Utils/TaskSchedule/ScheduleInterface.php +++ b/Utils/TaskSchedule/ScheduleInterface.php @@ -37,4 +37,6 @@ interface ScheduleInterface public function list(); public function set(TaskInterface $task); + + public function save(); } diff --git a/Utils/TaskSchedule/SchedulerFactory.php b/Utils/TaskSchedule/SchedulerFactory.php new file mode 100644 index 000000000..d517b6c00 --- /dev/null +++ b/Utils/TaskSchedule/SchedulerFactory.php @@ -0,0 +1,20 @@ +