<?php
//----------------------------------------------------------------------
// src/Logging/ProductLog.php
//----------------------------------------------------------------------
namespace App\Logging\Activity;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Product\Product;
use App\Logging\Tools;
use App\Services\LogTools;
class ProductLog
{
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(Product $product)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info stored in the object
$loggingData = $this->logTools->handleLoggingData($product);
$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($product, $loggingData);
//----------------------------------------------------------------------
if ($product->isDiscount())
$action = "discount_add";
else
$action = "product_add";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
public function logChanges(Product $product, $changes)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($product);
$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($product, $loggingData);
//----------------------------------------------------------------------
if ($product->isDiscount())
$action = "discount_edit";
else
$action = "product_edit";
$basicChanges = array(
"ref",
"title",
"description",
"publicPrice",
"internalPrice",
"ecoBonus",
);
$labelChanges = array(
"unit",
);
// Method getLogLabel() should be defined for these objects/entities
$objectChanges = array(
"question",
"logo",
);
$basicBoolChanges = array(
"readonly",
"samePrice",
);
// 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))
{
if (($key == 'publicPrice' || $key == 'internalPrice'))
{
if (floatval($change[0]) == floatval($change[1]))
{
continue;
}
}
$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, $labelChanges))
{
$pendingLogArgs[] = $this->tools->handleLabelChanges($args, $before, $after);
continue;
}
if (in_array($key, $objectChanges))
{
$pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
continue;
}
}
// Handle ManyToMany
if ($product->getTemplates()->isDirty())
{
$oldData = "";
$newData = "";
foreach ($product->getTemplates()->getSnapshot() as $template)
{
$oldData .= $template->display();
$oldData .= ", ";
}
foreach ($product->getTemplates() as $template)
{
$newData .= $template->display();
$newData .= ", ";
}
if ($oldData != "")
{
$oldData = substr($oldData, 0, -1);
$oldData = substr($oldData, 0, -1);
}
if ($newData != "")
{
$newData = substr($newData, 0, -1);
$newData = substr($newData, 0, -1);
}
$args["action"] = "product_edit_templates";
$args["old_value"] = $oldData;
$args["new_value"] = $newData;
$pendingLogArgs[] = $args;
}
return $pendingLogArgs;
}
public function logRemoval(Product $product)
{
$pendingLogArgs = [];
//----------------------------------------------------------------------
// Fetch eventual info skeletond in the object
$loggingData = $this->logTools->handleLoggingData($product);
$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($product, $loggingData);
//----------------------------------------------------------------------
if ($product->isDiscount())
$action = "discount_delete";
else
$action = "product_delete";
$args["action"] = $action;
$pendingLogArgs[] = $args;
return $pendingLogArgs;
}
private function initArgs(Product $product, $loggingData)
{
$societyGroup = $product->getSocietyGroup();
$args = array(
"object_id" => $product->getId(),
"object_bundle" => "Product",
"object_entity" => "Product",
"object_display" => $product->display(),
"society_group" => $societyGroup,
"info" => $loggingData['info'],
"special_author" => $loggingData['special_author'],
);
return $args;
}
}