src/Logging/Activity/ProductLog.php line 17

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