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

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/EquipmentLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\HR\Equipment;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Equipment\Equipment;
  8. use App\Logging\Tools;
  9. use App\Services\LogTools;
  10. class EquipmentLog
  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(Equipment $equipment)
  20.     {
  21.         $pendingLogArgs = [];
  22.         if ($equipment->getVehicle() !== null)
  23.         {
  24.             // This is already being logged by VehicleLog
  25.             // So skip it here
  26.             return $pendingLogArgs;
  27.         }
  28.         //----------------------------------------------------------------------
  29.         // Fetch eventual info stored in the object
  30.         $loggingData $this->logTools->handleLoggingData($equipment);
  31.         $info $loggingData['info'];
  32.         $specialAuthor $loggingData['special_author'];
  33.         $ignore $loggingData['ignore'];
  34.         if ($ignore) return $pendingLogArgs;
  35.         //----------------------------------------------------------------------
  36.         // Init base log args
  37.         // This does not contain action
  38.         $args $this->initArgs($equipment$loggingData);
  39.         //----------------------------------------------------------------------
  40.         $action "equipment_add";
  41.         $args["action"] = $action;
  42.         $pendingLogArgs[] = $args;
  43.         return $pendingLogArgs;
  44.     }
  45.     public function logChanges(Equipment $equipment$changes)
  46.     {
  47.         $pendingLogArgs = [];
  48.         //----------------------------------------------------------------------
  49.         // Fetch eventual info skeletond in the object
  50.         $loggingData $this->logTools->handleLoggingData($equipment);
  51.         $info $loggingData['info'];
  52.         $specialAuthor $loggingData['special_author'];
  53.         $ignore $loggingData['ignore'];
  54.         if ($ignore) return $pendingLogArgs;
  55.         //----------------------------------------------------------------------
  56.         // Init base log args
  57.         // This does not contain action
  58.         $args $this->initArgs($equipment$loggingData);
  59.         //----------------------------------------------------------------------
  60.         $action "equipment_edit";
  61.         if ($equipment->getVehicle() !== null)
  62.         {
  63.             $action "vehicle_edit";
  64.             $args['object_id'] = $equipment->getVehicle()->getId();
  65.             $args['object_entity'] = "Vehicle";
  66.         }
  67.         // This is triggered when we are actually changing the connected Address object
  68.         // The changes in the actual object are handled in AddressLog
  69.         $addressChanges = array(
  70.         );
  71.         $basicChanges = array(
  72.             "brand",
  73.             "model",
  74.             "ref",
  75.             "funding_contract_ref",
  76.             "insurance",
  77.             'mainImageId',
  78.         );
  79.         $basicBoolChanges = array(
  80.         );
  81.         $dateChanges = array(
  82.             "purchaseDate",
  83.         );
  84.         $labelChanges = array(
  85.             "status",
  86.             "type",
  87.             "state",
  88.             "fundingType",
  89.             "fundingEmitter",
  90.             "fundingDuration",
  91.         );
  92.         // Method getLogLabel() should be defined for these objects/entities
  93.         $objectChanges = array(
  94.             "society",
  95.             "humanResource",
  96.         );
  97.         // See what changed and log (members first)
  98.         foreach ($changes as $key => $change)
  99.         {
  100.             // Something to skip ?
  101.             if (false)
  102.             {
  103.                 continue;
  104.             }
  105.             $name $this->logTools->camelToSnakeCase($key);
  106.             $args["action"]    = $action."_".$name;
  107.             $before $change[0];
  108.             $after $change[1];
  109.             if (in_array($key$addressChanges))
  110.             {
  111.                 $pendingLogArgs[] = $this->tools->handleAddressChanges($args$before$after);
  112.                 continue;
  113.             }
  114.             if (in_array($key$basicChanges))
  115.             {
  116.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  117.                 continue;
  118.             }
  119.             if (in_array($key$basicBoolChanges))
  120.             {
  121.                 $pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args$before$after);
  122.                 continue;
  123.             }
  124.             if (in_array($key$dateChanges))
  125.             {
  126.                 // dateChanges can be null
  127.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  128.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  129.                 continue;
  130.             }
  131.             if (in_array($key$labelChanges))
  132.             {
  133.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  134.                 continue;
  135.             }
  136.             if (in_array($key$objectChanges))
  137.             {
  138.                 $changeLogs $this->tools->handleObjectChanges($args$before$after);
  139.                 
  140.                 if ($key == "humanResource")
  141.                 {
  142.                     if ($before !== null)
  143.                     {
  144.                         $changeLogs["object_human_resource_id"] = $before->getId();
  145.                         $changeLogs["object_human_resource_display"] = $before->display();
  146.                         $pendingLogArgs[] = $changeLogs;
  147.                     }
  148.                     if ($after !== null)
  149.                     {
  150.                         $changeLogs["object_human_resource_id"] = $after->getId();
  151.                         $changeLogs["object_human_resource_display"] = $after->display();
  152.                         $pendingLogArgs[] = $changeLogs;
  153.                     }
  154.                 }
  155.                 else
  156.                 {
  157.                     $pendingLogArgs[] = $changeLogs;
  158.                 }                
  159.                 
  160.                 continue;
  161.             }
  162.         }
  163.         return $pendingLogArgs;
  164.     }
  165.     public function logRemoval(Equipment $equipment)
  166.     {
  167.         $pendingLogArgs = [];
  168.         if ($equipment->getVehicle() !== null)
  169.         {
  170.             // This is already being logged by VehicleLog
  171.             // So skip it here
  172.             return $pendingLogArgs;
  173.         }
  174.         //----------------------------------------------------------------------
  175.         // Fetch eventual info skeletond in the object
  176.         $loggingData $this->logTools->handleLoggingData($equipment);
  177.         $info $loggingData['info'];
  178.         $specialAuthor $loggingData['special_author'];
  179.         $ignore $loggingData['ignore'];
  180.         if ($ignore) return $pendingLogArgs;
  181.         //----------------------------------------------------------------------
  182.         // Init base log args
  183.         // This does not contain action
  184.         $args $this->initArgs($equipment$loggingData);
  185.         //----------------------------------------------------------------------
  186.         $args["action"] = "equipment_delete";
  187.         $pendingLogArgs[] = $args;
  188.         return $pendingLogArgs;
  189.     }
  190.     private function initArgs(Equipment $equipment$loggingData)
  191.     {
  192.         $society $equipment->getSociety();
  193.         $societyGroup $equipment->getSocietyGroup();
  194.         // Not here
  195.         //    => We only want to log the fact that the resource has been 
  196.         //        associated / removed from the equipment
  197.         // Add HumanResource logging
  198.         // $humanResource = $equipment->getHumanResource();
  199.         $args = array(
  200.             "object_id"                    =>    $equipment->getId(),
  201.             "object_bundle"                =>    "Equipment",
  202.             "object_entity"                =>    "Equipment",
  203.             "object_display"            =>    $equipment->display(),
  204.             "society_group"                =>    $societyGroup,
  205.             "society"                    =>    $society,
  206.             // "object_human_resource_id"        =>    $humanResource !== null ? $humanResource->getId() : null,
  207.             // "object_human_resource_display"    =>    $humanResource !== null ? $humanResource->display() : null,
  208.             
  209.             "info"                        =>    $loggingData['info'],
  210.             "special_author"            =>    $loggingData['special_author'],
  211.         );
  212.         return $args;
  213.     }
  214. }