src/Logging/HR/Equipment/VehicleMaintenanceLog.php line 21

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