<?php
//----------------------------------------------------------------------
// src/AppRemix/Logging/PaymentLog.php
//----------------------------------------------------------------------
namespace App\AppRemix\Logging\Objects;
use Doctrine\Persistence\ManagerRegistry;
use App\AppRemix\Entity\Payment;
use App\AppRemix\Logging\Tools;
use App\Services\LogTools;
class PaymentLog
{
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(Payment $payment)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($payment);
$info = $loggingData['info'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($payment, $loggingData);
//----------------------------------------------------------------------
$action = "payment_add";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Payment $payment, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($payment);
$info = $loggingData['info'];
$ignore = $loggingData['ignore'];
if ($ignore) return $pendingLogArgs;
//----------------------------------------------------------------------
// Init base log args
// This does not contain action
$args = $this->initArgs($payment, $loggingData);
//----------------------------------------------------------------------
$editMode = "payment_edit";
$args["action"] = $editMode;
$jsonChanges = array(
);
$basicChanges = array(
"amount",
"urlPayment",
"stripeLinkId",
"info",
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
"devis",
"invoice",
);
$dateChanges = array(
"paymentDate",
"sentToClient",
"creationDate",
);
// 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"] = $editMode."_".$name;
$before = $change[0];
$after = $change[1];
// Handle Special cases
if ($key == "status")
{
$pendingLogArgs[] = $this->tools->handlePaymentStatusChanges($args, $before, $after);
continue;
}
if ($key == "type")
{
$pendingLogArgs[] = $this->tools->handlePaymentTypeChanges($args, $before, $after);
continue;
}
if (in_array($key, $basicChanges))
{
$pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
continue;
}
if (in_array($key, $objectChanges))
{
$pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
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, $jsonChanges))
{
$pendingLogArgs[] = $this->tools->handleJsonChanges($args, $before, $after);
continue;
}
}
return $pendingLogArgs;
}
public function logRemoval(Payment $payment)
{
$pendingLogArgs = [];
return $pendingLogArgs;
}
private function initArgs(Payment $payment, $loggingData)
{
$client = $payment->getClient();
$args = array(
"object_id" => $payment->getId(),
"object_entity" => "Payment",
"object_display" => $payment->display(),
"society" => $payment->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;
}
}