src/Logging/Media/CommentLog.php line 19

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