Add runSyncVehicles method to XLVask helpers for vehicle synchronization and caching.

This commit is contained in:
Jepp9350
2025-06-18 15:34:07 +02:00
parent 6ee8c0e2b1
commit 3bf1292775
@@ -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.