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

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/HR\Equipment\VehicleMaintenanceDeadlineLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\HR\Equipment;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\PropertyAccess\PropertyAccess;
  8. use App\Entity\Equipment\Vehicle;
  9. use App\Entity\Equipment\VehicleMaintenanceDeadline;
  10. use App\Logging\DeletionContextLogger;
  11. use App\Services\LogTools;
  12. class VehicleMaintenanceDeadlineLog
  13. {
  14.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsDeletionContextLogger $contextLogger)
  15.     {
  16.         $this->em $doctrine->getManager();
  17.         $this->logTools $logTools;
  18.         $this->contextLogger $contextLogger;
  19.     }
  20.     public function logCreation(VehicleMaintenanceDeadline $vmd)
  21.     {
  22.         return [];
  23.     }
  24.     public function logChanges(VehicleMaintenanceDeadline $vmd$changes)
  25.     {
  26.         $logs $this->logVehicle($vmd$changes);
  27.         if (!empty($logs))
  28.         {
  29.             return $logs;
  30.         }
  31.         // Nothing was found
  32.         return [];
  33.     }
  34.     public function logRemoval(VehicleMaintenanceDeadline $vmd)
  35.     {
  36.         return [];
  37.     }
  38.     public function logVehicle(VehicleMaintenanceDeadline $vmd$changes)
  39.     {
  40.         $pendingLogArgs = [];
  41.         //----------------------------------------------------------------------
  42.         // Fetch eventual info stored in the object
  43.         $loggingData $this->logTools->handleLoggingData($vmd);
  44.         $info $loggingData['info'];
  45.         $specialAuthor $loggingData['special_author'];
  46.         $ignore $loggingData['ignore'];
  47.         if ($ignore) return $pendingLogArgs;
  48.         //----------------------------------------------------------------------
  49.         // Look for Vehicle
  50.         $action null;
  51.         $vehicle $vmd->getVehicle();
  52.         if ($vehicle !== null)
  53.         {
  54.             $action "vehicle_edit_maintenance_deadline_";
  55.         }
  56.         if ($action === null)
  57.         {
  58.             // The owner of the given address was not identified
  59.             return [];
  60.         }
  61.         // If we are here it means that we have an owner and an action
  62.         // Fill the common arguments
  63.         $args = array(
  64.             "action"                    =>    $action,
  65.             "object_id"                    =>    $vehicle->getId(),
  66.             "object_bundle"                =>    "Equipment",
  67.             "object_entity"                =>    "Vehicle",
  68.             "object_display"            =>    $vehicle->display(),
  69.             "info"                        =>    $info,
  70.             "society_group"                =>    $vehicle->getSocietyGroup(),
  71.             "society"                    =>    $vehicle->getSociety(),
  72.             "special_author"            =>    $specialAuthor,
  73.         );
  74.         $changesLogged $this->computeChanges($vmd$changes);
  75.         foreach ($changesLogged as $key => $changeLogged)
  76.         {
  77.             $args["old_value"]    = $changeLogged["old_value"];
  78.             $args["new_value"]    = $changeLogged["new_value"];
  79.             $args["action"]    = $action $changeLogged["name"];
  80.             $pendingLogArgs[] = $args;
  81.         }
  82.         return $pendingLogArgs;
  83.     }
  84.     public function computeChanges(VehicleMaintenanceDeadline $vmd$changesSet)
  85.     {
  86.         if (count($changesSet) < 1)
  87.             return null;
  88.         $propertyAccessor PropertyAccess::createPropertyAccessor();
  89.         $changesLogged = array();
  90.         $changesToWatch = array("years""km");
  91.         foreach ($changesSet as $key => $change)
  92.         {
  93.             $name $this->logTools->camelToSnakeCase($key);
  94.             if (in_array($key$changesToWatch))
  95.             {
  96.                 if (empty($change[0]) && empty($change[1]))    continue;
  97.                 $changesLogged[] = array(
  98.                     "name" => $name,
  99.                     "old_value" => $change[0],
  100.                     "new_value" => $change[1],
  101.                 );
  102.             }
  103.         }
  104.         return $changesLogged;
  105.     }
  106. }