<?php
//----------------------------------------------------------------------
// src/Logging/EquipmentLog.php
//----------------------------------------------------------------------
namespace App\Logging\HR\Equipment;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Equipment\Equipment;
use App\Logging\Tools;
use App\Services\LogTools;
class EquipmentLog
{
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(Equipment $equipment)
{
$pendingLogArgs = [];
if ($equipment->getVehicle() !== null)
{
// This is already being logged by VehicleLog
// So skip it here
return $pendingLogArgs;
}
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($equipment);
$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($equipment, $loggingData);
//----------------------------------------------------------------------
$action = "equipment_add";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Equipment $equipment, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($equipment);
$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($equipment, $loggingData);
//----------------------------------------------------------------------
$action = "equipment_edit";
if ($equipment->getVehicle() !== null)
{
$action = "vehicle_edit";
$args['object_id'] = $equipment->getVehicle()->getId();
$args['object_entity'] = "Vehicle";
}
// This is triggered when we are actually changing the connected Address object
// The changes in the actual object are handled in AddressLog
$addressChanges = array(
);
$basicChanges = array(
"brand",
"model",
"ref",
"funding_contract_ref",
"insurance",
'mainImageId',
);
$basicBoolChanges = array(
);
$dateChanges = array(
"purchaseDate",
);
$labelChanges = array(
"status",
"type",
"state",
"fundingType",
"fundingEmitter",
"fundingDuration",
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
"society",
"humanResource",
);
// 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, $addressChanges))
{
$pendingLogArgs[] = $this->tools->handleAddressChanges($args, $before, $after);
continue;
}
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))
{
$changeLogs = $this->tools->handleObjectChanges($args, $before, $after);
if ($key == "humanResource")
{
if ($before !== null)
{
$changeLogs["object_human_resource_id"] = $before->getId();
$changeLogs["object_human_resource_display"] = $before->display();
$pendingLogArgs[] = $changeLogs;
}
if ($after !== null)
{
$changeLogs["object_human_resource_id"] = $after->getId();
$changeLogs["object_human_resource_display"] = $after->display();
$pendingLogArgs[] = $changeLogs;
}
}
else
{
$pendingLogArgs[] = $changeLogs;
}
continue;
}
}
return $pendingLogArgs;
}
public function logRemoval(Equipment $equipment)
{
$pendingLogArgs = [];
if ($equipment->getVehicle() !== null)
{
// This is already being logged by VehicleLog
// So skip it here
return $pendingLogArgs;
}
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($equipment);
$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($equipment, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "equipment_delete";
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
private function initArgs(Equipment $equipment, $loggingData)
{
$society = $equipment->getSociety();
$societyGroup = $equipment->getSocietyGroup();
// Not here
// => We only want to log the fact that the resource has been
// associated / removed from the equipment
// Add HumanResource logging
// $humanResource = $equipment->getHumanResource();
$args = array(
"object_id" => $equipment->getId(),
"object_bundle" => "Equipment",
"object_entity" => "Equipment",
"object_display" => $equipment->display(),
"society_group" => $societyGroup,
"society" => $society,
// "object_human_resource_id" => $humanResource !== null ? $humanResource->getId() : null,
// "object_human_resource_display" => $humanResource !== null ? $humanResource->display() : null,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}