Skip to content

Commit

Permalink
Refactoring: CronExpressionJob abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
josefbenjac committed Dec 18, 2017
1 parent bf4592e commit 2eb0169
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
35 changes: 2 additions & 33 deletions src/CallbackJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@

namespace Tlapnet\Scheduler;

use Cron\CronExpression;
use DateTime;

class CallbackJob implements IJob
class CallbackJob extends CronJob
{

/** @var CronExpression */
private $expression;

/** @var callable */
private $callback;

Expand All @@ -20,27 +14,10 @@ class CallbackJob implements IJob
*/
public function __construct($cron, $callback)
{
$this->expression = CronExpression::factory($cron);
parent::__construct($cron);
$this->callback = $callback;
}

/**
* @return bool
*/
public function isDue()
{
return $this->expression->isDue();
}

/**
* @param DateTime $dateTime
* @return bool
*/
public function isDueByDate(DateTime $dateTime)
{
return $this->expression->isDue($dateTime);
}

/**
* @return void
*/
Expand All @@ -49,14 +26,6 @@ public function run()
call_user_func($this->callback);
}

/**
* @return CronExpression
*/
public function getExpression()
{
return $this->expression;
}

/**
* @return callable
*/
Expand Down
47 changes: 47 additions & 0 deletions src/CronJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Tlapnet\Scheduler;

use Cron\CronExpression;
use DateTime;

abstract class CronJob implements IJob
{

/** @var CronExpression */
protected $expression;

/**
* @param string $cron
*/
public function __construct($cron)
{
$this->expression = CronExpression::factory($cron);
}

/**
* @return bool
*/
public function isDue()
{
return $this->expression->isDue();
}

/**
* @param DateTime $dateTime
* @return bool
*/
public function isDueByDate(DateTime $dateTime)
{
return $this->expression->isDue($dateTime);
}

/**
* @return CronExpression
*/
public function getExpression()
{
return $this->expression;
}

}

0 comments on commit 2eb0169

Please sign in to comment.