<?php
//----------------------------------------------------------------------
// src/Logging/Activity/AccessClientLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\AccessClient\AccessClient;
use App\Entity\AccessClient\AccessClientRecord;
use App\Logging\Tools;
use App\Services\LogTools;
class AccessClientLog
{
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(AccessClient $accessClient)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($accessClient);
$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($accessClient, $loggingData);
//----------------------------------------------------------------------
$action = "access_client_add";
foreach ($accessClient->getAccessClientRecords() as $accessClientRecord)
{
$args = $this->initArgs($accessClient, $accessClientRecord, $loggingData);
$args["action"] = $action;
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logChanges(AccessClient $accessClient, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($accessClient);
$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($accessClient, $loggingData);
//----------------------------------------------------------------------
$action = "access_client_edit";
// This is triggered when we are actually changing the connected Address object
// The changes in the actual object are handled in AddressLog
$addressChanges = array(
);
$basicChanges = array(
"password",
"lostPwdCode",
);
$basicBoolChanges = array(
"isActive",
"denyEmail",
);
$dateChanges = array(
"activationDate",
);
$labelChanges = array(
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
);
foreach ($accessClient->getAccessClientRecords() as $accessClientRecord)
{
$args = $this->initArgs($accessClient, $accessClientRecord, $loggingData);
// 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, $addressChanges))
{
$pendingLogArgs[] = $this->tools->handleAddressChanges($args, $before, $after);
continue;
}
if (in_array($key, $basicChanges))
{
$args = $this->tools->handleBasicChanges($args, $before, $after);
if ($key == "password")
{
$args['old_value'] = null;
$args['new_value'] = null;
}
$pendingLogArgs[] = $args;
continue;
}
if (in_array($key, $basicBoolChanges))
{
$args = $this->tools->handleBasicBoolChanges($args, $before, $after);
if ($key == "isActive")
{
if (!$before && $after) $args['action'] = "access_client_activate";
if ($before && !$after) $args['action'] = "access_client_deactivate";
$args['old_value'] = null;
$args['new_value'] = null;
}
$pendingLogArgs[] = $args;
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(AccessClient $accessClient)
{
$pendingLogArgs = [];
return $pendingLogArgs;
}
private function initArgs(AccessClient $accessClient, AccessClientRecord $accessClientRecord, $loggingData)
{
// Returns the Society of the AccessClient.humanResource
$society = $accessClientRecord->getClient()->getSociety();
$societyGroup = $accessClientRecord->getSocietyGroup();
$args = array(
"object_id" => $accessClient->getId(),
"object_bundle" => "Activity",
"object_entity" => "AccessClient",
"object_display" => $accessClient->display(),
"society_group" => $societyGroup,
"society" => $society,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}