src/Logging/HR/ApplicationLog.php line 21

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/ApplicationLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\HR;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\HR\Application\Application;
  8. use App\Logging\Tools;
  9. use App\Services\LogTools;
  10. class ApplicationLog
  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(Application $application)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData $this->logTools->handleLoggingData($application);
  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($application$loggingData);
  33.         //----------------------------------------------------------------------
  34.         $action "application_add";
  35.         $args["action"] = $action;
  36.         $pendingLogArgs[] = $args;
  37.         return $pendingLogArgs;
  38.     }
  39.     public function logChanges(Application $application$changes)
  40.     {
  41.         $pendingLogArgs = [];
  42.         //----------------------------------------------------------------------
  43.         // Fetch eventual info skeletond in the object
  44.         $loggingData $this->logTools->handleLoggingData($application);
  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($application$loggingData);
  53.         //----------------------------------------------------------------------
  54.         $action "application_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.             "address",
  59.         );
  60.         $basicChanges = array(
  61.             "ref",
  62.             "firstname",
  63.             "lastname",
  64.             "email",
  65.             "phone",
  66.             "sponsor",
  67.             "company",
  68.             "companyCode",
  69.             "legalForm",
  70.             "qualification",
  71.             "nbStaff",
  72.             "townsDisplay",
  73.             "departmentsDisplay",
  74.             "info",
  75.         );
  76.         $basicBoolChanges = array(
  77.         );
  78.         $dateChanges = array(
  79.         );
  80.         $labelChanges = array(
  81.             "status",
  82.             "category",
  83.             "experience",
  84.             "mobility",
  85.             "origin",
  86.             "position",
  87.             'level',
  88.         );
  89.         // Method getLogLabel() should be defined for these objects/entities
  90.         $objectChanges = array(
  91.             "society",
  92.         );
  93.         // See what changed and log (members first)
  94.         foreach ($changes as $key => $change)
  95.         {
  96.             // Something to skip ?
  97.             if (false)
  98.             {
  99.                 continue;
  100.             }
  101.             $name $this->logTools->camelToSnakeCase($key);
  102.             $args["action"]    = $action."_".$name;
  103.             $before $change[0];
  104.             $after $change[1];
  105.             if (in_array($key$addressChanges))
  106.             {
  107.                 $pendingLogArgs[] = $this->tools->handleAddressChanges($args$before$after);
  108.                 continue;
  109.             }
  110.             if (in_array($key$basicChanges))
  111.             {
  112.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  113.                 continue;
  114.             }
  115.             if (in_array($key$basicBoolChanges))
  116.             {
  117.                 $pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args$before$after);
  118.                 continue;
  119.             }
  120.             if (in_array($key$dateChanges))
  121.             {
  122.                 // dateChanges can be null
  123.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  124.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  125.                 continue;
  126.             }
  127.             if (in_array($key$labelChanges))
  128.             {
  129.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  130.                 continue;
  131.             }
  132.             if (in_array($key$objectChanges))
  133.             {
  134.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  135.                 continue;
  136.             }
  137.         }
  138.         // Handle ManyToMany
  139.         if ($application->getActivities()->isDirty())
  140.         {
  141.             $oldData "";
  142.             $newData "";
  143.             foreach ($application->getActivities()->getSnapshot() as $activity)
  144.             {
  145.                 $oldData .= $activity->display();
  146.                 $oldData .= ", ";
  147.             }
  148.             foreach ($application->getActivities() as $activity)
  149.             {
  150.                 $newData .= $activity->display();
  151.                 $newData .= ", ";
  152.             }
  153.             if ($oldData != "")
  154.             {
  155.                 $oldData substr($oldData0, -1);
  156.                 $oldData substr($oldData0, -1);
  157.             }
  158.             if ($newData != "")
  159.             {
  160.                 $newData substr($newData0, -1);
  161.                 $newData substr($newData0, -1);
  162.             }
  163.             $args["action"] = "application_edit_activities";
  164.             $args["old_value"] = $oldData;
  165.             $args["new_value"] = $newData;
  166.             $pendingLogArgs[] = $args;
  167.         }
  168.         return $pendingLogArgs;
  169.     }
  170.     public function logRemoval(Application $application)
  171.     {
  172.         $pendingLogArgs = [];
  173.         //----------------------------------------------------------------------
  174.         // Fetch eventual info skeletond in the object
  175.         $loggingData $this->logTools->handleLoggingData($application);
  176.         $info $loggingData['info'];
  177.         $specialAuthor $loggingData['special_author'];
  178.         $ignore $loggingData['ignore'];
  179.         if ($ignore) return $pendingLogArgs;
  180.         //----------------------------------------------------------------------
  181.         // Init base log args
  182.         // This does not contain action
  183.         $args $this->initArgs($application$loggingData);
  184.         //----------------------------------------------------------------------
  185.         $args["action"] = "application_delete";
  186.         $pendingLogArgs[] = $args;
  187.         return $pendingLogArgs;
  188.     }
  189.     private function initArgs(Application $application$loggingData)
  190.     {
  191.         $society $application->getSociety();
  192.         $societyGroup $application->getSocietyGroup();
  193.         // Add HumanResource logging
  194.         $humanResource $application->getHumanResource();
  195.         $args = array(
  196.             "object_id"                    =>    $application->getId(),
  197.             "object_bundle"                =>    "HR",
  198.             "object_entity"                =>    "Application",
  199.             "object_display"            =>    $application->display(),
  200.             "object_human_resource_id"        =>    $humanResource !== null $humanResource->getId() : null,
  201.             "object_human_resource_display"    =>    $humanResource !== null $humanResource->display() : null,
  202.             
  203.             "society_group"                =>    $societyGroup,
  204.             "society"                    =>    $society,
  205.             "info"                        =>    $loggingData['info'],
  206.             "special_author"            =>    $loggingData['special_author'],
  207.         );
  208.         return $args;
  209.     }
  210. }