src/Logging/Activity/ChargeLog.php line 17

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/ChargeLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Product\Charge;
  8. use App\Logging\Tools;
  9. use App\Services\LogTools;
  10. class ChargeLog
  11. {
  12.     private array $pendingLogArgs = [];
  13.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsTools $tools)
  14.     {
  15.         $this->em $doctrine->getManager();
  16.         $this->logTools $logTools;
  17.         $this->tools $tools;
  18.     }
  19.     public function logCreation(Charge $charge)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData $this->logTools->handleLoggingData($charge);
  25.         $info $loggingData['info'];
  26.         $specialAuthor $loggingData['special_author'];
  27.         $ignore $loggingData['ignore'];
  28.         if ($ignore) return $pendingLogArgs;
  29.         //----------------------------------------------------------------------
  30.         // Init base log args
  31.         // This does not contain action
  32.         $args $this->initArgs($charge$loggingData);
  33.         //----------------------------------------------------------------------
  34.         $action "charge_add";
  35.         $args["action"] = $action;
  36.         $pendingLogArgs[] = $args;
  37.         return $pendingLogArgs;
  38.     }
  39.     public function logChanges(Charge $charge$changes)
  40.     {
  41.         $pendingLogArgs = [];
  42.         //----------------------------------------------------------------------
  43.         // Fetch eventual info skeletond in the object
  44.         $loggingData $this->logTools->handleLoggingData($charge);
  45.         $info $loggingData['info'];
  46.         $specialAuthor $loggingData['special_author'];
  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($charge$loggingData);
  53.         //----------------------------------------------------------------------
  54.         $action "charge_edit";
  55.         $basicChanges = array(
  56.             "ref",
  57.             "title",
  58.             "duration"
  59.         );
  60.         $labelChanges = array(
  61.             "unit",
  62.         );
  63.         // Method getLogLabel() should be defined for these objects/entities
  64.         $objectChanges = array(
  65.         );
  66.         // See what changed and log (members first)
  67.         foreach ($changes as $key => $change)
  68.         {
  69.             // Something to skip ?
  70.             if (false)
  71.             {
  72.                 continue;
  73.             }
  74.             $name $this->logTools->camelToSnakeCase($key);
  75.             $args["action"]    = $action."_".$name;
  76.             $before $change[0];
  77.             $after $change[1];
  78.             if (in_array($key$basicChanges))
  79.             {
  80.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  81.                 continue;
  82.             }
  83.             if (in_array($key$labelChanges))
  84.             {
  85.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  86.                 continue;
  87.             }
  88.             if (in_array($key$objectChanges))
  89.             {
  90.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  91.                 continue;
  92.             }
  93.         }
  94.         // Handle ManyToMany
  95.         if ($charge->getTemplates()->isDirty())
  96.         {
  97.             $oldData "";
  98.             $newData "";
  99.             foreach ($charge->getTemplates()->getSnapshot() as $template)
  100.             {
  101.                 $oldData .= $template->display();
  102.                 $oldData .= ", ";
  103.             }
  104.             foreach ($charge->getTemplates() as $template)
  105.             {
  106.                 $newData .= $template->display();
  107.                 $newData .= ", ";
  108.             }
  109.             if ($oldData != "")
  110.             {
  111.                 $oldData substr($oldData0, -1);
  112.                 $oldData substr($oldData0, -1);
  113.             }
  114.             if ($newData != "")
  115.             {
  116.                 $newData substr($newData0, -1);
  117.                 $newData substr($newData0, -1);
  118.             }
  119.             $args["action"] = "charge_edit_templates";
  120.             $args["old_value"] = $oldData;
  121.             $args["new_value"] = $newData;
  122.             $pendingLogArgs[] = $args;
  123.         }
  124.         return $pendingLogArgs;
  125.     }
  126.     public function logRemoval(Charge $charge)
  127.     {
  128.         $pendingLogArgs = [];
  129.         //----------------------------------------------------------------------
  130.         // Fetch eventual info skeletond in the object
  131.         $loggingData $this->logTools->handleLoggingData($charge);
  132.         $info $loggingData['info'];
  133.         $specialAuthor $loggingData['special_author'];
  134.         $ignore $loggingData['ignore'];
  135.         if ($ignore) return $pendingLogArgs;
  136.         //----------------------------------------------------------------------
  137.         // Init base log args
  138.         // This does not contain action
  139.         $args $this->initArgs($charge$loggingData);
  140.         //----------------------------------------------------------------------
  141.         $args["action"] = "charge_delete";
  142.         $pendingLogArgs[] = $args;
  143.         return $pendingLogArgs;
  144.     }
  145.     private function initArgs(Charge $charge$loggingData)
  146.     {
  147.         $societyGroup $charge->getSocietyGroup();
  148.         $args = array(
  149.             "object_id"                    =>    $charge->getId(),
  150.             "object_bundle"                =>    "Product",
  151.             "object_entity"                =>    "Charge",
  152.             "object_display"            =>    $charge->display(),
  153.             "society_group"                =>    $societyGroup,
  154.             "info"                        =>    $loggingData['info'],
  155.             "special_author"            =>    $loggingData['special_author'],
  156.         );
  157.         return $args;
  158.     }
  159. }