src/AppRemix/Logging/Objects/ClientLog.php line 21

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/AppRemix/Logging/ClientLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\AppRemix\Logging\Objects;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\AppRemix\Entity\Client;
  8. use App\AppRemix\Logging\Tools;
  9. use App\Services\LogTools;
  10. class ClientLog
  11. {
  12.     private array $pendingLogArgs = [];
  13.     public function __construct(ManagerRegistry $doctrine, LogTools $logTools, Tools $tools)
  14.     {
  15.         $this->em = $doctrine->getManager();
  16.         $this->logTools = $logTools;
  17.         $this->tools = $tools;
  18.     }
  19.     public function logCreation(Client $client)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData = $this->logTools->handleLoggingData($client);
  25.         $info = $loggingData['info'];        
  26.         $ignore = $loggingData['ignore'];
  27.         if ($ignore) return $pendingLogArgs;
  28.         //----------------------------------------------------------------------
  29.         // Init base log args
  30.         // This does not contain action
  31.         $args = $this->initArgs($client, $loggingData);
  32.         //----------------------------------------------------------------------
  33.         $action = "client_add";
  34.         $args["action"] = $action;
  35.         $pendingLogArgs[] = $args;
  36.         return $pendingLogArgs;
  37.     }
  38.     public function logChanges(Client $client, $changes)
  39.     {
  40.         $pendingLogArgs = [];
  41.         //----------------------------------------------------------------------
  42.         // Fetch eventual info stored in the object
  43.         $loggingData = $this->logTools->handleLoggingData($client);
  44.         $info = $loggingData['info'];        
  45.         $ignore = $loggingData['ignore'];
  46.         if ($ignore) return $pendingLogArgs;
  47.         //----------------------------------------------------------------------
  48.         // Init base log args
  49.         // This does not contain action
  50.         $args = $this->initArgs($client, $loggingData);
  51.         //----------------------------------------------------------------------
  52.         $action = "client_edit";
  53.         // Reachable today through devis/update and invoice/update: company, phone, email.
  54.         // name and interventionAddress are not editable yet — listed so the day they are,
  55.         // their changes are logged without touching this class.
  56.         $basicChanges = array(
  57.             "name",
  58.             "company",
  59.             "phone",
  60.             "email",
  61.         );
  62.         $jsonChanges = array(
  63.             "interventionAddress",
  64.         );
  65.         // See what changed and log (members first)
  66.         foreach ($changes as $key => $change)
  67.         {
  68.             // Something to skip ?
  69.             if (false)
  70.             {
  71.                 continue;
  72.             }
  73.             $name = $this->logTools->camelToSnakeCase($key);
  74.             $args["action"]    = $action."_".$name;
  75.             $before = $change[0];
  76.             $after = $change[1];
  77.             if (in_array($key, $jsonChanges))
  78.             {
  79.                 $pendingLogArgs[] = $this->tools->handleJsonChanges($args, $before, $after);
  80.                 continue;
  81.             }
  82.             if (in_array($key, $basicChanges))
  83.             {
  84.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
  85.                 continue;
  86.             }
  87.         }
  88.         return $pendingLogArgs;
  89.     }
  90.     public function logRemoval(Client $client)
  91.     {
  92.         $pendingLogArgs = [];
  93.         return $pendingLogArgs;
  94.     }
  95.     private function initArgs(Client $client, $loggingData)
  96.     {
  97.         $args = array(
  98.             "object_id"                    =>    $client->getId(),
  99.             "object_entity"                =>    "Client",
  100.             "object_display"            =>    $client->display(),
  101.             "society"                    =>    $client->getSociety(),
  102.             // Null for a client created in this flush — handleClient() fills it post-flush.
  103.             "client_id"                    =>    $client->getId(),
  104.             "client_display"            =>    $client->displayForListing(),
  105.             "info"                        =>    $loggingData['info'],
  106.         );
  107.         return $args;
  108.     }
  109. }