<?php
//----------------------------------------------------------------------
// src/Logging/OPVarLog.php
//----------------------------------------------------------------------
namespace App\Logging\HR;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\HR\HumanResource;
use App\Entity\HR\Salary\OPVariable;
use App\Logging\DeletionContextLogger;
use App\Logging\Tools;
use App\Services\LogTools;
class OPVarLog
{
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(OPVariable $opVar)
{
$pendingLogArgs = [];
$humanResource = $opVar->getHumanResource();
if ($humanResource === null)
{
// Nothing to do here
return $pendingLogArgs;
}
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($opVar);
$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($opVar, $humanResource, $loggingData);
//----------------------------------------------------------------------
$action = "human_resource_add_op_var";
$args["action"] = $action;
$args["new_value"] = $opVar->display();
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(OPVariable $opVar, $changes)
{
$pendingLogArgs = [];
$humanResource = $opVar->getHumanResource();
if ($humanResource === null)
{
// Nothing to do here
return $pendingLogArgs;
}
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($opVar);
$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($opVar, $humanResource, $loggingData);
//----------------------------------------------------------------------
$action = "human_resource_edit_op_var";
$basicChanges = array(
"value",
"info",
);
$basicBoolChanges = array(
);
$dateChanges = array(
"startDate",
);
$labelChanges = array(
"status",
"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(OPVariable $opVar)
{
$pendingLogArgs = [];
$humanResource = null;
$cachedData = $this->contextLogger->getContext($opVar);
if ($cachedData !== null && !empty($cachedData))
{
if (array_key_exists('humanResource', $cachedData) && $cachedData['humanResource'] !== null)
{
$humanResource = $cachedData['humanResource'];
}
}
if ($humanResource === null)
{
// Nothing to do here
return $pendingLogArgs;
}
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($opVar);
$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($opVar, $humanResource, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "human_resource_delete_op_var";
$args["old_value"] = $opVar->display();
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
private function initArgs(OPVariable $opVar, HumanResource $humanResource, $loggingData)
{
$society = $humanResource->getSociety();
$societyGroup = $humanResource->getSocietyGroup();
$args = array(
"object_id" => $humanResource->getId(),
"object_bundle" => "HR",
"object_entity" => "HumanResource",
"object_display" => $humanResource->display(),
"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;
}
}