src/Logging/HR/Equipment/DamageLog.php line 19

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/HR/Equipment/DamageLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\HR\Equipment;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Equipment\Damage;
  8. use App\Logging\Tools;
  9. use App\Services\LogTools;
  10. class DamageLog
  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(Damage $damage)
  20.     {
  21.         $pendingLogArgs = [];
  22.         $equipment $damage->getEquipment();
  23.         if ($equipment === null)
  24.         {
  25.             return $pendingLogArgs;
  26.         }
  27.         //----------------------------------------------------------------------
  28.         // Fetch eventual info stored in the object
  29.         $loggingData $this->logTools->handleLoggingData($damage);
  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($damage$loggingData);
  38.         //----------------------------------------------------------------------
  39.         $action "equipment_damage_add";
  40.         if ($equipment->getVehicle() !== null)
  41.         {
  42.             $action 'vehicle_damage_add';
  43.         }
  44.         $args["action"] = $action;
  45.         $pendingLogArgs[] = $args;
  46.         return $pendingLogArgs;
  47.     }
  48.     public function logChanges(Damage $damage$changes)
  49.     {
  50.         $pendingLogArgs = [];
  51.         $equipment $damage->getEquipment();
  52.         if ($equipment === null)
  53.         {
  54.             return $pendingLogArgs;
  55.         }
  56.         //----------------------------------------------------------------------
  57.         // Fetch eventual info skeletond in the object
  58.         $loggingData $this->logTools->handleLoggingData($damage);
  59.         $info $loggingData['info'];
  60.         $specialAuthor $loggingData['special_author'];
  61.         $ignore $loggingData['ignore'];
  62.         if ($ignore) return $pendingLogArgs;
  63.         //----------------------------------------------------------------------
  64.         // Init base log args
  65.         // This does not contain action
  66.         $args $this->initArgs($damage$loggingData);
  67.         //----------------------------------------------------------------------
  68.         $action "equipment_damage_edit";
  69.         if ($equipment->getVehicle() !== null)
  70.         {
  71.             $action 'vehicle_damage_edit';
  72.         }
  73.         $basicChanges = array(
  74.             "info",
  75.         );
  76.         // See what changed and log (members first)
  77.         foreach ($changes as $key => $change)
  78.         {
  79.             // Something to skip ?
  80.             if (false)
  81.             {
  82.                 continue;
  83.             }
  84.             $name $this->logTools->camelToSnakeCase($key);
  85.             $args["action"]    = $action."_".$name;
  86.             $before $change[0];
  87.             $after $change[1];
  88.             if (in_array($key$basicChanges))
  89.             {
  90.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  91.                 continue;
  92.             }
  93.         }
  94.         return $pendingLogArgs;
  95.     }
  96.     public function logRemoval(Damage $damage)
  97.     {
  98.         $pendingLogArgs = [];
  99.         return $pendingLogArgs;
  100.     }
  101.     private function initArgs(Damage $damage$loggingData)
  102.     {
  103.         $equipment $damage->getEquipment();
  104.         $vehicle $equipment->getVehicle();
  105.         $society $equipment->getSociety();
  106.         $societyGroup $equipment->getSocietyGroup();
  107.         // Add HumanResource logging
  108.         $humanResource $damage->getHumanResource();
  109.         if ($vehicle !== null)
  110.         {
  111.             $objectId $vehicle->getId();
  112.             $objectDisplay $vehicle->display();
  113.             $objectEntity "Vehicle";
  114.         }
  115.         else
  116.         {
  117.             $objectId $equipment->getId();
  118.             $objectDisplay $equipment->display();
  119.             $objectEntity "Equipment";
  120.         }
  121.         $args = array(
  122.             "object_id"                    =>    $objectId,
  123.             "object_bundle"                =>    "Equipment",
  124.             "object_entity"                =>    $objectEntity,
  125.             "object_display"            =>    $objectDisplay,
  126.             "object_human_resource_id"        =>    $humanResource !== null $humanResource->getId() : null,
  127.             "object_human_resource_display"    =>    $humanResource !== null $humanResource->display() : null,
  128.             "society_group"                =>    $societyGroup,
  129.             "society"                    =>    $society,
  130.             "info"                        =>    $loggingData['info'],
  131.             "special_author"            =>    $loggingData['special_author'],
  132.         );
  133.         return $args;
  134.     }
  135. }