| 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 |
JPluginHelper::importPlugin('vikbooking'); |
| 15 |
|
| 16 |
/** |
| 17 |
* Factory application class. |
| 18 |
* |
| 19 |
* @since 1.15 (J) - 1.5 (WP) |
| 20 |
*/ |
| 21 |
final class VBOFactory |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Application configuration handler. |
| 25 |
* |
| 26 |
* @var VBOConfigRegistry |
| 27 |
*/ |
| 28 |
private static $config; |
| 29 |
|
| 30 |
/** |
| 31 |
* Application platform handler. |
| 32 |
* |
| 33 |
* @var VBOPlatformInterface |
| 34 |
*/ |
| 35 |
private static $platform; |
| 36 |
|
| 37 |
/** |
| 38 |
* Cron jobs factory instance. |
| 39 |
* |
| 40 |
* @var VBOCronFactory |
| 41 |
*/ |
| 42 |
private static $cronFactory; |
| 43 |
|
| 44 |
/** |
| 45 |
* Notification Center handler instance. |
| 46 |
* |
| 47 |
* @var VBONotificationCenter |
| 48 |
*/ |
| 49 |
private static $notificationCenter; |
| 50 |
|
| 51 |
/** |
| 52 |
* Crontab simulator instance. |
| 53 |
* |
| 54 |
* @var VBOCrontabSimulator |
| 55 |
*/ |
| 56 |
private static $crontabSimulator; |
| 57 |
|
| 58 |
/** |
| 59 |
* Task Manager instance. |
| 60 |
* |
| 61 |
* @var VBOTaskManager |
| 62 |
*/ |
| 63 |
private static $taskManager; |
| 64 |
|
| 65 |
/** |
| 66 |
* Class constructor. |
| 67 |
* @private This object cannot be instantiated. |
| 68 |
*/ |
| 69 |
private function __construct() |
| 70 |
{ |
| 71 |
// never called |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Class cloner. |
| 76 |
* @private This object cannot be cloned. |
| 77 |
*/ |
| 78 |
private function __clone() |
| 79 |
{ |
| 80 |
// never called |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Returns the current configuration object. |
| 85 |
* |
| 86 |
* @return VBOConfigRegistry |
| 87 |
*/ |
| 88 |
public static function getConfig() |
| 89 |
{ |
| 90 |
// check if config class is already instantiated |
| 91 |
if (is_null(static::$config)) |
| 92 |
{ |
| 93 |
// cache instantiation |
| 94 |
static::$config = new VBOConfigRegistryDatabase([ |
| 95 |
'db' => JFactory::getDbo(), |
| 96 |
]); |
| 97 |
} |
| 98 |
|
| 99 |
return static::$config; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Returns the current platform handler. |
| 104 |
* |
| 105 |
* @return VBOPlatformInterface |
| 106 |
*/ |
| 107 |
public static function getPlatform() |
| 108 |
{ |
| 109 |
// check if platform class is already instantiated |
| 110 |
if (is_null(static::$platform)) |
| 111 |
{ |
| 112 |
if (VBOPlatformDetection::isWordPress()) |
| 113 |
{ |
| 114 |
// running WordPress platform |
| 115 |
static::$platform = new VBOPlatformOrgWordpress(); |
| 116 |
} |
| 117 |
else |
| 118 |
{ |
| 119 |
// running Joomla platform |
| 120 |
static::$platform = new VBOPlatformOrgJoomla(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return static::$platform; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns the current cron factory. |
| 129 |
* |
| 130 |
* @return VBOCronFactory |
| 131 |
* |
| 132 |
* @since 1.15.10 (J) - 1.5.10 (WP) |
| 133 |
*/ |
| 134 |
public static function getCronFactory() |
| 135 |
{ |
| 136 |
// check if cron factory class is already instantiated |
| 137 |
if (is_null(static::$cronFactory)) |
| 138 |
{ |
| 139 |
// create cron factory class and register the default folder |
| 140 |
static::$cronFactory = new VBOCronFactory; |
| 141 |
static::$cronFactory->setIncludePaths(VBO_ADMIN_PATH . DIRECTORY_SEPARATOR . 'cronjobs'); |
| 142 |
|
| 143 |
/** |
| 144 |
* Trigger hook to allow third-party plugin to register custom folders in which |
| 145 |
* VikBooking should look for the creation of new cron job instances. |
| 146 |
* |
| 147 |
* In example: |
| 148 |
* $factory->addIncludePath($path); |
| 149 |
* $factory->addIncludePaths([$path1, $path2, ...]); |
| 150 |
* |
| 151 |
* @param VBOCronFactory $factory The cron jobs factory. |
| 152 |
* |
| 153 |
* @return void |
| 154 |
* |
| 155 |
* @since 1.15.10 (J) - 1.5.10 (WP) |
| 156 |
*/ |
| 157 |
VBOFactory::getPlatform()->getDispatcher()->trigger('onCreateCronJobsFactoryVikBooking', [static::$cronFactory]); |
| 158 |
} |
| 159 |
|
| 160 |
return static::$cronFactory; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Returns the current notification center instance. |
| 165 |
* |
| 166 |
* @return VBONotificationCenter |
| 167 |
* |
| 168 |
* @since 1.16.8 (J) - 1.6.8 (WP) |
| 169 |
*/ |
| 170 |
public static function getNotificationCenter() |
| 171 |
{ |
| 172 |
// check if the notification center class is already instantiated |
| 173 |
if (is_null(static::$notificationCenter)) |
| 174 |
{ |
| 175 |
// create Notification Center class |
| 176 |
static::$notificationCenter = new VBONotificationCenter; |
| 177 |
} |
| 178 |
|
| 179 |
return static::$notificationCenter; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Returns the current crontab simulator. |
| 184 |
* |
| 185 |
* @return VBOCrontabSimulator |
| 186 |
* |
| 187 |
* @since 1.17 (J) - 1.7 (WP) |
| 188 |
*/ |
| 189 |
public static function getCrontabSimulator() |
| 190 |
{ |
| 191 |
// check if cron factory class is already instantiated |
| 192 |
if (is_null(static::$crontabSimulator)) { |
| 193 |
// set up the preferred semaphore instance |
| 194 |
$semaphore = new VBOCrontabSemaphoreConfig; |
| 195 |
|
| 196 |
// set up the preferred logger instance |
| 197 |
$logger = new VBOCrontabLoggerFile; |
| 198 |
|
| 199 |
// instantiate simulator |
| 200 |
static::$crontabSimulator = new VBOCrontabSimulator($semaphore, $logger); |
| 201 |
|
| 202 |
if (VBOPlatformDetection::isJoomla()) { |
| 203 |
/** |
| 204 |
* Watch the automatic payments scheduled, if any. |
| 205 |
* Should run every 5 minutes. |
| 206 |
*/ |
| 207 |
static::$crontabSimulator->schedule(new VBOCrontabRunnerAware('pay_schedules_watcher', 5 * 60, function(VBOCrontabLogger $logger) { |
| 208 |
VBOModelPayschedules::getInstance()->watch(); |
| 209 |
})); |
| 210 |
|
| 211 |
/** |
| 212 |
* Run performances cleaner. |
| 213 |
* Should run every week. |
| 214 |
*/ |
| 215 |
static::$crontabSimulator->schedule(new VBOCrontabRunnerAware('performance_cleaner', 60 * 60 * 24 * 7, function(VBOCrontabLogger $logger) { |
| 216 |
// performance cleaning check |
| 217 |
VBOPerformanceCleaner::runCheck(); |
| 218 |
// clean up expired passcodes from smart locks |
| 219 |
VBOFactory::getDoorAccessControl()->cleanExpiredPasscodes(); |
| 220 |
})); |
| 221 |
|
| 222 |
/** |
| 223 |
* Run the door-access-control framework. |
| 224 |
* Should run every hour. |
| 225 |
*/ |
| 226 |
static::$crontabSimulator->schedule(new VBOCrontabRunnerAware('door_access_control', 60 * 60, function(VBOCrontabLogger $logger) { |
| 227 |
// watch if any booking is approaching the check-in date |
| 228 |
VBOFactory::getDoorAccessControl()->handleUpcomingArrivals(); |
| 229 |
// watch if passcodes get used for the first time |
| 230 |
VBOFactory::getDoorAccessControl()->watchFirstAccess(); |
| 231 |
})); |
| 232 |
|
| 233 |
/** |
| 234 |
* Run database optimization. |
| 235 |
* Should run every hour. |
| 236 |
*/ |
| 237 |
static::$crontabSimulator->schedule(new VBOCrontabRunnerAware('db_optimization', 60 * 60, function(VBOCrontabLogger $logger) { |
| 238 |
VBOPerformanceCleaner::optimizeDatabase(); |
| 239 |
})); |
| 240 |
} |
| 241 |
|
| 242 |
if (class_exists('VCMCrontabProvider')) { |
| 243 |
// in case VikChannelManager is installed, schedule the native background tasks |
| 244 |
(new VCMCrontabProvider)->provide(static::$crontabSimulator); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Trigger hook to allow third-party plugins to register custom background tasks. |
| 249 |
* |
| 250 |
* The following example explains how to schedule a task that runs every hour. |
| 251 |
* |
| 252 |
* ``` |
| 253 |
* $crontab->schedule(new VBOCrontabRunnerAware('unique_id', 60 * 60, function($logger) { |
| 254 |
* // do stuff |
| 255 |
* })); |
| 256 |
* ``` |
| 257 |
* |
| 258 |
* @param VBOCrontabSimulator $crontab The crontab simulator instance. |
| 259 |
* |
| 260 |
* @return void |
| 261 |
* |
| 262 |
* @since 1.17 (J) - 1.7 (WP) |
| 263 |
*/ |
| 264 |
VBOFactory::getPlatform()->getDispatcher()->trigger('onSetupCrontabSimulator', [static::$crontabSimulator]); |
| 265 |
|
| 266 |
// do not run in case we are doing an AJAX request |
| 267 |
if (!strcasecmp(JFactory::getApplication()->input->server->get('HTTP_X_REQUESTED_WITH', ''), 'xmlhttprequest')) { |
| 268 |
static::$crontabSimulator->stop(); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
return static::$crontabSimulator; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Returns the current task manager instance. |
| 277 |
* |
| 278 |
* @return VBOTaskManager |
| 279 |
* |
| 280 |
* @since 1.18.0 (J) - 1.8.0 (WP) |
| 281 |
*/ |
| 282 |
public static function getTaskManager() |
| 283 |
{ |
| 284 |
// check if the object is already instantiated |
| 285 |
if (is_null(static::$taskManager)) |
| 286 |
{ |
| 287 |
// create object instance |
| 288 |
static::$taskManager = new VBOTaskManager; |
| 289 |
} |
| 290 |
|
| 291 |
return static::$taskManager; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Returns a new chat mediator instance. |
| 296 |
* |
| 297 |
* @return VBOChatMediator |
| 298 |
* |
| 299 |
* @since 1.18 (J) - 1.8 (WP) |
| 300 |
*/ |
| 301 |
public static function getChatMediator() |
| 302 |
{ |
| 303 |
return new VBOChatMediator(new VBOChatStorageDatabase); |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Returns a new help wizard instance. |
| 308 |
* |
| 309 |
* @return VBOHelpWizard |
| 310 |
* |
| 311 |
* @since 1.18.2 (J) - 1.8.2 (WP) |
| 312 |
*/ |
| 313 |
public static function getHelpWizard() |
| 314 |
{ |
| 315 |
$helpWizard = new VBOHelpWizard([ |
| 316 |
'delay' => 0, |
| 317 |
]); |
| 318 |
|
| 319 |
// get VikBooking configuration status |
| 320 |
$metrics = VikBookingHelper::getFirstSetupMetrics(); |
| 321 |
|
| 322 |
// do not spam the user with wizard hints until the first configuration is complete |
| 323 |
if (empty($metrics['totprices']) || empty($metrics['totrooms']) || empty($metrics['totdailyfares'])) { |
| 324 |
return $helpWizard; |
| 325 |
} |
| 326 |
|
| 327 |
// attach the folder where the instructions should be stored |
| 328 |
$helpWizard->addIncludePath(VBO_ADMIN_PATH . '/helpers/src/help/wizard/drivers/'); |
| 329 |
|
| 330 |
return $helpWizard; |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Returns the Door Access Control instance. |
| 335 |
* |
| 336 |
* @return VBODooraccessFactory |
| 337 |
* |
| 338 |
* @since 1.18.4 (J) - 1.8.4 (WP) |
| 339 |
*/ |
| 340 |
public static function getDoorAccessControl() |
| 341 |
{ |
| 342 |
return VBODooraccessFactory::getInstance(); |
| 343 |
} |
| 344 |
} |
| 345 |
|