Skip to content

Commit

Permalink
refactor: use Normalizer instead
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Feb 20, 2021
1 parent afb57c4 commit 42700ab
Showing 1 changed file with 8 additions and 60 deletions.
68 changes: 8 additions & 60 deletions src/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,45 +29,13 @@ class Expression
/** @var SegmentChecker */
protected $checker;

protected static $expressions = [
'@yearly' => '0 0 1 1 *',
'@annually' => '0 0 1 1 *',
'@monthly' => '0 0 1 * *',
'@weekly' => '0 0 * * 0',
'@daily' => '0 0 * * *',
'@hourly' => '0 * * * *',
'@always' => '* * * * *',
'@5minutes' => '*/5 * * * *',
'@10minutes' => '*/10 * * * *',
'@15minutes' => '*/15 * * * *',
'@30minutes' => '0,30 * * * *',
];

protected static $literals = [
'sun' => 0,
'mon' => 1,
'tue' => 2,
'wed' => 3,
'thu' => 4,
'fri' => 5,
'sat' => 6,
'jan' => 1,
'feb' => 2,
'mar' => 3,
'apr' => 4,
'may' => 5,
'jun' => 6,
'jul' => 7,
'aug' => 8,
'sep' => 9,
'oct' => 10,
'nov' => 11,
'dec' => 12,
];

public function __construct(SegmentChecker $checker = null)
/** @var Normalizer */
protected $normalizer;

public function __construct(SegmentChecker $checker = null, Normalizer $normalizer = null)
{
$this->checker = $checker ?: new SegmentChecker;
$this->checker = $checker ?: new SegmentChecker;
$this->normalizer = $normalizer ?: new Normalizer;

if (null === static::$instance) {
static::$instance = $this;
Expand Down Expand Up @@ -123,7 +91,7 @@ public function isCronDue(string $expr, $time = null): bool
{
$this->checker->setReference(new ReferenceTime($time));

foreach (\explode(' ', $this->normalizeExpr($expr)) as $pos => $segment) {
foreach (\explode(' ', $this->normalizer->normalizeExpr($expr)) as $pos => $segment) {
if ($segment === '*' || $segment === '?') {
continue;
}
Expand All @@ -149,7 +117,7 @@ public function filter(array $jobs, $time = null): array
$dues = $cache = [];

foreach ($jobs as $name => $expr) {
$expr = $this->normalizeExpr($expr);
$expr = $this->normalizer->normalizeExpr($expr);

if (!isset($cache[$expr])) {
$cache[$expr] = $this->isCronDue($expr, $time);
Expand All @@ -162,24 +130,4 @@ public function filter(array $jobs, $time = null): array

return $dues;
}

protected function normalizeExpr(string $expr): string
{
$expr = \trim($expr);

if (isset(static::$expressions[$expr])) {
return static::$expressions[$expr];
}

$expr = \preg_replace('~\s+~', ' ', $expr);
$count = \substr_count($expr, ' ');

if ($count < 4 || $count > 5) {
throw new \UnexpectedValueException(
'Cron $expr should have 5 or 6 segments delimited by space'
);
}

return \str_ireplace(\array_keys(static::$literals), \array_values(static::$literals), $expr);
}
}

0 comments on commit 42700ab

Please sign in to comment.