<?php
//----------------------------------------------------------------------
// src/Logging/ChargeLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Product\Charge;
use App\Logging\Tools;
use App\Services\LogTools;
class ChargeLog
{
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(Charge $charge)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($charge);
$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($charge, $loggingData);
//----------------------------------------------------------------------
$action = "charge_add";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Charge $charge, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($charge);
$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($charge, $loggingData);
//----------------------------------------------------------------------
$action = "charge_edit";
$basicChanges = array(
"ref",
"title",
"duration"
);
$labelChanges = array(
"unit",
);
// 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, $labelChanges))
{
$pendingLogArgs[] = $this->tools->handleLabelChanges($args, $before, $after);
continue;
}
if (in_array($key, $objectChanges))
{
$pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
continue;
}
}
// Handle ManyToMany
if ($charge->getTemplates()->isDirty())
{
$oldData = "";
$newData = "";
foreach ($charge->getTemplates()->getSnapshot() as $template)
{
$oldData .= $template->display();
$oldData .= ", ";
}
foreach ($charge->getTemplates() as $template)
{
$newData .= $template->display();
$newData .= ", ";
}
if ($oldData != "")
{
$oldData = substr($oldData, 0, -1);
$oldData = substr($oldData, 0, -1);
}
if ($newData != "")
{
$newData = substr($newData, 0, -1);
$newData = substr($newData, 0, -1);
}
$args["action"] = "charge_edit_templates";
$args["old_value"] = $oldData;
$args["new_value"] = $newData;
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logRemoval(Charge $charge)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($charge);
$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($charge, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "charge_delete";
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
private function initArgs(Charge $charge, $loggingData)
{
$societyGroup = $charge->getSocietyGroup();
$args = array(
"object_id" => $charge->getId(),
"object_bundle" => "Product",
"object_entity" => "Charge",
"object_display" => $charge->display(),
"society_group" => $societyGroup,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}