src/Logging/Activity/CostLog.php line 20

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/CostLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Platform\Cost\Cost;
  8. use App\Logging\Tools;
  9. use App\Services\LogTools;
  10. class CostLog
  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(Cost $cost)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData $this->logTools->handleLoggingData($cost);
  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($cost$loggingData);
  33.         //----------------------------------------------------------------------
  34.         $action "cost_add";
  35.         $args["action"] = $action;
  36.         $pendingLogArgs[] = $args;
  37.         return $pendingLogArgs;
  38.     }
  39.     public function logChanges(Cost $cost$changes)
  40.     {
  41.         $pendingLogArgs = [];
  42.         //----------------------------------------------------------------------
  43.         // Fetch eventual info skeletond in the object
  44.         $loggingData $this->logTools->handleLoggingData($cost);
  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($cost$loggingData);
  53.         //----------------------------------------------------------------------
  54.         $action "cost_edit";
  55.         $basicChanges = array(
  56.             "ref",
  57.             "totalTTC",
  58.             "info",
  59.         );
  60.         $dateChanges = array(
  61.             "creationDate",
  62.         );
  63.         $labelChanges = array(
  64.             "status",
  65.             "type",
  66.             "category",
  67.         );
  68.         // Method getLogLabel() should be defined for these objects/entities
  69.         $objectChanges = array(
  70.             "society",
  71.             "supplier",
  72.             "client",
  73.         );
  74.         $skipChildren = array(
  75.             "status",
  76.             "type",
  77.         );
  78.         // See what changed and log (members first)
  79.         foreach ($changes as $key => $change)
  80.         {
  81.             // Something to skip ?
  82.             // Some Label Changes are applied to the parent and all its children => Skip the Children
  83.             if (in_array($key$skipChildren) && $cost->getParent() !== null)
  84.             {
  85.                 continue;
  86.             }
  87.             $name $this->logTools->camelToSnakeCase($key);
  88.             $args["action"]    = $action."_".$name;
  89.             $before $change[0];
  90.             $after $change[1];
  91.             if (in_array($key$basicChanges))
  92.             {
  93.                 if ($before == $after) continue;
  94.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  95.                 continue;
  96.             }
  97.             if (in_array($key$dateChanges))
  98.             {
  99.                 // dateChanges can be null
  100.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  101.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  102.                 continue;
  103.             }
  104.             if (in_array($key$labelChanges))
  105.             {
  106.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  107.                 continue;
  108.             }
  109.             if (in_array($key$objectChanges))
  110.             {
  111.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  112.                 continue;
  113.             }
  114.         }
  115.         // Handle ManyToMany
  116.         if ($cost->getCommands()->isDirty())
  117.         {
  118.             $oldData "";
  119.             $newData "";
  120.             foreach ($cost->getCommands()->getSnapshot() as $command)
  121.             {
  122.                 $oldData .= $command->display();
  123.                 $oldData .= ", ";
  124.             }
  125.             foreach ($cost->getCommands() as $command)
  126.             {
  127.                 $newData .= $command->display();
  128.                 $newData .= ", ";
  129.             }
  130.             if ($oldData != "")
  131.             {
  132.                 $oldData substr($oldData0, -1);
  133.                 $oldData substr($oldData0, -1);
  134.             }
  135.             if ($newData != "")
  136.             {
  137.                 $newData substr($newData0, -1);
  138.                 $newData substr($newData0, -1);
  139.             }
  140.             $args["action"] = "cost_edit_commands";
  141.             $args["old_value"] = $oldData;
  142.             $args["new_value"] = $newData;
  143.             $pendingLogArgs[] = $args;
  144.         }
  145.         return $pendingLogArgs;
  146.     }
  147.     public function logRemoval(Cost $cost)
  148.     {
  149.         $pendingLogArgs = [];
  150.         //----------------------------------------------------------------------
  151.         // Fetch eventual info skeletond in the object
  152.         $loggingData $this->logTools->handleLoggingData($cost);
  153.         $info $loggingData['info'];
  154.         $specialAuthor $loggingData['special_author'];
  155.         $ignore $loggingData['ignore'];
  156.         if ($ignore) return $pendingLogArgs;
  157.         //----------------------------------------------------------------------
  158.         // Init base log args
  159.         // This does not contain action
  160.         $args $this->initArgs($cost$loggingData);
  161.         //----------------------------------------------------------------------
  162.         $args["action"] = "cost_delete";
  163.         $pendingLogArgs[] = $args;
  164.         return $pendingLogArgs;
  165.     }
  166.     private function initArgs(Cost $cost$loggingData)
  167.     {
  168.         $society $cost->getSociety();
  169.         $societyGroup $cost->getSocietyGroup();
  170.         $args = array(
  171.             "object_id"                    =>    $cost->getId(),
  172.             "object_bundle"                =>    "Platform",
  173.             "object_entity"                =>    "Cost",
  174.             "object_display"            =>    $cost->display(),
  175.             "society_group"                =>    $societyGroup,
  176.             "society"                    =>    $society,
  177.             "info"                        =>    $loggingData['info'],
  178.             "special_author"            =>    $loggingData['special_author'],
  179.         );
  180.         return $args;
  181.     }
  182. }