src/Logging/HR/Equipment/NoteLog.php line 18

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/HR/Equipment/NoteLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\HR\Equipment;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Equipment\Note;
  8. use App\Logging\DeletionContextLogger;
  9. use App\Logging\Tools;
  10. use App\Services\LogTools;
  11. class NoteLog
  12. {
  13.     private array $pendingLogArgs = [];
  14.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsTools $toolsDeletionContextLogger $contextLogger)
  15.     {
  16.         $this->em $doctrine->getManager();
  17.         $this->logTools $logTools;
  18.         $this->tools $tools;
  19.         $this->contextLogger $contextLogger;
  20.     }
  21.     public function logCreation(Note $note)
  22.     {
  23.         $pendingLogArgs = [];
  24.         $equipment $note->getEquipment();
  25.         if ($equipment === null)
  26.         {
  27.             return $pendingLogArgs;
  28.         }
  29.         //----------------------------------------------------------------------
  30.         // Fetch eventual info stored in the object
  31.         $loggingData $this->logTools->handleLoggingData($note);
  32.         $info $loggingData['info'];
  33.         $specialAuthor $loggingData['special_author'];
  34.         $ignore $loggingData['ignore'];
  35.         if ($ignore) return $pendingLogArgs;
  36.         //----------------------------------------------------------------------
  37.         // Init base log args
  38.         // This does not contain action
  39.         $args $this->initArgs($note$loggingData);
  40.         //----------------------------------------------------------------------
  41.         $action "note_equipment_add";
  42.         $objectEntity "EquipmentNote";
  43.         if ($equipment->getVehicle() !== null)
  44.         {
  45.             $action 'note_vehicle_add';
  46.             $objectEntity 'VehicleNote';
  47.         }
  48.         $args["action"] = $action;
  49.         $args["object_entity"] = $objectEntity;
  50.         $pendingLogArgs[] = $args;
  51.         return $pendingLogArgs;
  52.     }
  53.     public function logChanges(Note $note$changes)
  54.     {
  55.         $pendingLogArgs = [];
  56.         $equipment $note->getEquipment();
  57.         if ($equipment === null)
  58.         {
  59.             return $pendingLogArgs;
  60.         }
  61.         //----------------------------------------------------------------------
  62.         // Fetch eventual info skeletond in the object
  63.         $loggingData $this->logTools->handleLoggingData($note);
  64.         $info $loggingData['info'];
  65.         $specialAuthor $loggingData['special_author'];
  66.         $ignore $loggingData['ignore'];
  67.         if ($ignore) return $pendingLogArgs;
  68.         //----------------------------------------------------------------------
  69.         // Init base log args
  70.         // This does not contain action
  71.         $args $this->initArgs($note$loggingData);
  72.         //----------------------------------------------------------------------
  73.         $action "note_equipment_edit";
  74.         $objectEntity "EquipmentNote";
  75.         if ($equipment->getVehicle() !== null)
  76.         {
  77.             $action 'note_vehicle_edit';
  78.             $objectEntity 'VehicleNote';
  79.         }
  80.         $args["object_entity"] = $objectEntity;
  81.         $basicChanges = array(
  82.             "body",
  83.         );
  84.         $basicBoolChanges = array(
  85.         );
  86.         // See what changed and log (members first)
  87.         foreach ($changes as $key => $change)
  88.         {
  89.             // Something to skip ?
  90.             if (false)
  91.             {
  92.                 continue;
  93.             }
  94.             $name $this->logTools->camelToSnakeCase($key);
  95.             $args["action"]    = $action."_".$name;
  96.             $before $change[0];
  97.             $after $change[1];
  98.             if (in_array($key$basicChanges))
  99.             {
  100.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  101.                 continue;
  102.             }
  103.             if (in_array($key$basicBoolChanges))
  104.             {
  105.                 $pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args$before$after);
  106.                 continue;
  107.             }
  108.         }
  109.         return $pendingLogArgs;
  110.     }
  111.     public function logRemoval(Note $note)
  112.     {
  113.         $pendingLogArgs = [];
  114.         $cachedData $this->contextLogger->getContext($note);
  115.         if ($cachedData === null || empty($cachedData))
  116.         {
  117.             // Nothing to do here
  118.             return $pendingLogArgs;
  119.         }
  120.         //----------------------------------------------------------------------
  121.         // Fetch eventual info skeletond in the object
  122.         $loggingData $this->logTools->handleLoggingData($note);
  123.         $info $loggingData['info'];
  124.         $specialAuthor $loggingData['special_author'];
  125.         $ignore $loggingData['ignore'];
  126.         if ($ignore) return $pendingLogArgs;
  127.         //----------------------------------------------------------------------
  128.         // Init base log args
  129.         // This does not contain action
  130.         $args $this->initArgs($note$loggingData);
  131.         //----------------------------------------------------------------------
  132.         if (array_key_exists('equipment'$cachedData) && $cachedData['equipment'] !== null)
  133.         {
  134.             $action "note_equipment_delete";
  135.             $objectEntity "EquipmentNote";
  136.             if ($cachedData['equipment']->getVehicle() !== null)
  137.             {
  138.                 $action "note_vehicle_delete";
  139.                 $objectEntity "VehicleNote";
  140.             }
  141.         }
  142.         $args["action"] = $action;
  143.         $args["object_entity"] = $objectEntity;
  144.         $pendingLogArgs[] = $args;
  145.         return $pendingLogArgs;
  146.     }
  147.     private function initArgs(Note $note$loggingData)
  148.     {
  149.         $society $note->getSociety();
  150.         $societyGroup $note->getSocietyGroup();
  151.         $args = array(
  152.             "object_id"                    =>    $note->getId(),
  153.             "object_bundle"                =>    "Equipment",
  154.             "object_entity"                =>    "EquipmentNote",
  155.             "object_display"            =>    $note->display(),
  156.             "society_group"                =>    $societyGroup,
  157.             "society"                    =>    $society,
  158.             "info"                        =>    $loggingData['info'],
  159.             "special_author"            =>    $loggingData['special_author'],
  160.         );
  161.         return $args;
  162.     }
  163. }