src/AppRemix/Logging/Objects/PaymentLog.php line 17

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/AppRemix/Logging/PaymentLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\AppRemix\Logging\Objects;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\AppRemix\Entity\Payment;
  8. use App\AppRemix\Logging\Tools;
  9. use App\Services\LogTools;
  10. class PaymentLog
  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(Payment $payment)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData = $this->logTools->handleLoggingData($payment);
  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($payment, $loggingData);
  32.         //----------------------------------------------------------------------
  33.         $action = "payment_add";
  34.         $args["action"] = $action;
  35.         $pendingLogArgs[] = $args;
  36.         return $pendingLogArgs;
  37.     }
  38.     public function logChanges(Payment $payment, $changes)
  39.     {
  40.         $pendingLogArgs = [];
  41.         //----------------------------------------------------------------------
  42.         // Fetch eventual info stored in the object
  43.         $loggingData = $this->logTools->handleLoggingData($payment);
  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($payment, $loggingData);
  51.         //----------------------------------------------------------------------
  52.         $editMode = "payment_edit";
  53.         $args["action"] = $editMode;
  54.         $jsonChanges = array(
  55.         );
  56.         $basicChanges = array(                                
  57.             "amount",
  58.             "urlPayment",
  59.             "stripeLinkId",
  60.             "info",
  61.         );
  62.         // Method getLogLabel() should be defined for these objects/entities
  63.         $objectChanges = array(
  64.             "devis",
  65.             "invoice",
  66.         );
  67.         $dateChanges = array(
  68.             "paymentDate",
  69.             "sentToClient",
  70.             "creationDate",
  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"]    = $editMode."_".$name;
  82.             $before = $change[0];
  83.             $after = $change[1];
  84.             // Handle Special cases
  85.             if ($key == "status")
  86.             {
  87.                 $pendingLogArgs[] = $this->tools->handlePaymentStatusChanges($args, $before, $after);
  88.                 continue;
  89.             }
  90.             if ($key == "type")
  91.             {
  92.                 $pendingLogArgs[] = $this->tools->handlePaymentTypeChanges($args, $before, $after);
  93.                 continue;
  94.             }
  95.             if (in_array($key, $basicChanges))
  96.             {
  97.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
  98.                 continue;
  99.             }
  100.             if (in_array($key, $objectChanges))
  101.             {
  102.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args, $before, $after);
  103.                 continue;
  104.             }
  105.             if (in_array($key, $dateChanges))
  106.             {
  107.                 // dateChanges can be null
  108.                 $changeLogs = $this->tools->handleDateChanges($args, $before, $after);
  109.                 if ($changeLogs !== null) $pendingLogArgs[] = $changeLogs;
  110.                 continue;
  111.             }
  112.             if (in_array($key, $jsonChanges))
  113.             {
  114.                 $pendingLogArgs[] = $this->tools->handleJsonChanges($args, $before, $after);
  115.                 continue;
  116.             }
  117.         }
  118.         return $pendingLogArgs;
  119.     }
  120.     public function logRemoval(Payment $payment)
  121.     {
  122.         $pendingLogArgs = [];
  123.         return $pendingLogArgs;
  124.     }
  125.     private function initArgs(Payment $payment, $loggingData)
  126.     {
  127.         $client = $payment->getClient();
  128.         $args = array(
  129.             "object_id"                    =>    $payment->getId(),            
  130.             "object_entity"                =>    "Payment",
  131.             "object_display"            =>    $payment->display(),
  132.             
  133.             "society"                    =>    $payment->getSociety(),
  134.             // Null for a client created in this flush — handleClient() fills it post-flush.
  135.             "client_id"                    =>    $client?->getId(),
  136.             "client_display"            =>    $client?->displayForListing(),
  137.             "info"                        =>    $loggingData['info'],
  138.         );
  139.         return $args;
  140.     }
  141. }