src/AppRemix/Logging/Objects/OpenAIPriceLog.php line 26

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/AppRemix/Logging/Objects/OpenAIPriceLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\AppRemix\Logging\Objects;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\AppRemix\Entity\OpenAIPrice;
  8. use App\AppRemix\Logging\Tools;
  9. use App\Services\LogTools;
  10. /**
  11.  * Activity log for the price of an OpenAI model.
  12.  *
  13.  * Creation is logged. Each of the three prices is logged with its old and new
  14.  * value. updateDate and updatedBy change on every save and are not logged.
  15.  * The model name is the identity of the row, not a price, so it is not logged.
  16.  */
  17. class OpenAIPriceLog
  18. {
  19.     public function __construct(ManagerRegistry $doctrine, LogTools $logTools, Tools $tools)
  20.     {
  21.         $this->em = $doctrine->getManager();
  22.         $this->logTools = $logTools;
  23.         $this->tools = $tools;
  24.     }
  25.     public function logCreation(OpenAIPrice $price)
  26.     {
  27.         $pendingLogArgs = [];
  28.         //----------------------------------------------------------------------
  29.         // Fetch eventual info stored in the object
  30.         $loggingData = $this->logTools->handleLoggingData($price);
  31.         $info = $loggingData['info'];
  32.         $ignore = $loggingData['ignore'];
  33.         if ($ignore) return $pendingLogArgs;
  34.         //----------------------------------------------------------------------
  35.         // Init base log args
  36.         // This does not contain action
  37.         $args = $this->initArgs($price, $loggingData);
  38.         //----------------------------------------------------------------------
  39.         $action = "open_ai_price_add";
  40.         $args["action"] = $action;
  41.         $pendingLogArgs[] = $args;
  42.         return $pendingLogArgs;
  43.     }
  44.     public function logChanges(OpenAIPrice $price, $changes)
  45.     {
  46.         $pendingLogArgs = [];
  47.         //----------------------------------------------------------------------
  48.         // Fetch eventual info stored in the object
  49.         $loggingData = $this->logTools->handleLoggingData($price);
  50.         $info = $loggingData['info'];
  51.         $ignore = $loggingData['ignore'];
  52.         if ($ignore) return $pendingLogArgs;
  53.         //----------------------------------------------------------------------
  54.         // Init base log args
  55.         // This does not contain action
  56.         $args = $this->initArgs($price, $loggingData);
  57.         //----------------------------------------------------------------------
  58.         $action = "open_ai_price_edit";
  59.         $jsonChanges = array(
  60.         );
  61.         // updateDate and updatedBy change on every save and are not logged.
  62.         $basicChanges = array(
  63.             "inputPrice",
  64.             "cachedInputPrice",
  65.             "outputPrice",
  66.         );
  67.         foreach ($changes as $key => $change)
  68.         {
  69.             $name = $this->logTools->camelToSnakeCase($key);
  70.             $args["action"]    = $action."_".$name;
  71.             $before = $change[0];
  72.             $after = $change[1];
  73.             if (in_array($key, $jsonChanges))
  74.             {
  75.                 $pendingLogArgs[] = $this->tools->handleJsonChanges($args, $before, $after);
  76.                 continue;
  77.             }
  78.             if (in_array($key, $basicChanges))
  79.             {
  80.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args, $before, $after);
  81.                 continue;
  82.             }
  83.         }
  84.         return $pendingLogArgs;
  85.     }
  86.     public function logRemoval(OpenAIPrice $price)
  87.     {
  88.         $pendingLogArgs = [];
  89.         return $pendingLogArgs;
  90.     }
  91.     private function initArgs(OpenAIPrice $price, $loggingData)
  92.     {
  93.         $args = array(
  94.             "object_id"                    =>    $price->getId(),
  95.             "object_entity"                =>    "OpenAIPrice",
  96.             "object_display"            =>    $price->display(),
  97.             "info"                        =>    $loggingData['info'],
  98.         );
  99.         return $args;
  100.     }
  101. }