src/Logging/Activity/SocietyGroupLog.php line 22

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/SocietyGroupLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\SocietyGroup;
  8. use App\Logging\Tools;
  9. use App\Services\LogTools;
  10. class SocietyGroupLog
  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(SocietyGroup $societyGroup)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData $this->logTools->handleLoggingData($societyGroup);
  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($societyGroup$loggingData);
  33.         //----------------------------------------------------------------------
  34.         $args["action"] = "society_group_add";
  35.         $pendingLogArgs[] = $args;
  36.         return $pendingLogArgs;
  37.     }
  38.     public function logChanges(SocietyGroup $societyGroup$changes)
  39.     {
  40.         $pendingLogArgs = [];
  41.         //----------------------------------------------------------------------
  42.         // Fetch eventual info skeletond in the object
  43.         $loggingData $this->logTools->handleLoggingData($societyGroup);
  44.         $info $loggingData['info'];
  45.         $specialAuthor $loggingData['special_author'];
  46.         $ignore $loggingData['ignore'];
  47.         if ($ignore) return $pendingLogArgs;
  48.         //----------------------------------------------------------------------
  49.         // Init base log args
  50.         // This does not contain action
  51.         $args $this->initArgs($societyGroup$loggingData);
  52.         //----------------------------------------------------------------------
  53.         $action "society_group_edit";
  54.         // This is triggered when we are actually changing the connected Address object
  55.         // The changes in the actual object are handled in AddressLog
  56.         $addressChanges = array(
  57.             "address",
  58.             "billingAddress",
  59.         );
  60.         $basicChanges = array(
  61.             "ref",
  62.             "name",
  63.             "contactEmail",
  64.             "noreplyEmail",
  65.             "siret",
  66.         );
  67.         $basicBoolChanges = array(
  68.             "isActive",
  69.         );
  70.         $dateChanges = array(
  71.         );
  72.         $labelChanges = array(
  73.         );
  74.         // Method getLogLabel() should be defined for these objects/entities
  75.         $objectChanges = array(
  76.             "admin",
  77.             "defaultManager",
  78.         );
  79.         // See what changed and log (members first)
  80.         foreach ($changes as $key => $change)
  81.         {
  82.             // Something to skip ?
  83.             if (false)
  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$addressChanges))
  92.             {
  93.                 $pendingLogArgs[] = $this->tools->handleAddressChanges($args$before$after);
  94.                 continue;
  95.             }
  96.             if (in_array($key$basicChanges))
  97.             {
  98.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  99.                 continue;
  100.             }
  101.             if (in_array($key$basicBoolChanges))
  102.             {
  103.                 $args $this->tools->handleBasicBoolChanges($args$before$after);
  104.                 if ($key == "isActive")
  105.                 {
  106.                     if (!$before && $after$args['action'] = "society_group_activate";
  107.                     if ($before && !$after$args['action'] = "society_group_deactivate";
  108.                     $args['old_value'] = null;
  109.                     $args['new_value'] = null;
  110.                 }
  111.                 $pendingLogArgs[] = $args;
  112.                 continue;
  113.             }
  114.             if (in_array($key$dateChanges))
  115.             {
  116.                 // dateChanges can be null
  117.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  118.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  119.                 continue;
  120.             }
  121.             if (in_array($key$labelChanges))
  122.             {
  123.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  124.                 continue;
  125.             }
  126.             if (in_array($key$objectChanges))
  127.             {
  128.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  129.                 continue;
  130.             }
  131.         }
  132.         // Handle ManyToMany
  133.         if ($societyGroup->getSocieties()->isDirty())
  134.         {
  135.             $oldData "";
  136.             $newData "";
  137.             foreach ($societyGroup->getSocieties()->getSnapshot() as $society)
  138.             {
  139.                 $oldData .= $society->display();
  140.                 $oldData .= ", ";
  141.             }
  142.             foreach ($societyGroup->getSocieties() as $society)
  143.             {
  144.                 $newData .= $society->display();
  145.                 $newData .= ", ";
  146.             }
  147.             if ($oldData != "")
  148.             {
  149.                 $oldData substr($oldData0, -1);
  150.                 $oldData substr($oldData0, -1);
  151.             }
  152.             if ($newData != "")
  153.             {
  154.                 $newData substr($newData0, -1);
  155.                 $newData substr($newData0, -1);
  156.             }
  157.             $args["action"] = "society_group_edit_societies";
  158.             $args["old_value"] = $oldData;
  159.             $args["new_value"] = $newData;
  160.             $pendingLogArgs[] = $args;
  161.         }
  162.         return $pendingLogArgs;
  163.     }
  164.     public function logRemoval(SocietyGroup $societyGroup)
  165.     {
  166.         $pendingLogArgs = [];
  167.         return $pendingLogArgs;
  168.     }
  169.     private function initArgs(SocietyGroup $societyGroup$loggingData)
  170.     {
  171.         $args = array(
  172.             "object_id"                    =>    $societyGroup->getId(),
  173.             "object_bundle"                =>    "Platform",
  174.             "object_entity"                =>    "SocietyGroup",
  175.             "object_display"            =>    $societyGroup->display(),
  176.             "society_group"                =>    $societyGroup,
  177.             "info"                        =>    $loggingData['info'],
  178.             "special_author"            =>    $loggingData['special_author'],
  179.         );
  180.         return $args;
  181.     }
  182.     public function handleSocietyGroupCreation($log$object)
  183.     {
  184.         if ($log->getAction() == "society_group_add")
  185.         {
  186.             $log->setSocietyGroupId($object->getId());
  187.         }
  188.     }
  189. }