src/Logging/HR/HumanResourceLog.php line 22

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/HumanResourceLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\HR;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\HR\HumanResource;
  8. use App\Entity\HR\Log as HRLog;
  9. use App\Logging\Tools;
  10. use App\Services\LogTools;
  11. class HumanResourceLog
  12. {
  13.     private array $pendingLogArgs = [];
  14.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsTools $tools)
  15.     {
  16.         $this->em $doctrine->getManager();
  17.         $this->logTools $logTools;
  18.         $this->tools $tools;
  19.     }
  20.     public function logCreation(HumanResource $humanResource)
  21.     {
  22.         $pendingLogArgs = [];
  23.         //----------------------------------------------------------------------
  24.         // Fetch eventual info stored in the object
  25.         $loggingData $this->logTools->handleLoggingData($humanResource);
  26.         $info $loggingData['info'];
  27.         $specialAuthor $loggingData['special_author'];
  28.         $ignore $loggingData['ignore'];
  29.         if ($ignore) return $pendingLogArgs;
  30.         //----------------------------------------------------------------------
  31.         // Init base log args
  32.         // This does not contain action
  33.         $args $this->initArgs($humanResource$loggingData);
  34.         //----------------------------------------------------------------------
  35.         $action "human_resource_add";
  36.         $args["action"] = $action;
  37.         $pendingLogArgs[] = $args;
  38.         return $pendingLogArgs;
  39.     }
  40.     public function logChanges(HumanResource $humanResource$changes)
  41.     {
  42.         $pendingLogArgs = [];
  43.         //----------------------------------------------------------------------
  44.         // Fetch eventual info skeletond in the object
  45.         $loggingData $this->logTools->handleLoggingData($humanResource);
  46.         $info $loggingData['info'];
  47.         $specialAuthor $loggingData['special_author'];
  48.         $ignore $loggingData['ignore'];
  49.         if ($ignore) return $pendingLogArgs;
  50.         //----------------------------------------------------------------------
  51.         // Init base log args
  52.         // This does not contain action
  53.         $args $this->initArgs($humanResource$loggingData);
  54.         //----------------------------------------------------------------------
  55.         $action "human_resource_edit";
  56.         // This is triggered when we are actually changing the connected Address object
  57.         // The changes in the actual object are handled in AddressLog
  58.         $addressChanges = array(
  59.             "address",
  60.         );
  61.         $basicChanges = array(
  62.             "lastname",
  63.             "firstname",
  64.             "birthplace",
  65.             "account",
  66.             "email",
  67.             "emailPerso",
  68.             "info",
  69.             "phone",
  70.             "phonePerso",
  71.             "company",
  72.         );
  73.         $basicBoolChanges = array(
  74.         );
  75.         $dateChanges = array(
  76.             "birthday",
  77.         );
  78.         $labelChanges = array(
  79.             "status",
  80.             "type",
  81.         );
  82.         // Method getLogLabel() should be defined for these objects/entities
  83.         $objectChanges = array(
  84.             "profileImage",
  85.             "society",
  86.             "supplier",
  87.         );
  88.         // See what changed and log (members first)
  89.         foreach ($changes as $key => $change)
  90.         {
  91.             // Something to skip ?
  92.             if (false)
  93.             {
  94.                 continue;
  95.             }
  96.             $name $this->logTools->camelToSnakeCase($key);
  97.             $args["action"]    = $action."_".$name;
  98.             $before $change[0];
  99.             $after $change[1];
  100.             if (in_array($key$addressChanges))
  101.             {
  102.                 $pendingLogArgs[] = $this->tools->handleAddressChanges($args$before$after);
  103.                 continue;
  104.             }
  105.             if (in_array($key$basicChanges))
  106.             {
  107.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  108.                 continue;
  109.             }
  110.             if (in_array($key$basicBoolChanges))
  111.             {
  112.                 $pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args$before$after);
  113.                 continue;
  114.             }
  115.             if (in_array($key$dateChanges))
  116.             {
  117.                 // dateChanges can be null
  118.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  119.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  120.                 continue;
  121.             }
  122.             if (in_array($key$labelChanges))
  123.             {
  124.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  125.                 continue;
  126.             }
  127.             if (in_array($key$objectChanges))
  128.             {
  129.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  130.                 continue;
  131.             }
  132.         }
  133.         return $pendingLogArgs;
  134.     }
  135.     public function logRemoval(HumanResource $humanResource)
  136.     {
  137.         $pendingLogArgs = [];
  138.         return $pendingLogArgs;
  139.     }
  140.     private function initArgs(HumanResource $humanResource$loggingData)
  141.     {
  142.         $society $humanResource->getSociety();
  143.         $societyGroup $humanResource->getSocietyGroup();
  144.         $args = array(
  145.             "object_id"                    =>    $humanResource->getId(),
  146.             "object_bundle"                =>    "HR",
  147.             "object_entity"                =>    "HumanResource",
  148.             "object_display"            =>    $humanResource->display(),
  149.             "object_human_resource_id"        =>    $humanResource->getId(),
  150.             "object_human_resource_display"    =>    $humanResource->display(),
  151.             "society_group"                =>    $societyGroup,
  152.             "society"                    =>    $society,
  153.             "info"                        =>    $loggingData['info'],
  154.             "special_author"            =>    $loggingData['special_author'],
  155.         );
  156.         return $args;
  157.     }
  158.     // When HumanResources are created the corresponding ID is not available
  159.     // So set it in the postFlush event
  160.     public function handleHumanResource($log$object)
  161.     {
  162.         if ($object instanceof HumanResource and $log instanceof HRLog)
  163.         {
  164.             if (empty($log->getObjectHumanResourceId()))
  165.             {
  166.                 $log->setObjectHumanResourceId($object->getId());
  167.                 return true;
  168.             }
  169.         }
  170.         return false;
  171.     }
  172. }