src/Logging/HR/Equipment/FineLog.php line 17

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/HR/Equipment/FineLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\HR\Equipment;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Equipment\Fine;
  8. use App\Logging\Tools;
  9. use App\Services\LogTools;
  10. class FineLog
  11. {
  12.     private array $pendingLogArgs = [];
  13.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsTools $tools)
  14.     {
  15.         $this->em $doctrine->getManager();
  16.         $this->logTools $logTools;
  17.         $this->tools $tools;
  18.     }
  19.     public function logCreation(Fine $fine)
  20.     {
  21.         $pendingLogArgs = [];
  22.         $vehicle $fine->getVehicle();
  23.         if ($vehicle === null)
  24.         {
  25.             return $pendingLogArgs;
  26.         }
  27.         //----------------------------------------------------------------------
  28.         // Fetch eventual info stored in the object
  29.         $loggingData $this->logTools->handleLoggingData($fine);
  30.         $info $loggingData['info'];
  31.         $specialAuthor $loggingData['special_author'];
  32.         $ignore $loggingData['ignore'];
  33.         if ($ignore) return $pendingLogArgs;
  34.         //----------------------------------------------------------------------
  35.         // Init base log args
  36.         // This does not contain action
  37.         $args $this->initArgs($fine$loggingData);
  38.         //----------------------------------------------------------------------
  39.         $action "vehicle_fine_add";
  40.         $args["action"] = $action;
  41.         $pendingLogArgs[] = $args;
  42.         return $pendingLogArgs;
  43.     }
  44.     public function logChanges(Fine $fine$changes)
  45.     {
  46.         $pendingLogArgs = [];
  47.         $vehicle $fine->getVehicle();
  48.         if ($vehicle === null)
  49.         {
  50.             return $pendingLogArgs;
  51.         }
  52.         //----------------------------------------------------------------------
  53.         // Fetch eventual info skeletond in the object
  54.         $loggingData $this->logTools->handleLoggingData($fine);
  55.         $info $loggingData['info'];
  56.         $specialAuthor $loggingData['special_author'];
  57.         $ignore $loggingData['ignore'];
  58.         if ($ignore) return $pendingLogArgs;
  59.         //----------------------------------------------------------------------
  60.         // Init base log args
  61.         // This does not contain action
  62.         $args $this->initArgs($fine$loggingData);
  63.         //----------------------------------------------------------------------
  64.         $action "vehicle_fine_edit";
  65.         $basicChanges = array(
  66.             "cost",
  67.             "ref",
  68.         );
  69.         $basicBoolChanges = array(
  70.         );
  71.         $dateChanges = array(
  72.         );
  73.         $labelChanges = array(
  74.             "type",
  75.         );
  76.         // Method getLogLabel() should be defined for these objects/entities
  77.         $objectChanges = array(
  78.             "humanResource",
  79.         );
  80.         // See what changed and log (members first)
  81.         foreach ($changes as $key => $change)
  82.         {
  83.             // Something to skip ?
  84.             if (false)
  85.             {
  86.                 continue;
  87.             }
  88.             $name $this->logTools->camelToSnakeCase($key);
  89.             $args["action"]    = $action."_".$name;
  90.             $before $change[0];
  91.             $after $change[1];
  92.             if (in_array($key$basicChanges))
  93.             {
  94.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  95.                 continue;
  96.             }
  97.             if (in_array($key$basicBoolChanges))
  98.             {
  99.                 $pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args$before$after);
  100.                 continue;
  101.             }
  102.             if (in_array($key$dateChanges))
  103.             {
  104.                 // dateChanges can be null
  105.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  106.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  107.                 continue;
  108.             }
  109.             if (in_array($key$labelChanges))
  110.             {
  111.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  112.                 continue;
  113.             }
  114.             if (in_array($key$objectChanges))
  115.             {
  116.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  117.                 continue;
  118.             }
  119.         }
  120.         return $pendingLogArgs;
  121.     }
  122.     public function logRemoval(Fine $fine)
  123.     {
  124.         $pendingLogArgs = [];
  125.         return $pendingLogArgs;
  126.     }
  127.     private function initArgs(Fine $fine$loggingData)
  128.     {
  129.         $vehicle $fine->getVehicle();
  130.         $society $vehicle->getSociety();
  131.         $societyGroup $vehicle->getSocietyGroup();
  132.         // Add HumanResource logging
  133.         $humanResource $vehicle->getHumanResource();
  134.         $args = array(
  135.             "object_id"                    =>    $vehicle->getId(),
  136.             "object_bundle"                =>    "Equipment",
  137.             "object_entity"                =>    "Vehicle",
  138.             "object_display"            =>    $vehicle->display(),
  139.             "object_human_resource_id"        =>    $humanResource !== null $humanResource->getId() : null,
  140.             "object_human_resource_display"    =>    $humanResource !== null $humanResource->display() : null,
  141.             
  142.             "society_group"                =>    $societyGroup,
  143.             "society"                    =>    $society,
  144.             "info"                        =>    $loggingData['info'],
  145.             "special_author"            =>    $loggingData['special_author'],
  146.         );
  147.         return $args;
  148.     }
  149. }