src/Logging/HR/NoteLog.php line 24

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/NoteLog.php
  4. // Apparently these are ApplicationNotes ...
  5. // so logging looks like this : note_application_*
  6. //----------------------------------------------------------------------
  7. namespace App\Logging\HR;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use App\Entity\HR\Note;
  10. use App\Logging\DeletionContextLogger;
  11. use App\Logging\Tools;
  12. use App\Services\LogTools;
  13. class NoteLog
  14. {
  15.     private array $pendingLogArgs = [];
  16.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsTools $toolsDeletionContextLogger $contextLogger)
  17.     {
  18.         $this->em $doctrine->getManager();
  19.         $this->logTools $logTools;
  20.         $this->tools $tools;
  21.         $this->contextLogger $contextLogger;
  22.     }
  23.     public function logCreation(Note $note)
  24.     {
  25.         $pendingLogArgs = [];
  26.         //----------------------------------------------------------------------
  27.         // Fetch eventual info stored in the object
  28.         $loggingData $this->logTools->handleLoggingData($note);
  29.         $info $loggingData['info'];
  30.         $specialAuthor $loggingData['special_author'];
  31.         $ignore $loggingData['ignore'];
  32.         if ($ignore) return $pendingLogArgs;
  33.         //----------------------------------------------------------------------
  34.         // Init base log args
  35.         // This does not contain action
  36.         $args $this->initArgs($note$loggingData);
  37.         //----------------------------------------------------------------------
  38.         $action "note_add";
  39.         $objectEntity "Note";
  40.         if ($note->getApplication() !== null)
  41.         {
  42.             $action 'note_application_add';
  43.             $objectEntity 'ApplicationNote';
  44.         }
  45.         $args["action"] = $action;
  46.         $args["object_entity"] = $objectEntity;
  47.         $pendingLogArgs[] = $args;
  48.         return $pendingLogArgs;
  49.     }
  50.     public function logChanges(Note $note$changes)
  51.     {
  52.         $pendingLogArgs = [];
  53.         //----------------------------------------------------------------------
  54.         // Fetch eventual info skeletond in the object
  55.         $loggingData $this->logTools->handleLoggingData($note);
  56.         $info $loggingData['info'];
  57.         $specialAuthor $loggingData['special_author'];
  58.         $ignore $loggingData['ignore'];
  59.         if ($ignore) return $pendingLogArgs;
  60.         //----------------------------------------------------------------------
  61.         // Init base log args
  62.         // This does not contain action
  63.         $args $this->initArgs($note$loggingData);
  64.         //----------------------------------------------------------------------
  65.         $action "note_edit";
  66.         $objectEntity "Note";
  67.         if ($note->getApplication() !== null)
  68.         {
  69.             $action 'note_application_edit';
  70.             $objectEntity 'ApplicationNote';
  71.         }
  72.         $args["object_entity"] = $objectEntity;
  73.         $basicChanges = array(
  74.             "body",
  75.         );
  76.         $basicBoolChanges = array(
  77.             "hidden",
  78.         );
  79.         // See what changed and log (members first)
  80.         foreach ($changes as $key => $change)
  81.         {
  82.             // Something to skip ?
  83.             if (false)
  84.             {
  85.                 continue;
  86.             }
  87.             $name $this->logTools->camelToSnakeCase($key);
  88.             $args["action"]    = $action."_".$name;
  89.             $before $change[0];
  90.             $after $change[1];
  91.             if (in_array($key$basicChanges))
  92.             {
  93.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  94.                 continue;
  95.             }
  96.             if (in_array($key$basicBoolChanges))
  97.             {
  98.                 $pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args$before$after);
  99.                 continue;
  100.             }
  101.         }
  102.         return $pendingLogArgs;
  103.     }
  104.     public function logRemoval(Note $note)
  105.     {
  106.         $pendingLogArgs = [];
  107.         $cachedData $this->contextLogger->getContext($note);
  108.         if ($cachedData === null || empty($cachedData))
  109.         {
  110.             // Nothing to do here
  111.             return $pendingLogArgs;
  112.         }
  113.         //----------------------------------------------------------------------
  114.         // Fetch eventual info skeletond in the object
  115.         $loggingData $this->logTools->handleLoggingData($note);
  116.         $info $loggingData['info'];
  117.         $specialAuthor $loggingData['special_author'];
  118.         $ignore $loggingData['ignore'];
  119.         if ($ignore) return $pendingLogArgs;
  120.         //----------------------------------------------------------------------
  121.         // Init base log args
  122.         // This does not contain action
  123.         $args $this->initArgs($note$loggingData);
  124.         //----------------------------------------------------------------------
  125.         $action "note_delete";
  126.         $objectEntity "Note";
  127.         if (array_key_exists('application'$cachedData) && $cachedData['application'] !== null)
  128.         {
  129.             $action 'note_application_delete';
  130.             $objectEntity 'ApplicationNote';
  131.         }
  132.         $args["action"] = $action;
  133.         $args["object_entity"] = $objectEntity;
  134.         $pendingLogArgs[] = $args;
  135.         return $pendingLogArgs;
  136.     }
  137.     private function initArgs(Note $note$loggingData)
  138.     {
  139.         $society $note->getSociety();
  140.         $societyGroup $note->getSocietyGroup();
  141.         $args = array(
  142.             "object_id"                    =>    $note->getId(),
  143.             "object_bundle"                =>    "HR",
  144.             "object_entity"                =>    "Note",
  145.             "object_display"            =>    $note->display(),
  146.             "society_group"                =>    $societyGroup,
  147.             "society"                    =>    $society,
  148.             "info"                        =>    $loggingData['info'],
  149.             "special_author"            =>    $loggingData['special_author'],
  150.         );
  151.         return $args;
  152.     }
  153. }