src/Logging/Media/ArticleLog.php line 17

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/Media/ArticleLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Media;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Media\Article;
  8. use App\Logging\Tools;
  9. use App\Services\LogTools;
  10. class ArticleLog
  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(Article $article)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData $this->logTools->handleLoggingData($article);
  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($article$loggingData);
  33.         //----------------------------------------------------------------------
  34.         $action "article_add";
  35.         $args["action"] = $action;
  36.         $pendingLogArgs[] = $args;
  37.         return $pendingLogArgs;
  38.     }
  39.     public function logChanges(Article $article$changes)
  40.     {
  41.         $pendingLogArgs = [];
  42.         //----------------------------------------------------------------------
  43.         // Fetch eventual info skeletond in the object
  44.         $loggingData $this->logTools->handleLoggingData($article);
  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($article$loggingData);
  53.         //----------------------------------------------------------------------
  54.         $action "article_edit";
  55.         $basicChanges = array(
  56.             "title",
  57.             "body",
  58.         );
  59.         $basicBoolChanges = array(
  60.             "published",
  61.         );
  62.         $dateChanges = array(
  63.         );
  64.         $labelChanges = array(
  65.         );
  66.         // Method getLogLabel() should be defined for these objects/entities
  67.         $objectChanges = array(
  68.         );
  69.         // See what changed and log (members first)
  70.         foreach ($changes as $key => $change)
  71.         {
  72.             // Something to skip ?
  73.             if (false)
  74.             {
  75.                 continue;
  76.             }
  77.             $name $this->logTools->camelToSnakeCase($key);
  78.             $args["action"]    = $action."_".$name;
  79.             $before $change[0];
  80.             $after $change[1];
  81.             if (in_array($key$basicChanges))
  82.             {
  83.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  84.                 continue;
  85.             }
  86.             if (in_array($key$basicBoolChanges))
  87.             {
  88.                 $pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args$before$after);
  89.                 continue;
  90.             }
  91.             if (in_array($key$dateChanges))
  92.             {
  93.                 // Display d/m/Y H:i => Add a fourth param true to handleDateChanges
  94.                 // dateChanges can be null
  95.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  96.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  97.                 continue;
  98.             }
  99.             if (in_array($key$labelChanges))
  100.             {
  101.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  102.                 continue;
  103.             }
  104.             if (in_array($key$objectChanges))
  105.             {
  106.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  107.                 continue;
  108.             }
  109.         }
  110.         return $pendingLogArgs;
  111.     }
  112.     public function logRemoval(Article $article)
  113.     {
  114.         $pendingLogArgs = [];
  115.         //----------------------------------------------------------------------
  116.         // Fetch eventual info skeletond in the object
  117.         $loggingData $this->logTools->handleLoggingData($article);
  118.         $info $loggingData['info'];
  119.         $specialAuthor $loggingData['special_author'];
  120.         $ignore $loggingData['ignore'];
  121.         if ($ignore) return $pendingLogArgs;
  122.         //----------------------------------------------------------------------
  123.         // Init base log args
  124.         // This does not contain action
  125.         $args $this->initArgs($article$loggingData);
  126.         //----------------------------------------------------------------------
  127.         $args["action"] = "article_delete";
  128.         $pendingLogArgs[] = $args;
  129.         return $pendingLogArgs;
  130.     }
  131.     private function initArgs(Article $article$loggingData)
  132.     {
  133.         $societyGroup $article->getSocietyGroup();
  134.         $args = array(
  135.             "object_id"                    =>    $article->getId(),
  136.             "object_bundle"                =>    "Media",
  137.             "object_entity"                =>    "Article",
  138.             "object_display"            =>    $article->display(),
  139.             "society_group"                =>    $societyGroup,
  140.             "info"                        =>    $loggingData['info'],
  141.             "special_author"            =>    $loggingData['special_author'],
  142.         );
  143.         return $args;
  144.     }
  145. }