src/Logging/Activity/DemandLog.php line 21

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/DemandLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Platform\Demand\Demand;
  8. use App\Logging\Tools;
  9. use App\Services\LogTools;
  10. class DemandLog
  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(Demand $demand)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData $this->logTools->handleLoggingData($demand);
  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($demand$loggingData);
  33.         //----------------------------------------------------------------------
  34.         $action "demand_add";
  35.         $args["action"] = $action;
  36.         $pendingLogArgs[] = $args;
  37.         return $pendingLogArgs;
  38.     }
  39.     public function logChanges(Demand $demand$changes)
  40.     {
  41.         $pendingLogArgs = [];
  42.         //----------------------------------------------------------------------
  43.         // Fetch eventual info skeletond in the object
  44.         $loggingData $this->logTools->handleLoggingData($demand);
  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($demand$loggingData);
  53.         //----------------------------------------------------------------------
  54.         $action "demand_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.         );
  59.         $basicChanges = array(
  60.             "ref",
  61.             "title",
  62.             "body",
  63.             "lastname",
  64.             "firstname",
  65.             "company",
  66.             "email",
  67.             "phone",
  68.             "address",
  69.             "complement",
  70.             "postalCode",
  71.             "town",
  72.             "country",
  73.         );
  74.         $dateChanges = array(
  75.         );
  76.         $labelChanges = array(
  77.             "status",
  78.             "type",
  79.         );
  80.         // Method getLogLabel() should be defined for these objects/entities
  81.         $objectChanges = array(
  82.             "client",
  83.             "mission",
  84.             "resultingMission",
  85.             "society",
  86.         );
  87.         // See what changed and log (members first)
  88.         foreach ($changes as $key => $change)
  89.         {
  90.             // Something to skip ?
  91.             if (false)
  92.             {
  93.                 continue;
  94.             }
  95.             $name $this->logTools->camelToSnakeCase($key);
  96.             $args["action"]    = $action."_".$name;
  97.             $before $change[0];
  98.             $after $change[1];
  99.             if (in_array($key$addressChanges))
  100.             {
  101.                 $pendingLogArgs[] = $this->tools->handleAddressChanges($args$before$after);
  102.                 continue;
  103.             }
  104.             if (in_array($key$basicChanges))
  105.             {
  106.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  107.                 continue;
  108.             }
  109.             if (in_array($key$dateChanges))
  110.             {
  111.                 // dateChanges can be null
  112.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  113.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  114.                 continue;
  115.             }
  116.             if (in_array($key$labelChanges))
  117.             {
  118.                 $args $this->tools->handleLabelChanges($args$before$after);
  119.                 $pendingLogArgs[] = $args;
  120.                 continue;
  121.             }
  122.             if (in_array($key$objectChanges))
  123.             {
  124.                 $args $this->tools->handleObjectChanges($args$before$after);
  125.                 if ($key == "mission"$args["action"] = "demand_link_to_mission";
  126.                 if ($key == "resultingMission"$args["action"] = "demand_convert_to_mission";
  127.                 $pendingLogArgs[] = $args;
  128.                 continue;
  129.             }
  130.         }
  131.         return $pendingLogArgs;
  132.     }
  133.     public function logRemoval(Demand $demand)
  134.     {
  135.         $pendingLogArgs = [];
  136.         return $pendingLogArgs;
  137.     }
  138.     private function initArgs(Demand $demand$loggingData)
  139.     {
  140.         $receiver $demand->getClient();
  141.         $society $demand->getSociety();
  142.         $societyGroup $demand->getSocietyGroup();        
  143.         $args = array(
  144.             "object_id"                    =>    $demand->getId(),
  145.             "object_bundle"                =>    "Platform",
  146.             "object_entity"                =>    "Demand",
  147.             "object_display"            =>    $demand->display(),
  148.             "object_client_id"            =>    $receiver !== null $receiver->getId() : null,
  149.             "object_client_display"        =>    $receiver !== null $receiver->getName() : null,
  150.             "society_group"                =>    $societyGroup,
  151.             "society"                    =>    $society,
  152.             "info"                        =>    $loggingData['info'],
  153.             "special_author"            =>    $loggingData['special_author'],
  154.         );
  155.         return $args;
  156.     }
  157. }