<?php
//----------------------------------------------------------------------
// src/AppRemix/Logging/ClientLog.php
//----------------------------------------------------------------------
namespace App\AppRemix\Logging\Objects;
use Doctrine\Persistence\ManagerRegistry;
use App\AppRemix\Entity\Client;
use App\AppRemix\Logging\Tools;
use App\Services\LogTools;
class ClientLog
{
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(Client $client)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($client);
$info = $loggingData['info'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($client, $loggingData);
//----------------------------------------------------------------------
$action = "client_add";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Client $client, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($client);
$info = $loggingData['info'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($client, $loggingData);
//----------------------------------------------------------------------
$action = "client_edit";
// Reachable today through devis/update and invoice/update: company, phone, email.
// name and interventionAddress are not editable yet — listed so the day they are,
// their changes are logged without touching this class.
$basicChanges = array(
"name",
"company",
"phone",
"email",
);
$jsonChanges = array(
"interventionAddress",
);
// 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(Client $client)
{
$pendingLogArgs = [];
return $pendingLogArgs;
}
private function initArgs(Client $client, $loggingData)
{
$args = array(
"object_id" => $client->getId(),
"object_entity" => "Client",
"object_display" => $client->display(),
"society" => $client->getSociety(),
// Null for a client created in this flush — handleClient() fills it post-flush.
"client_id" => $client->getId(),
"client_display" => $client->displayForListing(),
"info" => $loggingData['info'],
);
return $args;
}
}