src/Logging/HR/OPVarLog.php line 24

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/OPVarLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\HR;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\HR\HumanResource;
  8. use App\Entity\HR\Salary\OPVariable;
  9. use App\Logging\DeletionContextLogger;
  10. use App\Logging\Tools;
  11. use App\Services\LogTools;
  12. class OPVarLog
  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(OPVariable $opVar)
  23.     {
  24.         $pendingLogArgs = [];
  25.         $humanResource $opVar->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($opVar);
  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($opVar$humanResource$loggingData);
  42.         //----------------------------------------------------------------------
  43.         $action "human_resource_add_op_var";
  44.         $args["action"] = $action;
  45.         $args["new_value"] = $opVar->display();
  46.         $pendingLogArgs[] = $args;
  47.         return $pendingLogArgs;
  48.     }
  49.     public function logChanges(OPVariable $opVar$changes)
  50.     {
  51.         $pendingLogArgs = [];
  52.         $humanResource $opVar->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($opVar);
  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($opVar$humanResource$loggingData);
  69.         //----------------------------------------------------------------------
  70.         $action "human_resource_edit_op_var";
  71.         $basicChanges = array(
  72.             "value",
  73.             "info",
  74.         );
  75.         $basicBoolChanges = array(
  76.         );
  77.         $dateChanges = array(
  78.             "startDate",
  79.         );
  80.         $labelChanges = array(
  81.             "status",
  82.             "type",
  83.         );
  84.         // Method getLogLabel() should be defined for these objects/entities
  85.         $objectChanges = array(
  86.         );
  87.         // See what changed and log (members first)
  88.         foreach ($changes as $key => $change)
  89.         {
  90.             // Something to skip ?
  91.             if (false)
  92.             {
  93.                 continue;
  94.             }
  95.             $name $this->logTools->camelToSnakeCase($key);
  96.             $args["action"]    = $action."_".$name;
  97.             $before $change[0];
  98.             $after $change[1];
  99.             if (in_array($key$basicChanges))
  100.             {
  101.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  102.                 continue;
  103.             }
  104.             if (in_array($key$basicBoolChanges))
  105.             {
  106.                 $pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args$before$after);
  107.                 continue;
  108.             }
  109.             if (in_array($key$dateChanges))
  110.             {
  111.                 // dateChanges can be null
  112.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  113.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  114.                 continue;
  115.             }
  116.             if (in_array($key$labelChanges))
  117.             {
  118.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  119.                 continue;
  120.             }
  121.             if (in_array($key$objectChanges))
  122.             {
  123.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  124.                 continue;
  125.             }
  126.         }
  127.         return $pendingLogArgs;
  128.     }
  129.     public function logRemoval(OPVariable $opVar)
  130.     {
  131.         $pendingLogArgs = [];
  132.         $humanResource null;
  133.         $cachedData $this->contextLogger->getContext($opVar);
  134.         if ($cachedData !== null && !empty($cachedData))
  135.         {
  136.             if (array_key_exists('humanResource'$cachedData) && $cachedData['humanResource'] !== null)
  137.             {
  138.                 $humanResource $cachedData['humanResource'];
  139.             }
  140.         }
  141.         if ($humanResource === null)
  142.         {
  143.             // Nothing to do here
  144.             return $pendingLogArgs;
  145.         }
  146.         //----------------------------------------------------------------------
  147.         // Fetch eventual info skeletond in the object
  148.         $loggingData $this->logTools->handleLoggingData($opVar);
  149.         $info $loggingData['info'];
  150.         $specialAuthor $loggingData['special_author'];
  151.         $ignore $loggingData['ignore'];
  152.         if ($ignore) return $pendingLogArgs;
  153.         //----------------------------------------------------------------------
  154.         // Init base log args
  155.         // This does not contain action
  156.         $args $this->initArgs($opVar$humanResource$loggingData);
  157.         //----------------------------------------------------------------------
  158.         $args["action"] = "human_resource_delete_op_var";
  159.         $args["old_value"] = $opVar->display();
  160.         $pendingLogArgs[] = $args;
  161.         return $pendingLogArgs;
  162.     }
  163.     private function initArgs(OPVariable $opVarHumanResource $humanResource$loggingData)
  164.     {
  165.         $society $humanResource->getSociety();
  166.         $societyGroup $humanResource->getSocietyGroup();
  167.         $args = array(
  168.             "object_id"                    =>    $humanResource->getId(),
  169.             "object_bundle"                =>    "HR",
  170.             "object_entity"                =>    "HumanResource",
  171.             "object_display"            =>    $humanResource->display(),
  172.             "object_human_resource_id"        =>    $humanResource !== null $humanResource->getId() : null,
  173.             "object_human_resource_display"    =>    $humanResource !== null $humanResource->display() : null,
  174.             "society_group"                =>    $societyGroup,
  175.             "society"                    =>    $society,
  176.             "info"                        =>    $loggingData['info'],
  177.             "special_author"            =>    $loggingData['special_author'],
  178.         );
  179.         return $args;
  180.     }
  181. }