| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Cron runner basic implementation. |
| 16 |
* |
| 17 |
* @since 1.7 |
| 18 |
*/ |
| 19 |
class VBOCrontabRunnerAware implements VBOCrontabRunner |
| 20 |
{ |
| 21 |
/** @var string */ |
| 22 |
protected $id; |
| 23 |
|
| 24 |
/** @var int */ |
| 25 |
protected $interval; |
| 26 |
|
| 27 |
/** @var callback */ |
| 28 |
protected $callback; |
| 29 |
|
| 30 |
/** |
| 31 |
* Class constructor. |
| 32 |
* |
| 33 |
* @param string $id The cron unique identifier. |
| 34 |
* @param int $interval The execution interval in seconds (eg. 3600 to execute every hour). |
| 35 |
* @param callable $callback The callback that will be invoked. |
| 36 |
*/ |
| 37 |
public function __construct(string $id, int $interval, $callback) |
| 38 |
{ |
| 39 |
$this->id = $id; |
| 40 |
$this->interval = abs($interval); |
| 41 |
$this->callback = $callback; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @inheritDoc |
| 46 |
*/ |
| 47 |
final public function getID() |
| 48 |
{ |
| 49 |
return $this->id; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @inheritDoc |
| 54 |
*/ |
| 55 |
public function canRun() |
| 56 |
{ |
| 57 |
// always runnable |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @inheritDoc |
| 63 |
*/ |
| 64 |
public function getNextExecution(DateTime $lastExecution) |
| 65 |
{ |
| 66 |
$nextExecution = clone $lastExecution; |
| 67 |
$nextExecution->modify('+' . $this->interval . ' seconds'); |
| 68 |
return $nextExecution; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @inheritDoc |
| 73 |
*/ |
| 74 |
public function execute(VBOCrontabLogger $logger) |
| 75 |
{ |
| 76 |
// invoke the callback |
| 77 |
call_user_func_array($this->callback, [$logger]); |
| 78 |
} |
| 79 |
} |
| 80 |
|