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

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/AppRemix/Logging/PurchaseLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\AppRemix\Logging\Objects;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\AppRemix\Entity\Purchase;
  8. use App\AppRemix\Logging\Tools;
  9. use App\Services\LogTools;
  10. class PurchaseLog
  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(Purchase $purchase)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData = $this->logTools->handleLoggingData($purchase);
  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($purchase, $loggingData);
  32.         //----------------------------------------------------------------------
  33.         $action = "purchase_add";
  34.         $args["action"] = $action;
  35.         $pendingLogArgs[] = $args;
  36.         return $pendingLogArgs;
  37.     }
  38.     public function logChanges(Purchase $purchase, $changes)
  39.     {
  40.         $pendingLogArgs = [];
  41.         //----------------------------------------------------------------------
  42.         // Fetch eventual info stored in the object
  43.         $loggingData = $this->logTools->handleLoggingData($purchase);
  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($purchase, $loggingData);
  51.         //----------------------------------------------------------------------
  52.         $action = "purchase_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.         $jsonChanges = array(
  56.         );
  57.         $basicChanges = array(                                
  58.             "amount",
  59.             "label",            
  60.         );
  61.         $dateChanges = array(
  62.             "purchaseDate",
  63.         );
  64.         // See what changed and log (members first)
  65.         foreach ($changes as $key => $change)
  66.         {
  67.             // Something to skip ?
  68.             if (false)
  69.             {
  70.                 continue;
  71.             }
  72.             $name = $this->logTools->camelToSnakeCase($key);
  73.             $args["action"]    = $action."_".$name;
  74.             $before = $change[0];
  75.             $after = $change[1];
  76.             if (in_array($key, $basicChanges))
  77.             {
  78.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
  79.                 continue;
  80.             }
  81.             if (in_array($key, $dateChanges))
  82.             {
  83.                 // dateChanges can be null
  84.                 $changeLogs = $this->tools->handleDateChanges($args, $before, $after);
  85.                 if ($changeLogs !== null) $pendingLogArgs[] = $changeLogs;
  86.                 continue;
  87.             }
  88.             if (in_array($key, $jsonChanges))
  89.             {
  90.                 $pendingLogArgs[] = $this->tools->handleJsonChanges($args, $before, $after);
  91.                 continue;
  92.             }
  93.         }
  94.         return $pendingLogArgs;
  95.     }
  96.     public function logRemoval(Purchase $purchase)
  97.     {
  98.         $pendingLogArgs = [];
  99.         //----------------------------------------------------------------------
  100.         // Fetch eventual info stored in the object
  101.         $loggingData = $this->logTools->handleLoggingData($purchase);
  102.         $info = $loggingData['info'];        
  103.         $ignore = $loggingData['ignore'];
  104.         if ($ignore) return $pendingLogArgs;
  105.         //----------------------------------------------------------------------
  106.         // Init base log args
  107.         // This does not contain action
  108.         $args = $this->initArgs($purchase, $loggingData);
  109.         //----------------------------------------------------------------------
  110.         $args["action"] = "purchase_delete";
  111.         $pendingLogArgs[] = $args;
  112.         return $pendingLogArgs;
  113.     }
  114.     private function initArgs(Purchase $purchase, $loggingData)
  115.     {
  116.         $args = array(
  117.             "object_id"                    =>    $purchase->getId(),            
  118.             "object_entity"                =>    "Purchase",
  119.             "object_display"            =>    $purchase->display(),
  120.             
  121.             "society"                    =>    $purchase->getSociety(),
  122.             "info"                        =>    $loggingData['info'],            
  123.         );
  124.         return $args;
  125.     }
  126. }