<?php
//----------------------------------------------------------------------
// src/AppRemix/Logging/SocietyLog.php
//----------------------------------------------------------------------
namespace App\AppRemix\Logging\Objects;
use Doctrine\Persistence\ManagerRegistry;
use App\AppRemix\Entity\Society;
use App\AppRemix\Logging\Tools;
use App\Services\LogTools;
class SocietyLog
{
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(Society $society)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($society);
$info = $loggingData['info'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($society, $loggingData);
//----------------------------------------------------------------------
$action = "society_add";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Society $society, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($society);
$info = $loggingData['info'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($society, $loggingData);
//----------------------------------------------------------------------
$action = "society_edit";
// This is triggered when we are actually changing the connected Address object
// The changes in the actual object are handled in AddressLog
// info is a json column (siret, ape, tva, RSC, capital, insurance): it must go through
// handleJsonChanges() so the array is encoded before smartLog() cleans it as text.
$jsonChanges = array(
"address",
"info",
);
$basicChanges = array(
"name",
"email",
"phone",
"address",
"info",
"iban",
"stripeToken",
// Not closureRequestedAt: it changes with the flag and would log a second row.
"closureRequested",
);
// 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, $jsonChanges))
{
$pendingLogArgs[] = $this->tools->handleJsonChanges($args, $before, $after);
continue;
}
if (in_array($key, $basicChanges))
{
$pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
continue;
}
}
return $pendingLogArgs;
}
public function logRemoval(Society $society)
{
$pendingLogArgs = [];
return $pendingLogArgs;
}
private function initArgs(Society $society, $loggingData)
{
$args = array(
"object_id" => $society->getId(),
"object_entity" => "Society",
"object_display" => $society->display(),
"society" => $society,
"info" => $loggingData['info'],
);
return $args;
}
}