<?php
//----------------------------------------------------------------------
// src/Logging/Media/InodeLog.php
//----------------------------------------------------------------------
namespace App\Logging\Media;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Cloud\Inode;
use App\Logging\Tools;
use App\Services\LogTools;
class CloudInodeLog
{
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(Inode $inode)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($inode);
$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($inode, $loggingData);
//----------------------------------------------------------------------
if ($inode->isFolder())
$action = "inode_folder_add";
else
$action = "inode_file_add";
$args["action"] = $action;
$args["new_value"] = $inode->display();
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Inode $inode, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($inode);
$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($inode, $loggingData);
//----------------------------------------------------------------------
if ($inode->isFolder())
$action = "inode_folder_edit";
else
$action = "inode_file_edit";
$basicChanges = array(
"name",
);
$basicBoolChanges = array(
);
$dateChanges = array(
);
$labelChanges = array(
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
"parent",
);
// 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, $basicChanges))
{
$pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
continue;
}
if (in_array($key, $basicBoolChanges))
{
$pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args, $before, $after);
continue;
}
if (in_array($key, $dateChanges))
{
// Display d/m/Y H:i => Add a fourth param true to handleDateChanges
// 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(Inode $inode)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($inode);
$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($inode, $loggingData);
//----------------------------------------------------------------------
if ($inode->isFolder())
$action = "inode_folder_delete";
else
$action = "inode_file_delete";
$args["action"] = $action;
$args["old_value"] = $inode->display();
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
private function initArgs(Inode $inode, $loggingData)
{
$societyGroup = $inode->getSocietyGroup();
$args = array(
"object_id" => $inode->getId(),
"object_bundle" => "Cloud",
"object_entity" => "Inode",
"object_display" => $inode->display(),
"society_group" => $societyGroup,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}