src/Logging/Activity/TemplateLog.php line 19

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/TemplateLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Product\Template;
  8. use App\Logging\Tools;
  9. use App\Services\LogTools;
  10. class TemplateLog
  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(Template $template)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData $this->logTools->handleLoggingData($template);
  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($template$loggingData);
  33.         //----------------------------------------------------------------------
  34.         $action "template_add";
  35.         $args["action"] = $action;
  36.         $pendingLogArgs[] = $args;
  37.         return $pendingLogArgs;
  38.     }
  39.     public function logChanges(Template $template$changes)
  40.     {
  41.         $pendingLogArgs = [];
  42.         //----------------------------------------------------------------------
  43.         // Fetch eventual info skeletond in the object
  44.         $loggingData $this->logTools->handleLoggingData($template);
  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($template$loggingData);
  53.         //----------------------------------------------------------------------
  54.         $action "template_edit";
  55.         $basicChanges = array(
  56.             "ref",
  57.             "title",
  58.             "description",
  59.             "zoneCostUnits",
  60.         );
  61.         $basicBoolChanges = array(
  62.             "isActive",
  63.             "hasFreeProduct",
  64.             "emitterEqualsReceiver",
  65.             "includeAlways",
  66.             "useLinearMeters",
  67.         );
  68.         $labelChanges = array(
  69.         );
  70.         // Method getLogLabel() should be defined for these objects/entities
  71.         $objectChanges = array(
  72.             "tva",
  73.             "defaultInterventionZoneCostProduct",
  74.         );
  75.         // See what changed and log (members first)
  76.         foreach ($changes as $key => $change)
  77.         {
  78.             // Something to skip ?
  79.             if (false)
  80.             {
  81.                 continue;
  82.             }
  83.             $name $this->logTools->camelToSnakeCase($key);
  84.             $args["action"]    = $action."_".$name;
  85.             $before $change[0];
  86.             $after $change[1];
  87.             if (in_array($key$basicChanges))
  88.             {
  89.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  90.                 continue;
  91.             }
  92.             if (in_array($key$basicBoolChanges))
  93.             {
  94.                 $pendingLogArgs[] = $this->tools->handleBasicBoolChanges($args$before$after);
  95.                 continue;
  96.             }
  97.             if (in_array($key$labelChanges))
  98.             {
  99.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  100.                 continue;
  101.             }
  102.             if (in_array($key$objectChanges))
  103.             {
  104.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  105.                 continue;
  106.             }
  107.         }
  108.         // Handle ManyToMany
  109.         if ($template->getProducts()->isDirty())
  110.         {
  111.             $oldData "";
  112.             $newData "";
  113.             foreach ($template->getProducts()->getSnapshot() as $product)
  114.             {
  115.                 $oldData .= $product->display();
  116.                 $oldData .= ", ";
  117.             }
  118.             foreach ($template->getProducts() as $product)
  119.             {
  120.                 $newData .= $product->display();
  121.                 $newData .= ", ";
  122.             }
  123.             if ($oldData != "")
  124.             {
  125.                 $oldData substr($oldData0, -1);
  126.                 $oldData substr($oldData0, -1);
  127.             }
  128.             if ($newData != "")
  129.             {
  130.                 $newData substr($newData0, -1);
  131.                 $newData substr($newData0, -1);
  132.             }
  133.             $args["action"] = "template_edit_products";
  134.             $args["old_value"] = $oldData;
  135.             $args["new_value"] = $newData;
  136.             $pendingLogArgs[] = $args;
  137.         }
  138.         if ($template->getCharges()->isDirty())
  139.         {
  140.             $oldData "";
  141.             $newData "";
  142.             foreach ($template->getCharges()->getSnapshot() as $charge)
  143.             {
  144.                 $oldData .= $charge->display();
  145.                 $oldData .= ", ";
  146.             }
  147.             foreach ($template->getCharges() as $charge)
  148.             {
  149.                 $newData .= $charge->display();
  150.                 $newData .= ", ";
  151.             }
  152.             if ($oldData != "")
  153.             {
  154.                 $oldData substr($oldData0, -1);
  155.                 $oldData substr($oldData0, -1);
  156.             }
  157.             if ($newData != "")
  158.             {
  159.                 $newData substr($newData0, -1);
  160.                 $newData substr($newData0, -1);
  161.             }
  162.             $args["action"] = "template_edit_charges";
  163.             $args["old_value"] = $oldData;
  164.             $args["new_value"] = $newData;
  165.             $pendingLogArgs[] = $args;
  166.         }
  167.         return $pendingLogArgs;
  168.     }
  169.     public function logRemoval(Template $template)
  170.     {
  171.         $pendingLogArgs = [];
  172.         //----------------------------------------------------------------------
  173.         // Fetch eventual info skeletond in the object
  174.         $loggingData $this->logTools->handleLoggingData($template);
  175.         $info $loggingData['info'];
  176.         $specialAuthor $loggingData['special_author'];
  177.         $ignore $loggingData['ignore'];
  178.         if ($ignore) return $pendingLogArgs;
  179.         //----------------------------------------------------------------------
  180.         // Init base log args
  181.         // This does not contain action
  182.         $args $this->initArgs($template$loggingData);
  183.         //----------------------------------------------------------------------
  184.         $args["action"] = "template_delete";
  185.         $pendingLogArgs[] = $args;
  186.         return $pendingLogArgs;
  187.     }
  188.     private function initArgs(Template $template$loggingData)
  189.     {
  190.         $societyGroup $template->getSocietyGroup();
  191.         $args = array(
  192.             "object_id"                    =>    $template->getId(),
  193.             "object_bundle"                =>    "Product",
  194.             "object_entity"                =>    "Template",
  195.             "object_display"            =>    $template->display(),
  196.             "society_group"                =>    $societyGroup,
  197.             "info"                        =>    $loggingData['info'],
  198.             "special_author"            =>    $loggingData['special_author'],
  199.         );
  200.         return $args;
  201.     }
  202. }