src/AppRemix/Logging/Objects/FileLog.php line 17

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/AppRemix/Logging/FileLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\AppRemix\Logging\Objects;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\AppRemix\Entity\File;
  8. use App\AppRemix\Logging\Tools;
  9. use App\Services\LogTools;
  10. class FileLog
  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(File $file)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData = $this->logTools->handleLoggingData($file);
  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($file, $loggingData);
  32.         //----------------------------------------------------------------------
  33.         $action = "file_add";
  34.         $args["action"] = $action;
  35.         $pendingLogArgs[] = $args;
  36.         return $pendingLogArgs;
  37.     }
  38.     public function logChanges(File $file, $changes)
  39.     {
  40.         $pendingLogArgs = [];
  41.         //----------------------------------------------------------------------
  42.         // Fetch eventual info stored in the object
  43.         $loggingData = $this->logTools->handleLoggingData($file);
  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($file, $loggingData);
  51.         //----------------------------------------------------------------------
  52.         $action = "file_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.             "name",            
  59.         );
  60.         // Method getLogLabel() should be defined for these objects/entities
  61.         $objectChanges = array(            
  62.             "type",        
  63.         );
  64.         $boolChanges = array(            
  65.             "favorite",
  66.         );
  67.         // See what changed and log (members first)
  68.         foreach ($changes as $key => $change)
  69.         {
  70.             // Something to skip ?
  71.             if (false)
  72.             {
  73.                 continue;
  74.             }
  75.             $name = $this->logTools->camelToSnakeCase($key);
  76.             $args["action"]    = $action."_".$name;
  77.             $before = $change[0];
  78.             $after = $change[1];
  79.             if (in_array($key, $basicChanges))
  80.             {
  81.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
  82.                 continue;
  83.             }
  84.             if (in_array($key, $boolChanges))
  85.             {
  86.                 $pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args, $before, $after);
  87.                 continue;
  88.             }
  89.             if (in_array($key, $objectChanges))
  90.             {
  91.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
  92.                 continue;
  93.             }
  94.             if (in_array($key, $jsonChanges))
  95.             {
  96.                 $pendingLogArgs[] = $this->tools->handleJsonChanges($args, $before, $after);
  97.                 continue;
  98.             }
  99.         }
  100.         return $pendingLogArgs;
  101.     }
  102.     public function logRemoval(File $file)
  103.     {
  104.         $pendingLogArgs = [];
  105.         //----------------------------------------------------------------------
  106.         // Fetch eventual info stored in the object
  107.         $loggingData = $this->logTools->handleLoggingData($file);
  108.         $info = $loggingData['info'];        
  109.         $ignore = $loggingData['ignore'];
  110.         if ($ignore) return $pendingLogArgs;
  111.         //----------------------------------------------------------------------
  112.         // Init base log args
  113.         // This does not contain action
  114.         $args = $this->initArgs($file, $loggingData);
  115.         //----------------------------------------------------------------------
  116.         $args["action"] = "file_delete";
  117.         $pendingLogArgs[] = $args;
  118.         return $pendingLogArgs;
  119.     }
  120.     private function initArgs(File $file, $loggingData)
  121.     {
  122.         $mission = $file->getMission();
  123.         $args = array(
  124.             "object_id"                    =>    $file->getId(),            
  125.             "object_entity"                =>    "File",
  126.             "object_display"            =>    $file->display(),
  127.             
  128.             "society"                    =>    $file->getSociety(),
  129.             "mission_id"                =>    $mission !== null ? $mission->getId() : null,
  130.             "mission_display"            =>    $mission !== null ? $mission->display() : null,
  131.             "info"                        =>    $loggingData['info'],            
  132.         );
  133.         return $args;
  134.     }
  135. }