factory.php
408 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 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 | * Factory application class. |
| 16 | * |
| 17 | * @since 1.6 |
| 18 | * @since 1.7 Renamed from UIFactory |
| 19 | */ |
| 20 | final class VAPFactory |
| 21 | { |
| 22 | /** |
| 23 | * Application configuration handlers. |
| 24 | * |
| 25 | * @var VAPConfig[] |
| 26 | */ |
| 27 | private static $config = array(); |
| 28 | |
| 29 | /** |
| 30 | * Application event dispatcher. |
| 31 | * |
| 32 | * @var VAPEventDispatcher |
| 33 | */ |
| 34 | private static $eventDispatcher = null; |
| 35 | |
| 36 | /** |
| 37 | * The currency object. |
| 38 | * |
| 39 | * @var VAPCurrency |
| 40 | * |
| 41 | * @since 1.7 |
| 42 | */ |
| 43 | private static $currency = null; |
| 44 | |
| 45 | /** |
| 46 | * The translator object. |
| 47 | * |
| 48 | * @var VAPLanguageTranslator |
| 49 | * |
| 50 | * @since 1.7 |
| 51 | */ |
| 52 | private static $translator = null; |
| 53 | |
| 54 | /** |
| 55 | * The API Framework instance. |
| 56 | * |
| 57 | * @var VAPApiFramework |
| 58 | * |
| 59 | * @since 1.7 |
| 60 | */ |
| 61 | private static $api = null; |
| 62 | |
| 63 | /** |
| 64 | * The wizard object. |
| 65 | * |
| 66 | * @var VAPWizard |
| 67 | * |
| 68 | * @since 1.7.1 |
| 69 | */ |
| 70 | private static $wizard = null; |
| 71 | |
| 72 | /** |
| 73 | * Default configuration class handler. |
| 74 | * |
| 75 | * @var string |
| 76 | */ |
| 77 | public static $defaultConfigClass = 'database'; |
| 78 | |
| 79 | /** |
| 80 | * Class constructor. |
| 81 | * @private This object cannot be instantiated. |
| 82 | */ |
| 83 | private function __construct() |
| 84 | { |
| 85 | // never called |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Class cloner. |
| 90 | * @private This object cannot be cloned. |
| 91 | */ |
| 92 | private function __clone() |
| 93 | { |
| 94 | // never called |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get the current configuration object. |
| 99 | * |
| 100 | * @param string $class The handler to use. |
| 101 | * @param array $args An options array. |
| 102 | * |
| 103 | * @return VAPConfig |
| 104 | * |
| 105 | * @throws Exception When the configuration class doesn't exist. |
| 106 | */ |
| 107 | public static function getConfig($class = null, array $args = array()) |
| 108 | { |
| 109 | // if class not set, get the default one |
| 110 | if ($class === null) |
| 111 | { |
| 112 | $class = static::$defaultConfigClass; |
| 113 | } |
| 114 | |
| 115 | // check if config class is already instantiated |
| 116 | if (!isset(static::$config[$class])) |
| 117 | { |
| 118 | // build classname |
| 119 | $classname = 'VAPConfig' . ucwords($class[0]) . substr($class, 1); |
| 120 | |
| 121 | // try to import it (on failure, throws exception) |
| 122 | if (!VAPLoader::import('libraries.config.classes.' . $class) |
| 123 | || !class_exists($classname)) |
| 124 | { |
| 125 | throw new Exception("Config {$class} not found!"); |
| 126 | } |
| 127 | |
| 128 | // cache instantiation |
| 129 | static::$config[$class] = new $classname($args); |
| 130 | } |
| 131 | |
| 132 | return static::$config[$class]; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns the internal event dispatcher instance. |
| 137 | * |
| 138 | * @return VAPEventDispatcher The event dispatcher. |
| 139 | */ |
| 140 | public static function getEventDispatcher() |
| 141 | { |
| 142 | if (static::$eventDispatcher === null) |
| 143 | { |
| 144 | VAPLoader::import('libraries.event.dispatcher'); |
| 145 | |
| 146 | // obtain the software version always from the database |
| 147 | $version = static::getConfig()->get('version', VIKAPPOINTMENTS_SOFTWARE_VERSION); |
| 148 | |
| 149 | // build options array |
| 150 | $options = array( |
| 151 | 'alias' => 'com_vikappointments', |
| 152 | 'version' => $version, |
| 153 | 'admin' => JFactory::getApplication()->isClient('administrator'), |
| 154 | 'call' => null, // call is useless as it would be always the same |
| 155 | ); |
| 156 | |
| 157 | static::$eventDispatcher = VAPEventDispatcher::getInstance($options); |
| 158 | } |
| 159 | |
| 160 | return static::$eventDispatcher; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Instantiate a new currency object. |
| 165 | * |
| 166 | * @param boolean $reload True to refresh the currency settings (@since 1.7.1). |
| 167 | * |
| 168 | * @return VAPCurrency |
| 169 | * |
| 170 | * @since 1.7 |
| 171 | */ |
| 172 | public static function getCurrency($reload = false) |
| 173 | { |
| 174 | if (static::$currency === null || $reload) |
| 175 | { |
| 176 | $config = static::getConfig(); |
| 177 | |
| 178 | VAPLoader::import('libraries.currency.currency'); |
| 179 | |
| 180 | // obtain configuration data |
| 181 | $data = array( |
| 182 | 'currencyname' => $config->getString('currencyname', 'EUR'), |
| 183 | 'currencysymb' => $config->getString('currencysymb', '€'), |
| 184 | 'currsymbpos' => $config->getInt('currsymbpos', 1), |
| 185 | 'currdecimalsep' => $config->getString('currdecimalsep', '.'), |
| 186 | 'currthousandssep' => $config->getString('currthousandssep', ','), |
| 187 | 'currdecimaldig' => $config->getUint('currdecimaldig', 2), |
| 188 | ); |
| 189 | |
| 190 | // set up currency |
| 191 | $currency = new VAPCurrency( |
| 192 | $data['currencyname'], |
| 193 | $data['currencysymb'], |
| 194 | abs($data['currsymbpos']), |
| 195 | array($data['currdecimalsep'], $data['currthousandssep']), |
| 196 | $data['currdecimaldig'], |
| 197 | // include space if we have position equals to [1,2] |
| 198 | $data['currsymbpos'] > 0 |
| 199 | ); |
| 200 | |
| 201 | // define the entry point in a variable to avoid losing the initial reference of the chain |
| 202 | $currencyConversionProvider = new VAPCurrencyConverterProviderNull; |
| 203 | |
| 204 | // define the last node that will be used for chaining |
| 205 | $node = $currencyConversionProvider; |
| 206 | |
| 207 | if ($config->getBool('currency_ecb_enabled')) |
| 208 | { |
| 209 | // chain ECB provider |
| 210 | $node = $node->setNext(new VAPCurrencyConverterProviderEcb); |
| 211 | } |
| 212 | |
| 213 | if ($config->getBool('currency_currencyapi_enabled')) |
| 214 | { |
| 215 | // chain Currency API provider |
| 216 | $node = $node->setNext(new VAPCurrencyConverterProviderCurrencyapi( |
| 217 | $config->get('currency_currencyapi_key', ''), |
| 218 | $config->get('currency_currencyapi_cache', 240) |
| 219 | )); |
| 220 | } |
| 221 | |
| 222 | if ($config->getBool('currency_floatrates_enabled')) |
| 223 | { |
| 224 | // chain Float Rates provider |
| 225 | $node = $node->setNext(new VAPCurrencyConverterProviderFloatrates); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Create currency converter mediator. |
| 230 | * |
| 231 | * @since 1.7.6 |
| 232 | */ |
| 233 | $currencyConverter = new VAPCurrencyConverterMediator($currency, $currencyConversionProvider); |
| 234 | |
| 235 | // pull preferred currency code from the user state |
| 236 | $userCurrencyCode = JFactory::getApplication()->getUserState('vikappointments.user.currency', $currency->getCode()); |
| 237 | |
| 238 | try |
| 239 | { |
| 240 | // create a new currency object to satisfy the user preferences |
| 241 | $currency = $currencyConverter->getCurrency($userCurrencyCode); |
| 242 | } |
| 243 | catch (Exception $error) |
| 244 | { |
| 245 | // unable to use a currency converter |
| 246 | } |
| 247 | |
| 248 | // internally cache the currency |
| 249 | static::$currency = $currency; |
| 250 | } |
| 251 | |
| 252 | return static::$currency; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Instantiate a new translator object. |
| 257 | * |
| 258 | * @return VAPLanguageTranslator |
| 259 | * |
| 260 | * @since 1.7 |
| 261 | */ |
| 262 | public static function getTranslator() |
| 263 | { |
| 264 | if (static::$translator === null) |
| 265 | { |
| 266 | VAPLoader::import('libraries.language.translator'); |
| 267 | |
| 268 | static::$translator = VAPLanguageTranslator::getInstance(); |
| 269 | } |
| 270 | |
| 271 | return static::$translator; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Instantiate a new Framework API object. |
| 276 | * |
| 277 | * @return VAPApiFramework |
| 278 | * |
| 279 | * @since 1.7 |
| 280 | */ |
| 281 | public static function getApi() |
| 282 | { |
| 283 | if (static::$api === null) { |
| 284 | |
| 285 | // include API libraries and implementors |
| 286 | VAPLoader::import('libraries.api.autoload'); |
| 287 | VAPLoader::import('libraries.api.implementors.login'); |
| 288 | VAPLoader::import('libraries.api.implementors.framework'); |
| 289 | |
| 290 | // instantiate API Framework |
| 291 | // leave constructor empty to select default plugins folder: |
| 292 | // components/com_vikappointments/helpers/libraries/api/plugins/ |
| 293 | static::$api = VAPApiFramework::getInstance(); |
| 294 | |
| 295 | // get event dispatcher |
| 296 | $dispatcher = static::getEventDispatcher(); |
| 297 | |
| 298 | /** |
| 299 | * Trigger event to let the plugins alter the application framework. |
| 300 | * It is possible to use this event to include third-party applications. |
| 301 | * |
| 302 | * In example: |
| 303 | * $api->addIncludePath($path); |
| 304 | * $api->addIncludePaths([$path1, $path2, ...]); |
| 305 | * |
| 306 | * @param VAPApiFramework $api The API framework instance. |
| 307 | * |
| 308 | * @return void |
| 309 | * |
| 310 | * @since 1.7 |
| 311 | */ |
| 312 | $dispatcher->trigger('onInitApplicationFramework', array(static::$api)); |
| 313 | |
| 314 | // get config handler |
| 315 | $config = static::getConfig(); |
| 316 | |
| 317 | // set apis configuration |
| 318 | static::$api->set('max_failure_attempts', $config->getUint('apimaxfail', 10)); |
| 319 | |
| 320 | } |
| 321 | |
| 322 | return static::$api; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Instantiate a new wizard object. |
| 327 | * |
| 328 | * @return VAPWizard |
| 329 | * |
| 330 | * @since 1.7.1 |
| 331 | */ |
| 332 | public static function getWizard() |
| 333 | { |
| 334 | if (static::$wizard === null) |
| 335 | { |
| 336 | VAPLoader::import('libraries.wizard.wizard'); |
| 337 | |
| 338 | // get global wizard instance |
| 339 | $wizard = VAPWizard::getInstance(); |
| 340 | |
| 341 | // complete setup only if not yet completed |
| 342 | if (!$wizard->isDone()) |
| 343 | { |
| 344 | // define list of steps to load |
| 345 | $steps = array( |
| 346 | 'system', |
| 347 | 'taxes', |
| 348 | 'employees', |
| 349 | 'services', |
| 350 | 'options', |
| 351 | 'locations', |
| 352 | 'locwdays', |
| 353 | 'payments', |
| 354 | 'syspack', |
| 355 | 'packages', |
| 356 | 'syssubscr', |
| 357 | 'subscriptions', |
| 358 | ); |
| 359 | |
| 360 | // set up wizard |
| 361 | $wizard->setup($steps); |
| 362 | |
| 363 | // set up steps dependencies |
| 364 | |
| 365 | if ($wizard['services'] && $wizard['employees']) |
| 366 | { |
| 367 | $wizard['services']->addDependency($wizard['employees']); |
| 368 | } |
| 369 | |
| 370 | if ($wizard['options'] && $wizard['services']) |
| 371 | { |
| 372 | $wizard['options']->addDependency($wizard['services']); |
| 373 | } |
| 374 | |
| 375 | if ($wizard['locations'] && $wizard['employees']) |
| 376 | { |
| 377 | $wizard['locations']->addDependency($wizard['employees']); |
| 378 | } |
| 379 | |
| 380 | if ($wizard['locwdays'] && $wizard['employees']) |
| 381 | { |
| 382 | $wizard['locwdays']->addDependency($wizard['employees']); |
| 383 | } |
| 384 | |
| 385 | if ($wizard['locwdays'] && $wizard['locations']) |
| 386 | { |
| 387 | $wizard['locwdays']->addDependency($wizard['locations']); |
| 388 | } |
| 389 | |
| 390 | if ($wizard['packages'] && $wizard['syspack']) |
| 391 | { |
| 392 | $wizard['packages']->addDependency($wizard['syspack']); |
| 393 | } |
| 394 | |
| 395 | if ($wizard['subscriptions'] && $wizard['syssubscr']) |
| 396 | { |
| 397 | $wizard['subscriptions']->addDependency($wizard['syssubscr']); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // cache wizard |
| 402 | static::$wizard = $wizard; |
| 403 | } |
| 404 | |
| 405 | return static::$wizard; |
| 406 | } |
| 407 | } |
| 408 |