src/Logging/Activity/NoteLog.php line 21

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/NoteLog.php
  4. // This is a special type of log since it handles
  5. // both SHARING (double logging)
  6. // and DELETION (using context logger)
  7. //----------------------------------------------------------------------
  8. namespace App\Logging\Activity;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use App\Entity\Platform\Note;
  11. use App\Logging\DeletionContextLogger;
  12. use App\Logging\Tools;
  13. use App\Services\LogTools;
  14. class NoteLog
  15. {
  16.     private array $pendingLogArgs = [];
  17.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsTools $toolsDeletionContextLogger $contextLogger)
  18.     {
  19.         $this->em $doctrine->getManager();
  20.         $this->logTools $logTools;
  21.         $this->tools $tools;
  22.         $this->contextLogger $contextLogger;
  23.     }
  24.     public function logCreation(Note $note)
  25.     {
  26.         // Get Note data considering sharing
  27.         $noteData $this->getDataConsideringSharing($note);
  28.         if ($noteData === null) return [];
  29.         $pendingLogArgs = [];
  30.         //----------------------------------------------------------------------
  31.         // Fetch eventual info stored in the object
  32.         $loggingData $this->logTools->handleLoggingData($note);
  33.         $info $loggingData['info'];
  34.         $specialAuthor $loggingData['special_author'];
  35.         $ignore $loggingData['ignore'];
  36.         if ($ignore) return $pendingLogArgs;
  37.         //----------------------------------------------------------------------
  38.         // Init base log args
  39.         // This does not contain action
  40.         $args $this->initArgs($note$loggingData$noteData);
  41.         //----------------------------------------------------------------------
  42.         $action "note_add";
  43.         $args["action"] = $action;
  44.         $pendingLogArgs[] = $args;
  45.         if ($noteData['double'])
  46.         {
  47.             $argsBis $this->initArgs($note$loggingData$noteData);
  48.             $argsBis["society_group"] = $noteData['societyGroupAuthor'];
  49.             $argsBis["society"] = $noteData['societyAuthor'];
  50.             $argsBis["action"] = $action;
  51.             $pendingLogArgs[] = $argsBis;
  52.         }
  53.         return $pendingLogArgs;
  54.     }
  55.     public function logChanges(Note $note$changes)
  56.     {
  57.         // Get Note data considering sharing
  58.         $noteData $this->getDataConsideringSharing($note);
  59.         if ($noteData === null) return [];
  60.         $pendingLogArgs = [];
  61.         //----------------------------------------------------------------------
  62.         // Fetch eventual info noted in the object
  63.         $loggingData $this->logTools->handleLoggingData($note);
  64.         $info $loggingData['info'];
  65.         $specialAuthor $loggingData['special_author'];
  66.         $ignore $loggingData['ignore'];
  67.         if ($ignore) return $pendingLogArgs;
  68.         //----------------------------------------------------------------------
  69.         // Init base log args
  70.         // This does not contain action
  71.         $args $this->initArgs($note$loggingData$noteData);
  72.         //----------------------------------------------------------------------
  73.         $action "note_edit";
  74.         $double $noteData['double'];
  75.         if ($double)
  76.         {
  77.             $argsBis $this->initArgs($note$loggingData$noteData);
  78.             $argsBis["society_group"] = $noteData['societyGroupAuthor'];
  79.             $argsBis["society"] = $noteData['societyAuthor'];
  80.         }
  81.         $basicChanges = array(
  82.             "body",
  83.             "hidden",
  84.         );
  85.         // See what changed and log (members first)
  86.         foreach ($changes as $key => $change)
  87.         {
  88.             // Something to skip ?
  89.             if (false)
  90.             {
  91.                 continue;
  92.             }
  93.             $name $this->logTools->camelToSnakeCase($key);
  94.             $args["action"]    = $action."_".$name;
  95.             if ($double)
  96.             {
  97.                 $argsBis["action"] = $args["action"];
  98.             }
  99.             $before $change[0];
  100.             $after $change[1];
  101.             if (in_array($key$basicChanges))
  102.             {
  103.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  104.                 if ($double)
  105.                 {
  106.                     $pendingLogArgs[] = $this->tools->handleBasicChanges($argsBis$before$after);
  107.                 }
  108.                 continue;
  109.             }
  110.         }
  111.         return $pendingLogArgs;
  112.     }
  113.     public function logRemoval(Note $note)
  114.     {
  115.         // Get Note data considering sharing and context
  116.         $noteData $this->getDataConsideringContext($note);
  117.         if ($noteData === null) return [];
  118.         $pendingLogArgs = [];
  119.         //----------------------------------------------------------------------
  120.         // Fetch eventual info noted in the object
  121.         $loggingData $this->logTools->handleLoggingData($note);
  122.         $info $loggingData['info'];
  123.         $specialAuthor $loggingData['special_author'];
  124.         $ignore $loggingData['ignore'];
  125.         if ($ignore) return $pendingLogArgs;
  126.         //----------------------------------------------------------------------
  127.         // Init base log args
  128.         // This does not contain action
  129.         $args $this->initArgs($note$loggingData$noteData);
  130.         //----------------------------------------------------------------------
  131.         $action "note_delete";
  132.         $args["action"] = $action;
  133.         $pendingLogArgs[] = $args;
  134.         if ($noteData['double'])
  135.         {
  136.             $argsBis $this->initArgs($note$loggingData$noteData);
  137.             $argsBis["society_group"] = $noteData['societyGroupAuthor'];
  138.             $argsBis["society"] = $noteData['societyAuthor'];
  139.             $argsBis["action"] = $action;
  140.             $pendingLogArgs[] = $argsBis;
  141.         }
  142.         return $pendingLogArgs;
  143.     }
  144.     private function initArgs(Note $note$loggingData$noteData)
  145.     {
  146.         $receiver $noteData['receiver'];
  147.         $society $noteData['society'];
  148.         $societyGroup $noteData['societyGroup'];        
  149.         $mission $noteData['mission'];        
  150.         $args = array(
  151.             "object_id"                    =>    $note->getId(),
  152.             "object_bundle"                =>    "Platform",
  153.             "object_entity"                =>    "Note",
  154.             "object_display"            =>    $note->display(),
  155.             "object_client_id"            =>    $receiver !== null $receiver->getId() : null,
  156.             "object_client_display"        =>    $receiver !== null $receiver->getName() : null,
  157.             "object_mission_id"            =>    $mission !== null $mission->getId() : null,
  158.             "object_mission_display"    =>    $mission !== null $mission->getRef() : null,
  159.             "society_group"                =>    $societyGroup,
  160.             "society"                    =>    $society,
  161.             "info"                        =>    $loggingData['info'],
  162.             "special_author"            =>    $loggingData['special_author'],
  163.         );
  164.         return $args;
  165.     }
  166.     private function getDataConsideringSharing(Note $note)
  167.     {
  168.         $mission $note->getMission();
  169.         if ($mission === null)
  170.         {
  171.             // Simplest case
  172.             $receiver $note->getClient();
  173.             if ($receiver === null)
  174.             {
  175.                 return null;
  176.             }
  177.             return array(
  178.                 'double'                =>    false,
  179.                 'receiver'                =>    $receiver,
  180.                 'mission'                =>    null,
  181.                 // Society is null if receiver is store
  182.                 'society'                =>    $receiver->getSociety(),
  183.                 'societyGroup'            =>    $receiver->getSocietyGroup(),
  184.                 'societyAuthor'            =>    null,
  185.                 'societyGroupAuthor'    =>    null,
  186.             );
  187.         }
  188.         // Handle sharing for Missions
  189.         // At this point we know that $mission is not null
  190.         $receiver $mission->getReceiver();
  191.         if ($receiver === null)
  192.         {
  193.             return null;
  194.         }
  195.         $society $receiver->getSociety();
  196.         $societyGroup $receiver->getSocietyGroup();
  197.         // At this point we know that $mission and $receiver are not null
  198.         $double false;
  199.         $societyGroupAuthor $mission->getSocietyGroupAuthor();
  200.         $societyGroupOwner $mission->getSocietyGroupOwner();
  201.         $societyAuthor $mission->getSociety();
  202.         $societyOwner $mission->getSocietyOwner();
  203.         if (!$societyGroupAuthor->equals($societyGroupOwner))
  204.         {
  205.             $double true;
  206.             // Primary Log : Owner
  207.             // Secondary Log : Author
  208.             $society $societyOwner;
  209.             $societyGroup $societyGroupOwner;
  210.         }
  211.         return array(
  212.             'double'                =>    true,
  213.             'receiver'                =>    $receiver,
  214.             'mission'                =>    $mission,
  215.             'society'                =>    $society,
  216.             'societyGroup'            =>    $societyGroup,
  217.             'societyAuthor'            =>    $societyAuthor,
  218.             'societyGroupAuthor'    =>    $societyGroupAuthor,
  219.         );
  220.     }
  221.     private function getDataConsideringContext(Note $note)
  222.     {
  223.         $cachedData $this->contextLogger->getContext($note);
  224.         if ($cachedData === null || empty($cachedData))
  225.         {
  226.             return null;
  227.         }
  228.         // Check mission first
  229.         $mission null;
  230.         if (array_key_exists('mission'$cachedData) && $cachedData['mission'] !== null)
  231.         {
  232.             $mission $cachedData['mission'];
  233.         }
  234.         if ($mission === null)
  235.         {
  236.             // Simplest case
  237.             $receiver null;
  238.             if (array_key_exists('client'$cachedData) && $cachedData['client'] !== null)
  239.             {
  240.                 $receiver $cachedData['client'];
  241.             }
  242.             if ($receiver === null)
  243.             {
  244.                 return null;
  245.             }
  246.             return array(
  247.                 'double'                =>    false,
  248.                 'receiver'                =>    $receiver,
  249.                 'mission'                =>    null,
  250.                 // Society is null if receiver is store
  251.                 'society'                =>    $receiver->getSociety(),
  252.                 'societyGroup'            =>    $receiver->getSocietyGroup(),
  253.                 'societyAuthor'            =>    null,
  254.                 'societyGroupAuthor'    =>    null,
  255.             );
  256.         }
  257.         // Handle sharing for Missions
  258.         // At this point we know that $mission is not null
  259.         $receiver $mission->getReceiver();
  260.         if ($receiver === null)
  261.         {
  262.             return null;
  263.         }
  264.         $society $receiver->getSociety();
  265.         $societyGroup $receiver->getSocietyGroup();
  266.         // At this point we know that $mission and $receiver are not null
  267.         $double false;
  268.         $societyGroupAuthor $mission->getSocietyGroupAuthor();
  269.         $societyGroupOwner $mission->getSocietyGroupOwner();
  270.         $societyAuthor $mission->getSociety();
  271.         $societyOwner $mission->getSocietyOwner();
  272.         if (!$societyGroupAuthor->equals($societyGroupOwner))
  273.         {
  274.             $double true;
  275.             // Primary Log : Owner
  276.             // Secondary Log : Author
  277.             $society $societyOwner;
  278.             $societyGroup $societyGroupOwner;
  279.         }
  280.         return array(
  281.             'double'                =>    $double,
  282.             'receiver'                =>    $receiver,
  283.             'mission'                =>    $mission,
  284.             'society'                =>    $society,
  285.             'societyGroup'            =>    $societyGroup,
  286.             'societyAuthor'            =>    $societyAuthor,
  287.             'societyGroupAuthor'    =>    $societyGroupAuthor,
  288.         );
  289.     }
  290. }