<?php
//----------------------------------------------------------------------
// src/Logging/HR\Equipment\VehicleMaintenanceDeadlineLog.php
//----------------------------------------------------------------------
namespace App\Logging\HR\Equipment;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\PropertyAccess\PropertyAccess;
use App\Entity\Equipment\Vehicle;
use App\Entity\Equipment\VehicleMaintenanceDeadline;
use App\Logging\DeletionContextLogger;
use App\Services\LogTools;
class VehicleMaintenanceDeadlineLog
{
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, DeletionContextLogger $contextLogger)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->contextLogger = $contextLogger;
}
public function logCreation(VehicleMaintenanceDeadline $vmd)
{
return [];
}
public function logChanges(VehicleMaintenanceDeadline $vmd, $changes)
{
$logs = $this->logVehicle($vmd, $changes);
if (!empty($logs))
{
return $logs;
}
// Nothing was found
return [];
}
public function logRemoval(VehicleMaintenanceDeadline $vmd)
{
return [];
}
public function logVehicle(VehicleMaintenanceDeadline $vmd, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($vmd);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Look for Vehicle
$action = null;
$vehicle = $vmd->getVehicle();
if ($vehicle !== null)
{
$action = "vehicle_edit_maintenance_deadline_";
}
if ($action === null)
{
// The owner of the given address was not identified
return [];
}
// If we are here it means that we have an owner and an action
// Fill the common arguments
$args = array(
"action" => $action,
"object_id" => $vehicle->getId(),
"object_bundle" => "Equipment",
"object_entity" => "Vehicle",
"object_display" => $vehicle->display(),
"info" => $info,
"society_group" => $vehicle->getSocietyGroup(),
"society" => $vehicle->getSociety(),
"special_author" => $specialAuthor,
);
$changesLogged = $this->computeChanges($vmd, $changes);
foreach ($changesLogged as $key => $changeLogged)
{
$args["old_value"] = $changeLogged["old_value"];
$args["new_value"] = $changeLogged["new_value"];
$args["action"] = $action . $changeLogged["name"];
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function computeChanges(VehicleMaintenanceDeadline $vmd, $changesSet)
{
if (count($changesSet) < 1)
return null;
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$changesLogged = array();
$changesToWatch = array("years", "km");
foreach ($changesSet as $key => $change)
{
$name = $this->logTools->camelToSnakeCase($key);
if (in_array($key, $changesToWatch))
{
if (empty($change[0]) && empty($change[1])) continue;
$changesLogged[] = array(
"name" => $name,
"old_value" => $change[0],
"new_value" => $change[1],
);
}
}
return $changesLogged;
}
}