Skip to content

Commit d1c5fd1

Browse files
committed
refactor: expression method calls
1 parent 5aaeee8 commit d1c5fd1

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

src/Expression.php

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,36 @@ class Expression
5858
*/
5959
public static function isDue($expr, $time = null)
6060
{
61-
list($expr, $time) = static::process($expr, $time);
61+
static $instance;
6262

63+
if (!$instance) {
64+
$instance = new static;
65+
}
66+
67+
return $instance->isCronDue($expr, $time);
68+
}
69+
70+
/**
71+
* Instance call.
72+
*
73+
* Parse cron expression to decide if it can be run on given time (or default now).
74+
*
75+
* @param string $expr The cron expression.
76+
* @param int $time The timestamp to validate the cron expr against. Defaults to now.
77+
*
78+
* @return bool
79+
*/
80+
public function isCronDue($expr, $time = null)
81+
{
82+
list($expr, $time) = $this->process($expr, $time);
83+
84+
$checker = new SegmentChecker;
6385
foreach ($expr as $pos => $segment) {
6486
if ($segment === '*' || $segment === '?') {
6587
continue;
6688
}
6789

68-
if (!SegmentChecker::isDue($segment, $pos, $time)) {
90+
if (!$checker->isDue($segment, $pos, $time)) {
6991
return false;
7092
}
7193
}
@@ -81,53 +103,38 @@ public static function isDue($expr, $time = null)
81103
*
82104
* @return array
83105
*/
84-
protected static function process($expr, $time)
106+
protected function process($expr, $time)
85107
{
86108
if (isset(static::$expressions[$expr])) {
87109
$expr = static::$expressions[$expr];
88110
}
89111

90-
$expr = str_ireplace(array_keys(static::$literals), array_values(static::$literals), $expr);
91-
$expr = explode(' ', $expr);
112+
$expr = \str_ireplace(\array_keys(static::$literals), \array_values(static::$literals), $expr);
113+
$expr = \explode(' ', $expr);
92114

93-
if (count($expr) < 5 || count($expr) > 6) {
115+
if (\count($expr) < 5 || \count($expr) > 6) {
94116
throw new \UnexpectedValueException(
95117
'Cron $expr should have 5 or 6 segments delimited by space'
96118
);
97119
}
98120

99121
$time = static::normalizeTime($time);
100122

101-
$time = array_map('intval', explode(' ', date('i G j n w Y t d m N', $time)));
123+
$time = \array_map('intval', \explode(' ', \date('i G j n w Y t d m N', $time)));
102124

103125
return [$expr, $time];
104126
}
105127

106-
protected static function normalizeTime($time)
128+
protected function normalizeTime($time)
107129
{
108130
if (empty($time)) {
109-
$time = time();
110-
} elseif (is_string($time)) {
111-
$time = strtotime($time);
131+
$time = \time();
132+
} elseif (\is_string($time)) {
133+
$time = \strtotime($time);
112134
} elseif ($time instanceof \DateTime) {
113135
$time = $time->getTimestamp();
114136
}
115137

116138
return $time;
117139
}
118-
119-
/**
120-
* Instance call.
121-
*
122-
* Parse cron expression to decide if it can be run on given time (or default now).
123-
*
124-
* @param string $expr The cron expression.
125-
* @param int $time The timestamp to validate the cron expr against. Defaults to now.
126-
*
127-
* @return bool
128-
*/
129-
public function isCronDue($expr, $time = null)
130-
{
131-
return static::isDue($expr, $time);
132-
}
133140
}

0 commit comments

Comments
 (0)