| 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 |
* VikBooking cron job abstraction. |
| 16 |
* |
| 17 |
* @since 1.5.10 |
| 18 |
*/ |
| 19 |
abstract class VBOCronJob |
| 20 |
{ |
| 21 |
use VBOLoggerAware; |
| 22 |
|
| 23 |
/** |
| 24 |
* The cron object data. |
| 25 |
* |
| 26 |
* @var object |
| 27 |
*/ |
| 28 |
protected $data = null; |
| 29 |
|
| 30 |
/** |
| 31 |
* The cron job driver parameters. |
| 32 |
* |
| 33 |
* @var JObject |
| 34 |
*/ |
| 35 |
protected $params; |
| 36 |
|
| 37 |
/** |
| 38 |
* Debug mode flag. Always disabled by default. |
| 39 |
* |
| 40 |
* @var bool |
| 41 |
*/ |
| 42 |
private $debug = false; |
| 43 |
|
| 44 |
/** |
| 45 |
* Class constructor. |
| 46 |
* |
| 47 |
* @param object $data The cron data. |
| 48 |
*/ |
| 49 |
public function __construct($data = null) |
| 50 |
{ |
| 51 |
if ($data) |
| 52 |
{ |
| 53 |
if (is_numeric($data)) |
| 54 |
{ |
| 55 |
// ID given, fetch cron job data |
| 56 |
$this->data = VBOMvcModel::getInstance('cronjob')->getItem((int) $data); |
| 57 |
} |
| 58 |
else |
| 59 |
{ |
| 60 |
// create a clone to avoid affecting original argument too |
| 61 |
$this->data = clone $data; |
| 62 |
} |
| 63 |
|
| 64 |
// wrap parameters in a registry for a better ease of use |
| 65 |
$this->params = new JObject($data->params); |
| 66 |
|
| 67 |
// avoid keeping duplicate information |
| 68 |
unset($this->data->params); |
| 69 |
} |
| 70 |
else |
| 71 |
{ |
| 72 |
// use an empty object to avoid errors in child classes |
| 73 |
$this->params = new JObject(); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns the cron job details. |
| 79 |
* |
| 80 |
* @return object |
| 81 |
*/ |
| 82 |
final public function getData() |
| 83 |
{ |
| 84 |
return $this->data; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns the title of the cron job. |
| 89 |
* |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
abstract public function getTitle(); |
| 93 |
|
| 94 |
/** |
| 95 |
* This method should return all the form fields required to collect the information |
| 96 |
* needed for the execution of the cron job. |
| 97 |
* |
| 98 |
* @return array An associative array of form fields. |
| 99 |
*/ |
| 100 |
public function getForm() |
| 101 |
{ |
| 102 |
return []; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Launches the process declared by the cron job. |
| 107 |
* |
| 108 |
* @return boolean True on success, false otherwise. |
| 109 |
*/ |
| 110 |
final public function run() |
| 111 |
{ |
| 112 |
$result = $this->execute(); |
| 113 |
|
| 114 |
$this->postflight(); |
| 115 |
|
| 116 |
return (bool) $result; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Sets the debug mode. |
| 121 |
* |
| 122 |
* @param boolean $mode True to enable the debug mode, false otherwise. |
| 123 |
* |
| 124 |
* @return self |
| 125 |
*/ |
| 126 |
final public function setDebug($mode = true) |
| 127 |
{ |
| 128 |
$this->debug = (bool) $mode; |
| 129 |
|
| 130 |
return $this; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Checks whether the debug mode is on or off. |
| 135 |
* |
| 136 |
* @return boolean |
| 137 |
*/ |
| 138 |
final public function isDebug() |
| 139 |
{ |
| 140 |
return $this->debug; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Outputs the given string if we are in debug mode. |
| 145 |
* |
| 146 |
* @param string $str The string to output. |
| 147 |
* @param string $separator A separator to append. |
| 148 |
* |
| 149 |
* @return void |
| 150 |
*/ |
| 151 |
protected function output($str, $separator = "\n") |
| 152 |
{ |
| 153 |
if (!$this->isDebug()) |
| 154 |
{ |
| 155 |
// suppress output if we are not in debug mode |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
if (is_array($str) || is_object($str)) |
| 160 |
{ |
| 161 |
// convert object into a readable string |
| 162 |
$str = print_r($str, true); |
| 163 |
} |
| 164 |
|
| 165 |
// output string |
| 166 |
echo $str . $separator; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Helper method used to flag the specified element as tracked. This is really helpful to easily |
| 171 |
* check whether a specific record has been already parsed, which can be done by passing the same |
| 172 |
* element argument to the `isTracked` method. |
| 173 |
* |
| 174 |
* It is recommended to register only scalar values in order to prevent an uncontrolled increase of the |
| 175 |
* total length, which can arrive up to 2^16-1 characters (65535). It's up to the sub-classes to take care of |
| 176 |
* this limit, which should clean the flag_char property in order to always have less than 65536 characters. |
| 177 |
* |
| 178 |
* @param mixed $element The element to track. |
| 179 |
* |
| 180 |
* @return boolean True on success, false otherwise. |
| 181 |
*/ |
| 182 |
abstract protected function track($element); |
| 183 |
|
| 184 |
/** |
| 185 |
* Checks whether the specified element has been already processed. |
| 186 |
* |
| 187 |
* @param mixed $element The element to check. |
| 188 |
* |
| 189 |
* @return boolean True if already processed, false otherwise. |
| 190 |
*/ |
| 191 |
abstract protected function isTracked($element); |
| 192 |
|
| 193 |
/** |
| 194 |
* Executes the cron job. |
| 195 |
* |
| 196 |
* @return boolean True on success, false otherwise. |
| 197 |
*/ |
| 198 |
abstract protected function execute(); |
| 199 |
|
| 200 |
/** |
| 201 |
* Executes after processing the cron job. |
| 202 |
* |
| 203 |
* @return void |
| 204 |
*/ |
| 205 |
protected function postflight() |
| 206 |
{ |
| 207 |
// children classes can override this method to perform some extra queries |
| 208 |
} |
| 209 |
} |
| 210 |
|