src/Logging/Activity/SocietyLog.php line 17

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/SocietyLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Platform\Society;
  8. use App\Logging\Tools;
  9. use App\Services\LogTools;
  10. class SocietyLog
  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(Society $society)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData $this->logTools->handleLoggingData($society);
  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($society$loggingData);
  33.         //----------------------------------------------------------------------
  34.         $action "society_add";
  35.         $args["action"] = $action;
  36.         $pendingLogArgs[] = $args;
  37.         return $pendingLogArgs;
  38.     }
  39.     public function logChanges(Society $society$changes)
  40.     {
  41.         $pendingLogArgs = [];
  42.         //----------------------------------------------------------------------
  43.         // Fetch eventual info societyd in the object
  44.         $loggingData $this->logTools->handleLoggingData($society);
  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($society$loggingData);
  53.         //----------------------------------------------------------------------
  54.         $action "society_edit";
  55.         // This is triggered when we are actually changing the connected Address object
  56.         // The changes in the actual object are handled in AddressLog
  57.         $addressChanges = array(
  58.             "address",
  59.         );
  60.         $basicChanges = array(
  61.             "ref",
  62.             "name",
  63.             "email",
  64.             "contactEmail",
  65.             "noreplyEmail",
  66.             "phoneNumber",
  67.             "siren",
  68.             "defaultSociety",
  69.         );
  70.         $dateChanges = array(
  71.         );
  72.         $labelChanges = array(
  73.         );
  74.         // Method getLogLabel() should be defined for these objects/entities
  75.         $objectChanges = array(
  76.             "defaultManager",
  77.             "defaultEmitter",
  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$dateChanges))
  102.             {
  103.                 // dateChanges can be null
  104.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  105.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  106.                 continue;
  107.             }
  108.             if (in_array($key$labelChanges))
  109.             {
  110.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  111.                 continue;
  112.             }
  113.             if (in_array($key$objectChanges))
  114.             {
  115.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  116.                 continue;
  117.             }
  118.         }
  119.         // Handle ManyToMany
  120.         if ($society->getStores()->isDirty())
  121.         {
  122.             $oldData "";
  123.             $newData "";
  124.             foreach ($society->getStores()->getSnapshot() as $store)
  125.             {
  126.                 $oldData .= $store->display();
  127.                 $oldData .= ", ";
  128.             }
  129.             foreach ($society->getStores() as $store)
  130.             {
  131.                 $newData .= $store->display();
  132.                 $newData .= ", ";
  133.             }
  134.             if ($oldData != "")
  135.             {
  136.                 $oldData substr($oldData0, -1);
  137.                 $oldData substr($oldData0, -1);
  138.             }
  139.             if ($newData != "")
  140.             {
  141.                 $newData substr($newData0, -1);
  142.                 $newData substr($newData0, -1);
  143.             }
  144.             $args["action"] = "society_edit_stores";
  145.             $args["old_value"] = $oldData;
  146.             $args["new_value"] = $newData;
  147.             $pendingLogArgs[] = $args;
  148.         }
  149.         return $pendingLogArgs;
  150.     }
  151.     public function logRemoval(Society $society)
  152.     {
  153.         $pendingLogArgs = [];
  154.         return $pendingLogArgs;
  155.     }
  156.     private function initArgs(Society $society$loggingData)
  157.     {
  158.         $societyGroup $society->getSocietyGroup();
  159.         $args = array(
  160.             "object_id"                    =>    $society->getId(),
  161.             "object_bundle"                =>    "Platform",
  162.             "object_entity"                =>    "Society",
  163.             "object_display"            =>    $society->display(),
  164.             "society_group"                =>    $societyGroup,
  165.             "society"                    =>    $society,
  166.             "info"                        =>    $loggingData['info'],
  167.             "special_author"            =>    $loggingData['special_author'],
  168.         );
  169.         return $args;
  170.     }
  171. }