src/Logging/Activity/Prospect/ReportLog.php line 24

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