<?php
//----------------------------------------------------------------------
// src/Logging/Activity/Prospect/ReportLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity\Prospect;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Client\Client;
use App\Entity\Platform\Report;
use App\Logging\DeletionContextLogger;
use App\Logging\Tools;
use App\Services\LogTools;
class ReportLog
{
private array $pendingLogArgs = [];
public function __construct(ManagerRegistry $doctrine, DeletionContextLogger $contextLogger, LogTools $logTools, Tools $tools)
{
$this->em = $doctrine->getManager();
$this->contextLogger = $contextLogger;
$this->logTools = $logTools;
$this->tools = $tools;
}
public function logCreation(Report $report)
{
// Mandatory conditions
$prospect = $report->getClient();
if ($prospect === null) return [];
if ($prospect->getIndividual() === null) return [];
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($report);
$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($report, $prospect, $loggingData);
//----------------------------------------------------------------------
$action = "prospect_add_report";
$args["action"] = $action;
$args["new_value"] = $report->display();
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Report $report, $changes)
{
// Mandatory conditions
$prospect = $report->getClient();
if ($prospect === null) return [];
if ($prospect->getIndividual() === null) return [];
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($report);
$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($report, $prospect, $loggingData);
//----------------------------------------------------------------------
$action = "prospect_edit_report";
$basicChanges = array(
"info",
"note",
);
$basicBoolChanges = array(
);
$dateChanges = array(
"reportDate",
);
$labelChanges = array(
);
// 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))
{
// Display d/m/Y H:i => Add a fourth param true to handleDateChanges
// 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(Report $report)
{
// Mandatory conditions
$cachedData = $this->contextLogger->getContext($report);
if ($cachedData === null || empty($cachedData)) return [];
if (array_key_exists('prospect', $cachedData) && $cachedData['prospect'] !== null)
{
$prospect = $cachedData['prospect'];
if ($prospect->getIndividual() === null) return [];
}
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($report);
$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($report, $prospect, $loggingData);
//----------------------------------------------------------------------
$args["action"] = "prospect_delete_report";
$args["old_value"] = $report->displayForLogDelete();
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
private function initArgs(Report $report, Client $prospect, $loggingData)
{
$objectEntity = "Client.Prospect";
$receiver = $prospect;
$individual = $prospect->getIndividual();
$society = $individual->getSociety();
$societyGroup = $individual->getSocietyGroup();
$args = array(
"object_id" => $prospect->getId(),
"object_bundle" => "Platform",
"object_entity" => $objectEntity,
"object_display" => $prospect->display(),
"object_client_id" => $receiver !== null ? $receiver->getId() : null,
"object_client_display" => $receiver !== null ? $receiver->getName() : null,
"society_group" => $societyGroup,
"society" => $society,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}