<?php
//----------------------------------------------------------------------
// src/Logging/HR/Equipment/DamageLog.php
//----------------------------------------------------------------------
namespace App\Logging\HR\Equipment;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Equipment\Damage;
use App\Logging\Tools;
use App\Services\LogTools;
class DamageLog
{
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(Damage $damage)
{
$pendingLogArgs = [];
$equipment = $damage->getEquipment();
if ($equipment === null)
{
return $pendingLogArgs;
}
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($damage);
$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($damage, $loggingData);
//----------------------------------------------------------------------
$action = "equipment_damage_add";
if ($equipment->getVehicle() !== null)
{
$action = 'vehicle_damage_add';
}
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Damage $damage, $changes)
{
$pendingLogArgs = [];
$equipment = $damage->getEquipment();
if ($equipment === null)
{
return $pendingLogArgs;
}
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($damage);
$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($damage, $loggingData);
//----------------------------------------------------------------------
$action = "equipment_damage_edit";
if ($equipment->getVehicle() !== null)
{
$action = 'vehicle_damage_edit';
}
$basicChanges = array(
"info",
);
// 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;
}
}
return $pendingLogArgs;
}
public function logRemoval(Damage $damage)
{
$pendingLogArgs = [];
return $pendingLogArgs;
}
private function initArgs(Damage $damage, $loggingData)
{
$equipment = $damage->getEquipment();
$vehicle = $equipment->getVehicle();
$society = $equipment->getSociety();
$societyGroup = $equipment->getSocietyGroup();
// Add HumanResource logging
$humanResource = $damage->getHumanResource();
if ($vehicle !== null)
{
$objectId = $vehicle->getId();
$objectDisplay = $vehicle->display();
$objectEntity = "Vehicle";
}
else
{
$objectId = $equipment->getId();
$objectDisplay = $equipment->display();
$objectEntity = "Equipment";
}
$args = array(
"object_id" => $objectId,
"object_bundle" => "Equipment",
"object_entity" => $objectEntity,
"object_display" => $objectDisplay,
"object_human_resource_id" => $humanResource !== null ? $humanResource->getId() : null,
"object_human_resource_display" => $humanResource !== null ? $humanResource->display() : null,
"society_group" => $societyGroup,
"society" => $society,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}