<?php
//----------------------------------------------------------------------
// src/Logging/HR/Equipment/VehicleMaintenanceLog.php
//----------------------------------------------------------------------
namespace App\Logging\HR\Equipment;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Equipment\VehicleMaintenance;
use App\Logging\Tools;
use App\Services\LogTools;
class VehicleMaintenanceLog
{
private array $pendingLogArgs = [];
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, Tools $tools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->tools = $tools;
}
public function logCreation(VehicleMaintenance $vehicleMaintenance)
{
$pendingLogArgs = [];
if ($vehicleMaintenance->getVehicle() === null)
{
$pendingLogArgs = [];
}
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($vehicleMaintenance);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($vehicleMaintenance, $loggingData);
//----------------------------------------------------------------------
$action = "vehicle_maintenance_add";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(VehicleMaintenance $vehicleMaintenance, $changes)
{
$pendingLogArgs = [];
if ($vehicleMaintenance->getVehicle() === null)
{
$pendingLogArgs = [];
}
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($vehicleMaintenance);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($vehicleMaintenance, $loggingData);
//----------------------------------------------------------------------
$action = "vehicle_maintenance_edit";
$basicChanges = array(
"km",
"info",
"cost",
);
$basicBoolChanges = array(
);
$dateChanges = array(
);
$labelChanges = array(
"type",
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
);
// See what changed and log (members first)
foreach ($changes as $key => $change)
{
// Something to skip ?
if (false)
{
continue;
}
$name = $this->logTools->camelToSnakeCase($key);
$args["action"] = $action."_".$name;
$before = $change[0];
$after = $change[1];
if (in_array($key, $basicChanges))
{
$pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
continue;
}
if (in_array($key, $basicBoolChanges))
{
$pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args, $before, $after);
continue;
}
if (in_array($key, $dateChanges))
{
// dateChanges can be null
$changeLogs = $this->tools->handleDateChanges($args, $before, $after);
if ($changeLogs !== null) $pendingLogArgs[] = $changeLogs;
continue;
}
if (in_array($key, $labelChanges))
{
$pendingLogArgs[] = $this->tools->handleLabelChanges($args, $before, $after);
continue;
}
if (in_array($key, $objectChanges))
{
$pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
continue;
}
}
return $pendingLogArgs;
}
public function logRemoval(VehicleMaintenance $vehicleMaintenance)
{
$pendingLogArgs = [];
return $pendingLogArgs;
}
private function initArgs(VehicleMaintenance $vehicleMaintenance, $loggingData)
{
$vehicle = $vehicleMaintenance->getVehicle();
$society = $vehicle->getSociety();
$societyGroup = $vehicle->getSocietyGroup();
$args = array(
"object_id" => $vehicle->getId(),
"object_bundle" => "Equipment",
"object_entity" => "Vehicle",
"object_display" => $vehicle->display(),
"society_group" => $societyGroup,
"society" => $society,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}