From 3bf1292775ea38da5e01cd9595ef5187128a2c60 Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Wed, 18 Jun 2025 15:34:07 +0200 Subject: [PATCH] Add `runSyncVehicles` method to XLVask helpers for vehicle synchronization and caching. --- .../modules/xlvask/helpers/xlvask_tasks.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/services/nginx/app/modules/xlvask/helpers/xlvask_tasks.php b/services/nginx/app/modules/xlvask/helpers/xlvask_tasks.php index d82c987a..5fe6a88d 100644 --- a/services/nginx/app/modules/xlvask/helpers/xlvask_tasks.php +++ b/services/nginx/app/modules/xlvask/helpers/xlvask_tasks.php @@ -36,10 +36,12 @@ class xlvask_tasks // Synchronize users, this will keep the users (XL Vask customer objects) in cache up to date $this->runSyncUsers(true); $this->runSyncUsage(); + $this->runSyncVehicles(); $this->runCleanupTasks(); }; } + /** * Run the synchronize users task * This task fetches the users from XL Vask, and synchronizes them with this system. @@ -359,6 +361,44 @@ class xlvask_tasks return $order; } + /** + * Synchronize vehicles with XL Vask + * This method is used to synchronize the vehicles with XL Vask. + * It fetches the vehicles from XL Vask and adds them to the cache. + * This includes: + * - Fetching the vehicles from XL Vask + * - Caching the vehicles in this system + * @param bool $isSilent If true, will not throw an exception if synchronization is not enabled + * @return null|array + * @throws Exception If the task fails or if synchronization is not enabled + * @see xlvask_vehicle + * @see xlvask_cache::getVehicleCache() + * @see xlvask_cache::setVehicleCache() + * @see xlvask_cache::isVehicleCached() + */ + public function runSyncVehicles(bool $isSilent = false): ?array + { + // Define the XL Vask object + $xlvask = new \classes\xlvask(); + // Require the module to be enabled + $xlvask->requireModuleEnabled(); + // Check if synchronization is enabled + if (!$xlvask->config->synchronization_enabled->isTrue()) { + if (!$isSilent) { + throw new Exception('XL Vask synchronization is not enabled.'); + } + return null; // Synchronization is not enabled, do nothing + } + // Get the vehicles from XL Vask + $vehicles = $xlvask->getVehicles(); + // Cache the vehicles + foreach ( $vehicles as $vehicle ) { + /** @var xlvask_vehicle $vehicle */ + $xlvask->getCache()->setVehicleCache($vehicle->registrationNumber, $vehicle); + } + return $vehicles; // Return the vehicles + } + /** * Run the cleanup tasks * This task is used to clean up the XL Vask module.