<?php
//----------------------------------------------------------------------
// src/Logging/PhoneLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Access;
use App\Entity\Platform\Phone;
use App\Logging\DeletionContextLogger;
use App\Logging\Tools;
use App\Services\LogTools;
class PhoneLog
{
private array $pendingLogArgs = [];
public function __construct(ManagerRegistry $doctrine, LogTools $logTools, DeletionContextLogger $contextLogger, Tools $tools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->contextLogger = $contextLogger;
$this->tools = $tools;
}
public function logCreation(Phone $phone)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($phone);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
$individual = $phone->getIndividual();
if ($individual === null)
{
return $pendingLogArgs;
}
if ($individual->getClient() === null)
{
return $pendingLogArgs;
}
if (empty($individual->getClient()->getId()))
{
// Avoid logging phone creation when creating an Individual
return $pendingLogArgs;
}
$society = $individual->getSociety();
if ($society === null)
{
return $pendingLogArgs;
}
$action = "individual_add_phone";
$objectEntity = "Individual";
$args = array(
"action" => $action,
"object_id" => $individual->getId(),
"object_bundle" => "Platform",
"object_entity" => $objectEntity,
"object_display" => $individual->display(),
"object_client_id" => $individual->getClient()->getId(),
"object_client_display" => $individual->getClient()->getName(),
"info" => $info,
"society_group" => $individual->getSociety()->getGroup(),
"society" => $individual->getSociety(),
"new_value" => $phone->displayWithLabel(),
"special_author" => $specialAuthor,
);
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Phone $phone, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($phone);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
$individual = $phone->getIndividual();
if ($individual === null)
{
return $pendingLogArgs;
}
if ($individual->getClient() === null)
{
return $pendingLogArgs;
}
if (empty($individual->getClient()->getId()))
{
// Avoid logging phone creation when creating an Individual
return $pendingLogArgs;
}
$action = "individual_edit_phone";
$objectEntity = "Individual";
// Fill the common arguments
$args = array(
"action" => $action,
"object_id" => $individual->getId(),
"object_bundle" => "Platform",
"object_entity" => $objectEntity,
"object_display" => $individual->display(),
"object_client_id" => $individual->getClient()->getId(),
"object_client_display" => $individual->getClient()->getName(),
"info" => $info,
"society_group" => $individual->getSociety()->getGroup(),
"society" => $individual->getSociety(),
"special_author" => $specialAuthor,
);
$basicChanges = array(
"number",
);
$labelChanges = array(
"label",
);
// See what changed and log (members first)
foreach ($changes as $key => $change)
{
$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;
}
}
return $pendingLogArgs;
}
public function logRemoval(Phone $phone)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($phone);
$info = $loggingData['info'];
$specialAuthor = $loggingData['special_author'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
$cachedData = $this->contextLogger->getContext($phone);
if ($cachedData === null || empty($cachedData))
{
// Nothing to do here
return $pendingLogArgs;
}
if (!array_key_exists('missions', $cachedData) && !array_key_exists('individual', $cachedData))
{
// Nothing to do here
return $pendingLogArgs;
}
$individual = $cachedData['individual'];
if ($individual === null)
{
return $pendingLogArgs;
}
$action = "individual_delete_phone";
$objectEntity = "Individual";
// Fill the common arguments
$args = array(
"action" => $action,
"object_id" => $individual->getId(),
"object_bundle" => "Platform",
"object_entity" => $objectEntity,
"object_display" => $individual->display(),
"object_client_id" => $individual->getClient()->getId(),
"object_client_display" => $individual->getClient()->getName(),
"info" => $info,
"society_group" => $individual->getSociety()->getGroup(),
"society" => $individual->getSociety(),
"old_value" => $phone->displayWithLabel(),
"special_author" => $specialAuthor,
);
$pendingLogArgs[] = $args;
// Next => Missions
// Log for $mission.societyGroupAuthor only
$action = "mission_delete_phone_from_individual";
$objectEntity = "Mission";
foreach ($cachedData['missions'] as $mission)
{
$args = array(
"action" => $action,
"object_id" => $mission->getId(),
"object_bundle" => "Mission",
"object_entity" => $objectEntity,
"object_display" => $mission->display(),
"object_client_id" => $mission->getReceiver()->getId(),
"object_client_display" => $mission->getReceiver()->getName(),
"info" => $info,
"society_group" => $mission->getSocietyGroupAuthor(),
"society" => $mission->getSociety(),
"old_value" => $phone->displayWithLabel(),
"special_author" => $specialAuthor,
);
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
}