<?php
//----------------------------------------------------------------------
// src/Logging/HR/Equipment/NoteLog.php
//----------------------------------------------------------------------
namespace App\Logging\HR\Equipment;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Equipment\Note;
use App\Logging\DeletionContextLogger;
use App\Logging\Tools;
use App\Services\LogTools;
class NoteLog
{
private array $pendingLogArgs = [];
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, Tools $tools, DeletionContextLogger $contextLogger)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->tools = $tools;
$this->contextLogger = $contextLogger;
}
public function logCreation(Note $note)
{
$pendingLogArgs = [];
$equipment = $note->getEquipment();
if ($equipment === null)
{
return $pendingLogArgs;
}
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($note);
$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($note, $loggingData);
//----------------------------------------------------------------------
$action = "note_equipment_add";
$objectEntity = "EquipmentNote";
if ($equipment->getVehicle() !== null)
{
$action = 'note_vehicle_add';
$objectEntity = 'VehicleNote';
}
$args["action"] = $action;
$args["object_entity"] = $objectEntity;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Note $note, $changes)
{
$pendingLogArgs = [];
$equipment = $note->getEquipment();
if ($equipment === null)
{
return $pendingLogArgs;
}
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($note);
$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($note, $loggingData);
//----------------------------------------------------------------------
$action = "note_equipment_edit";
$objectEntity = "EquipmentNote";
if ($equipment->getVehicle() !== null)
{
$action = 'note_vehicle_edit';
$objectEntity = 'VehicleNote';
}
$args["object_entity"] = $objectEntity;
$basicChanges = array(
"body",
);
$basicBoolChanges = 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;
}
}
return $pendingLogArgs;
}
public function logRemoval(Note $note)
{
$pendingLogArgs = [];
$cachedData = $this->contextLogger->getContext($note);
if ($cachedData === null || empty($cachedData))
{
// Nothing to do here
return $pendingLogArgs;
}
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($note);
$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($note, $loggingData);
//----------------------------------------------------------------------
if (array_key_exists('equipment', $cachedData) && $cachedData['equipment'] !== null)
{
$action = "note_equipment_delete";
$objectEntity = "EquipmentNote";
if ($cachedData['equipment']->getVehicle() !== null)
{
$action = "note_vehicle_delete";
$objectEntity = "VehicleNote";
}
}
$args["action"] = $action;
$args["object_entity"] = $objectEntity;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
private function initArgs(Note $note, $loggingData)
{
$society = $note->getSociety();
$societyGroup = $note->getSocietyGroup();
$args = array(
"object_id" => $note->getId(),
"object_bundle" => "Equipment",
"object_entity" => "EquipmentNote",
"object_display" => $note->display(),
"society_group" => $societyGroup,
"society" => $society,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}