src/Logging/HR/HRVarLog.php line 19

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/HRVarLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\HR;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\HR\HumanResource;
  8. use App\Entity\HR\Salary\HRVariable;
  9. use App\Logging\DeletionContextLogger;
  10. use App\Logging\Tools;
  11. use App\Services\LogTools;
  12. class HRVarLog
  13. {
  14.     private array $pendingLogArgs = [];
  15.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsTools $toolsDeletionContextLogger $contextLogger)
  16.     {
  17.         $this->em $doctrine->getManager();
  18.         $this->logTools $logTools;
  19.         $this->tools $tools;
  20.         $this->contextLogger $contextLogger;
  21.     }
  22.     public function logCreation(HRVariable $hrVar)
  23.     {
  24.         $pendingLogArgs = [];
  25.         $humanResource $hrVar->getHumanResource();
  26.         if ($humanResource === null)
  27.         {
  28.             // Nothing to do here
  29.             return $pendingLogArgs;
  30.         }
  31.         //----------------------------------------------------------------------
  32.         // Fetch eventual info stored in the object
  33.         $loggingData $this->logTools->handleLoggingData($hrVar);
  34.         $info $loggingData['info'];
  35.         $specialAuthor $loggingData['special_author'];
  36.         $ignore $loggingData['ignore'];
  37.         if ($ignore) return $pendingLogArgs;
  38.         //----------------------------------------------------------------------
  39.         // Init base log args
  40.         // This does not contain action
  41.         $args $this->initArgs($hrVar$humanResource$loggingData);
  42.         //----------------------------------------------------------------------
  43.         $action "human_resource_add_hr_var";
  44.         $args["action"] = $action;
  45.         $args["new_value"] = $hrVar->display();
  46.         $pendingLogArgs[] = $args;
  47.         return $pendingLogArgs;
  48.     }
  49.     public function logChanges(HRVariable $hrVar$changes)
  50.     {
  51.         $pendingLogArgs = [];
  52.         $humanResource $hrVar->getHumanResource();
  53.         if ($humanResource === null)
  54.         {
  55.             // Nothing to do here
  56.             return $pendingLogArgs;
  57.         }
  58.         //----------------------------------------------------------------------
  59.         // Fetch eventual info skeletond in the object
  60.         $loggingData $this->logTools->handleLoggingData($hrVar);
  61.         $info $loggingData['info'];
  62.         $specialAuthor $loggingData['special_author'];
  63.         $ignore $loggingData['ignore'];
  64.         if ($ignore) return $pendingLogArgs;
  65.         //----------------------------------------------------------------------
  66.         // Init base log args
  67.         // This does not contain action
  68.         $args $this->initArgs($hrVar$humanResource$loggingData);
  69.         //----------------------------------------------------------------------
  70.         $action "human_resource_edit_hr_var";
  71.         $basicChanges = array(
  72.             "value",
  73.             "info",
  74.         );
  75.         $basicBoolChanges = array(
  76.         );
  77.         $dateChanges = array(
  78.             "startDate",
  79.             "endDate",
  80.         );
  81.         $labelChanges = array(
  82.             "status",
  83.             "type",
  84.             "frequency",
  85.         );
  86.         // Method getLogLabel() should be defined for these objects/entities
  87.         $objectChanges = array(
  88.         );
  89.         // See what changed and log (members first)
  90.         foreach ($changes as $key => $change)
  91.         {
  92.             // Something to skip ?
  93.             if (false)
  94.             {
  95.                 continue;
  96.             }
  97.             $name $this->logTools->camelToSnakeCase($key);
  98.             $args["action"]    = $action."_".$name;
  99.             $before $change[0];
  100.             $after $change[1];
  101.             if (in_array($key$basicChanges))
  102.             {
  103.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  104.                 continue;
  105.             }
  106.             if (in_array($key$basicBoolChanges))
  107.             {
  108.                 $pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args$before$after);
  109.                 continue;
  110.             }
  111.             if (in_array($key$dateChanges))
  112.             {
  113.                 // dateChanges can be null
  114.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  115.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  116.                 continue;
  117.             }
  118.             if (in_array($key$labelChanges))
  119.             {
  120.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  121.                 continue;
  122.             }
  123.             if (in_array($key$objectChanges))
  124.             {
  125.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  126.                 continue;
  127.             }
  128.         }
  129.         return $pendingLogArgs;
  130.     }
  131.     public function logRemoval(HRVariable $hrVar)
  132.     {
  133.         $pendingLogArgs = [];
  134.         $humanResource null;
  135.         $cachedData $this->contextLogger->getContext($hrVar);
  136.         if ($cachedData !== null && !empty($cachedData))
  137.         {
  138.             if (array_key_exists('humanResource'$cachedData) && $cachedData['humanResource'] !== null)
  139.             {
  140.                 $humanResource $cachedData['humanResource'];
  141.             }
  142.         }
  143.         if ($humanResource === null)
  144.         {
  145.             // Nothing to do here
  146.             return $pendingLogArgs;
  147.         }
  148.         //----------------------------------------------------------------------
  149.         // Fetch eventual info skeletond in the object
  150.         $loggingData $this->logTools->handleLoggingData($hrVar);
  151.         $info $loggingData['info'];
  152.         $specialAuthor $loggingData['special_author'];
  153.         $ignore $loggingData['ignore'];
  154.         if ($ignore) return $pendingLogArgs;
  155.         //----------------------------------------------------------------------
  156.         // Init base log args
  157.         // This does not contain action
  158.         $args $this->initArgs($hrVar$humanResource$loggingData);
  159.         //----------------------------------------------------------------------
  160.         $args["action"] = "human_resource_delete_hr_var";
  161.         $args["old_value"] = $hrVar->display();
  162.         $pendingLogArgs[] = $args;
  163.         return $pendingLogArgs;
  164.     }
  165.     private function initArgs(HRVariable $hrVarHumanResource $humanResource$loggingData)
  166.     {
  167.         $society $humanResource->getSociety();
  168.         $societyGroup $humanResource->getSocietyGroup();
  169.         $args = array(
  170.             "object_id"                    =>    $humanResource->getId(),
  171.             "object_bundle"                =>    "HR",
  172.             "object_entity"                =>    "HumanResource",
  173.             "object_display"            =>    $humanResource->display(),
  174.             "object_human_resource_id"        =>    $humanResource !== null $humanResource->getId() : null,
  175.             "object_human_resource_display"    =>    $humanResource !== null $humanResource->display() : null,
  176.             
  177.             "society_group"                =>    $societyGroup,
  178.             "society"                    =>    $society,
  179.             "info"                        =>    $loggingData['info'],
  180.             "special_author"            =>    $loggingData['special_author'],
  181.         );
  182.         return $args;
  183.     }
  184. }