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

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/VehicleLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\HR\Equipment;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Equipment\Vehicle;
  8. use App\Logging\Tools;
  9. use App\Services\LogTools;
  10. class VehicleLog
  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(Vehicle $vehicle)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData $this->logTools->handleLoggingData($vehicle);
  25.         $info $loggingData['info'];
  26.         $specialAuthor $loggingData['special_author'];
  27.         $ignore $loggingData['ignore'];
  28.         if ($ignore) return $pendingLogArgs;
  29.         //----------------------------------------------------------------------
  30.         // Init base log args
  31.         // This does not contain action
  32.         $args $this->initArgs($vehicle$loggingData);
  33.         //----------------------------------------------------------------------
  34.         $action "vehicle_add";
  35.         $args["action"] = $action;
  36.         $pendingLogArgs[] = $args;
  37.         return $pendingLogArgs;
  38.     }
  39.     public function logChanges(Vehicle $vehicle$changes)
  40.     {
  41.         $pendingLogArgs = [];
  42.         //----------------------------------------------------------------------
  43.         // Fetch eventual info skeletond in the object
  44.         $loggingData $this->logTools->handleLoggingData($vehicle);
  45.         $info $loggingData['info'];
  46.         $specialAuthor $loggingData['special_author'];
  47.         $ignore $loggingData['ignore'];
  48.         if ($ignore) return $pendingLogArgs;
  49.         //----------------------------------------------------------------------
  50.         // Init base log args
  51.         // This does not contain action
  52.         $args $this->initArgs($vehicle$loggingData);
  53.         //----------------------------------------------------------------------
  54.         $action "vehicle_edit";
  55.         // This is triggered when we are actually changing the connected Address object
  56.         // The changes in the actual object are handled in AddressLog
  57.         $addressChanges = array(
  58.         );
  59.         $basicChanges = array(
  60.             "fuel",
  61.             "color",
  62.             "fuelCard",
  63.             "speedyCard",
  64.             "highwayToll",
  65.             'registrationImageId',
  66.         );
  67.         $basicBoolChanges = array(
  68.         );
  69.         $dateChanges = array(
  70.         );
  71.         $labelChanges = array(
  72.         );
  73.         // Method getLogLabel() should be defined for these objects/entities
  74.         $objectChanges = array(
  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$addressChanges))
  89.             {
  90.                 $pendingLogArgs[] = $this->tools->handleAddressChanges($args$before$after);
  91.                 continue;
  92.             }
  93.             if (in_array($key$basicChanges))
  94.             {
  95.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  96.                 continue;
  97.             }
  98.             if (in_array($key$basicBoolChanges))
  99.             {
  100.                 $pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args$before$after);
  101.                 continue;
  102.             }
  103.             if (in_array($key$dateChanges))
  104.             {
  105.                 // dateChanges can be null
  106.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  107.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  108.                 continue;
  109.             }
  110.             if (in_array($key$labelChanges))
  111.             {
  112.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  113.                 continue;
  114.             }
  115.             if (in_array($key$objectChanges))
  116.             {
  117.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  118.                 continue;
  119.             }
  120.         }
  121.         return $pendingLogArgs;
  122.     }
  123.     public function logRemoval(Vehicle $vehicle)
  124.     {
  125.         $pendingLogArgs = [];
  126.         //----------------------------------------------------------------------
  127.         // Fetch eventual info skeletond in the object
  128.         $loggingData $this->logTools->handleLoggingData($vehicle);
  129.         $info $loggingData['info'];
  130.         $specialAuthor $loggingData['special_author'];
  131.         $ignore $loggingData['ignore'];
  132.         if ($ignore) return $pendingLogArgs;
  133.         //----------------------------------------------------------------------
  134.         // Init base log args
  135.         // This does not contain action
  136.         $args $this->initArgs($vehicle$loggingData);
  137.         //----------------------------------------------------------------------
  138.         $args["action"] = "vehicle_delete";
  139.         $pendingLogArgs[] = $args;
  140.         return $pendingLogArgs;
  141.     }
  142.     private function initArgs(Vehicle $vehicle$loggingData)
  143.     {
  144.         $society $vehicle->getSociety();
  145.         $societyGroup $vehicle->getSocietyGroup();
  146.         // Nope
  147.         // Add HumanResource logging
  148.         // $humanResource = $vehicle->getHumanResource();
  149.         $args = array(
  150.             "object_id"                    =>    $vehicle->getId(),
  151.             "object_bundle"                =>    "Equipment",
  152.             "object_entity"                =>    "Vehicle",
  153.             "object_display"            =>    $vehicle->display(),
  154.             // "object_human_resource_id"        =>    $humanResource !== null ? $humanResource->getId() : null,
  155.             // "object_human_resource_display"    =>    $humanResource !== null ? $humanResource->display() : null,
  156.             "society_group"                =>    $societyGroup,
  157.             "society"                    =>    $society,
  158.             "info"                        =>    $loggingData['info'],
  159.             "special_author"            =>    $loggingData['special_author'],
  160.         );
  161.         return $args;
  162.     }
  163. }