src/AppRemix/Logging/Objects/SocietyLog.php line 20

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/AppRemix/Logging/SocietyLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\AppRemix\Logging\Objects;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\AppRemix\Entity\Society;
  8. use App\AppRemix\Logging\Tools;
  9. use App\Services\LogTools;
  10. class SocietyLog
  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(Society $society)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData = $this->logTools->handleLoggingData($society);
  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($society, $loggingData);
  32.         //----------------------------------------------------------------------
  33.         $action = "society_add";
  34.         $args["action"] = $action;
  35.         $pendingLogArgs[] = $args;
  36.         return $pendingLogArgs;
  37.     }
  38.     public function logChanges(Society $society, $changes)
  39.     {
  40.         $pendingLogArgs = [];
  41.         //----------------------------------------------------------------------
  42.         // Fetch eventual info stored in the object
  43.         $loggingData = $this->logTools->handleLoggingData($society);
  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($society, $loggingData);
  51.         //----------------------------------------------------------------------
  52.         $action = "society_edit";
  53.         // This is triggered when we are actually changing the connected Address object
  54.         // The changes in the actual object are handled in AddressLog
  55.         // info is a json column (siret, ape, tva, RSC, capital, insurance): it must go through
  56.         // handleJsonChanges() so the array is encoded before smartLog() cleans it as text.
  57.         $jsonChanges = array(
  58.             "address",
  59.             "info",
  60.         );
  61.         $basicChanges = array(                    
  62.             "name",
  63.             "email",            
  64.             "phone",
  65.             "address",
  66.             "info",
  67.             "iban",
  68.             "stripeToken",
  69.             // Not closureRequestedAt: it changes with the flag and would log a second row.
  70.             "closureRequested",
  71.         );
  72.         // See what changed and log (members first)
  73.         foreach ($changes as $key => $change)
  74.         {
  75.             // Something to skip ?
  76.             if (false)
  77.             {
  78.                 continue;
  79.             }
  80.             $name = $this->logTools->camelToSnakeCase($key);
  81.             $args["action"]    = $action."_".$name;
  82.             $before = $change[0];
  83.             $after = $change[1];
  84.             if (in_array($key, $jsonChanges))
  85.             {
  86.                 $pendingLogArgs[] = $this->tools->handleJsonChanges($args, $before, $after);
  87.                 continue;
  88.             }
  89.             if (in_array($key, $basicChanges))
  90.             {
  91.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
  92.                 continue;
  93.             }
  94.         }
  95.         return $pendingLogArgs;
  96.     }
  97.     public function logRemoval(Society $society)
  98.     {
  99.         $pendingLogArgs = [];
  100.         return $pendingLogArgs;
  101.     }
  102.     private function initArgs(Society $society, $loggingData)
  103.     {
  104.         $args = array(
  105.             "object_id"                    =>    $society->getId(),            
  106.             "object_entity"                =>    "Society",
  107.             "object_display"            =>    $society->display(),
  108.             
  109.             "society"                    =>    $society,
  110.             "info"                        =>    $loggingData['info'],            
  111.         );
  112.         return $args;
  113.     }
  114. }