src/AppRemix/Logging/Objects/ProductLog.php line 21

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/AppRemix/Logging/ProductLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\AppRemix\Logging\Objects;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\AppRemix\Entity\Product;
  8. use App\AppRemix\Logging\Tools;
  9. use App\Services\LogTools;
  10. class ProductLog
  11. {
  12.     private array $pendingLogArgs = [];
  13.     public function __construct(ManagerRegistry $doctrine, LogTools $logTools, Tools $tools)
  14.     {
  15.         $this->em = $doctrine->getManager();
  16.         $this->logTools = $logTools;
  17.         $this->tools = $tools;
  18.     }
  19.     public function logCreation(Product $product)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData = $this->logTools->handleLoggingData($product);
  25.         $info = $loggingData['info'];        
  26.         $ignore = $loggingData['ignore'];
  27.         if ($ignore) return $pendingLogArgs;
  28.         //----------------------------------------------------------------------
  29.         // Init base log args
  30.         // This does not contain action
  31.         $args = $this->initArgs($product, $loggingData);
  32.         //----------------------------------------------------------------------
  33.         $action = "product_add";
  34.         $args["action"] = $action;
  35.         $pendingLogArgs[] = $args;
  36.         return $pendingLogArgs;
  37.     }
  38.     public function logChanges(Product $product, $changes)
  39.     {
  40.         $pendingLogArgs = [];
  41.         //----------------------------------------------------------------------
  42.         // Fetch eventual info stored in the object
  43.         $loggingData = $this->logTools->handleLoggingData($product);
  44.         $info = $loggingData['info'];        
  45.         $ignore = $loggingData['ignore'];
  46.         if ($ignore) return $pendingLogArgs;
  47.         //----------------------------------------------------------------------
  48.         // Init base log args
  49.         // This does not contain action
  50.         $args = $this->initArgs($product, $loggingData);
  51.         //----------------------------------------------------------------------
  52.         $action = "product_edit";
  53.         // This is triggered when we are actually changing the connected Address object
  54.         // The changes in the actual object are handled in AddressLog
  55.         $jsonChanges = array(
  56.         );
  57.         $basicChanges = array(                    
  58.             "title",
  59.             "unit",
  60.             "price",
  61.             "template",
  62.         );
  63.         // See what changed and log (members first)
  64.         foreach ($changes as $key => $change)
  65.         {
  66.             // Something to skip ?
  67.             if (false)
  68.             {
  69.                 continue;
  70.             }
  71.             $name = $this->logTools->camelToSnakeCase($key);
  72.             $args["action"]    = $action."_".$name;
  73.             $before = $change[0];
  74.             $after = $change[1];
  75.             if (in_array($key, $basicChanges))
  76.             {
  77.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
  78.                 continue;
  79.             }
  80.             if (in_array($key, $jsonChanges))
  81.             {
  82.                 $pendingLogArgs[] = $this->tools->handleJsonChanges($args, $before, $after);
  83.                 continue;
  84.             }
  85.         }
  86.         return $pendingLogArgs;
  87.     }
  88.     public function logRemoval(Product $product)
  89.     {
  90.         $pendingLogArgs = [];
  91.         //----------------------------------------------------------------------
  92.         // Fetch eventual info stored in the object
  93.         $loggingData = $this->logTools->handleLoggingData($product);
  94.         $info = $loggingData['info'];        
  95.         $ignore = $loggingData['ignore'];
  96.         if ($ignore) return $pendingLogArgs;
  97.         //----------------------------------------------------------------------
  98.         // Init base log args
  99.         // This does not contain action
  100.         $args = $this->initArgs($product, $loggingData);
  101.         //----------------------------------------------------------------------
  102.         $args["action"] = "product_delete";
  103.         $pendingLogArgs[] = $args;
  104.         return $pendingLogArgs;
  105.     }
  106.     private function initArgs(Product $product, $loggingData)
  107.     {
  108.         $args = array(
  109.             "object_id"                    =>    $product->getId(),            
  110.             "object_entity"                =>    "Product",
  111.             "object_display"            =>    $product->display(),
  112.             
  113.             "society"                    =>    $product->getSociety(),
  114.             "info"                        =>    $loggingData['info'],            
  115.         );
  116.         return $args;
  117.     }
  118. }