src/AppRemix/Logging/Objects/OpenAIConfigLog.php line 22

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