src/Logging/Activity/PhoneLog.php line 23

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/PhoneLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Access;
  8. use App\Entity\Platform\Phone;
  9. use App\Logging\DeletionContextLogger;
  10. use App\Logging\Tools;
  11. use App\Services\LogTools;
  12. class PhoneLog
  13. {
  14.     private array $pendingLogArgs = [];
  15.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsDeletionContextLogger $contextLoggerTools $tools)
  16.     {
  17.         $this->em $doctrine->getManager();
  18.         $this->logTools $logTools;
  19.         $this->contextLogger $contextLogger;
  20.         $this->tools $tools;
  21.     }
  22.     public function logCreation(Phone $phone)
  23.     {
  24.         $pendingLogArgs = [];
  25.         //----------------------------------------------------------------------
  26.         // Fetch eventual info stored in the object
  27.         $loggingData $this->logTools->handleLoggingData($phone);
  28.         $info $loggingData['info'];
  29.         $specialAuthor $loggingData['special_author'];
  30.         $ignore $loggingData['ignore'];
  31.         if ($ignore) return $pendingLogArgs;
  32.         //----------------------------------------------------------------------
  33.         $individual $phone->getIndividual();
  34.         if ($individual === null)
  35.         {
  36.             return $pendingLogArgs;
  37.         }
  38.         if ($individual->getClient() === null)
  39.         {
  40.             return $pendingLogArgs;
  41.         }
  42.         if (empty($individual->getClient()->getId()))
  43.         {
  44.             // Avoid logging phone creation when creating an Individual
  45.             return $pendingLogArgs;
  46.         }
  47.         $society $individual->getSociety();
  48.         if ($society === null)
  49.         {
  50.             return $pendingLogArgs;
  51.         }
  52.         $action "individual_add_phone";
  53.         $objectEntity "Individual";
  54.         $args = array(
  55.             "action"                    =>    $action,
  56.             "object_id"                    =>    $individual->getId(),
  57.             "object_bundle"                =>    "Platform",
  58.             "object_entity"                =>    $objectEntity,
  59.             "object_display"            =>    $individual->display(),
  60.             "object_client_id"            =>    $individual->getClient()->getId(),
  61.             "object_client_display"        =>    $individual->getClient()->getName(),
  62.             "info"                        =>    $info,
  63.             "society_group"                =>    $individual->getSociety()->getGroup(),
  64.             "society"                    =>    $individual->getSociety(),
  65.             "new_value"                    =>    $phone->displayWithLabel(),
  66.             "special_author"            =>    $specialAuthor,
  67.         );
  68.         $pendingLogArgs[] = $args;
  69.         return $pendingLogArgs;
  70.     }
  71.     public function logChanges(Phone $phone$changes)
  72.     {
  73.         $pendingLogArgs = [];
  74.         //----------------------------------------------------------------------
  75.         // Fetch eventual info stored in the object
  76.         $loggingData $this->logTools->handleLoggingData($phone);
  77.         $info $loggingData['info'];
  78.         $specialAuthor $loggingData['special_author'];
  79.         $ignore $loggingData['ignore'];
  80.         if ($ignore) return $pendingLogArgs;
  81.         //----------------------------------------------------------------------
  82.         $individual $phone->getIndividual();
  83.         if ($individual === null)
  84.         {
  85.             return $pendingLogArgs;
  86.         }
  87.         if ($individual->getClient() === null)
  88.         {
  89.             return $pendingLogArgs;
  90.         }
  91.         if (empty($individual->getClient()->getId()))
  92.         {
  93.             // Avoid logging phone creation when creating an Individual
  94.             return $pendingLogArgs;
  95.         }
  96.         $action "individual_edit_phone";
  97.         $objectEntity "Individual";
  98.         // Fill the common arguments
  99.         $args = array(
  100.             "action"                    =>    $action,
  101.             "object_id"                    =>    $individual->getId(),
  102.             "object_bundle"                =>    "Platform",
  103.             "object_entity"                =>    $objectEntity,
  104.             "object_display"            =>    $individual->display(),
  105.             "object_client_id"            =>    $individual->getClient()->getId(),
  106.             "object_client_display"        =>    $individual->getClient()->getName(),
  107.             "info"                        =>    $info,
  108.             "society_group"                =>    $individual->getSociety()->getGroup(),
  109.             "society"                    =>    $individual->getSociety(),
  110.             "special_author"            =>    $specialAuthor,
  111.         );
  112.         $basicChanges = array(
  113.             "number",
  114.         );
  115.         $labelChanges = array(
  116.             "label",
  117.         );
  118.         // See what changed and log (members first)
  119.         foreach ($changes as $key => $change)
  120.         {
  121.             $name $this->logTools->camelToSnakeCase($key);
  122.             $args["action"]        = $action."_".$name;
  123.             $before $change[0];
  124.             $after $change[1];
  125.             if (in_array($key$basicChanges))
  126.             {
  127.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  128.                 continue;
  129.             }
  130.             if (in_array($key$labelChanges))
  131.             {
  132.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  133.                 continue;
  134.             }
  135.         }
  136.         return $pendingLogArgs;
  137.     }
  138.     public function logRemoval(Phone $phone)
  139.     {
  140.         $pendingLogArgs = [];
  141.         //----------------------------------------------------------------------
  142.         // Fetch eventual info stored in the object
  143.         $loggingData $this->logTools->handleLoggingData($phone);
  144.         $info $loggingData['info'];
  145.         $specialAuthor $loggingData['special_author'];
  146.         $ignore $loggingData['ignore'];
  147.         if ($ignore) return $pendingLogArgs;
  148.         //----------------------------------------------------------------------
  149.         $cachedData $this->contextLogger->getContext($phone);
  150.         if ($cachedData === null || empty($cachedData))
  151.         {
  152.             // Nothing to do here
  153.             return $pendingLogArgs;
  154.         }
  155.         if (!array_key_exists('missions'$cachedData) && !array_key_exists('individual'$cachedData))
  156.         {
  157.             // Nothing to do here
  158.             return $pendingLogArgs;
  159.         }
  160.         $individual $cachedData['individual'];
  161.         if ($individual === null)
  162.         {
  163.             return $pendingLogArgs;
  164.         }
  165.         $action "individual_delete_phone";
  166.         $objectEntity "Individual";
  167.         // Fill the common arguments
  168.         $args = array(
  169.             "action"                    =>    $action,
  170.             "object_id"                    =>    $individual->getId(),
  171.             "object_bundle"                =>    "Platform",
  172.             "object_entity"                =>    $objectEntity,
  173.             "object_display"            =>    $individual->display(),
  174.             "object_client_id"            =>    $individual->getClient()->getId(),
  175.             "object_client_display"        =>    $individual->getClient()->getName(),
  176.             "info"                        =>    $info,
  177.             "society_group"                =>    $individual->getSociety()->getGroup(),
  178.             "society"                    =>    $individual->getSociety(),
  179.             "old_value"                    =>    $phone->displayWithLabel(),
  180.             "special_author"            =>    $specialAuthor,
  181.         );
  182.         $pendingLogArgs[] = $args;
  183.         // Next => Missions
  184.         // Log for $mission.societyGroupAuthor only
  185.         $action "mission_delete_phone_from_individual";
  186.         $objectEntity "Mission";
  187.         foreach ($cachedData['missions'] as $mission)
  188.         {
  189.             $args = array(
  190.                 "action"                    =>    $action,
  191.                 "object_id"                    =>    $mission->getId(),
  192.                 "object_bundle"                =>    "Mission",
  193.                 "object_entity"                =>    $objectEntity,
  194.                 "object_display"            =>    $mission->display(),
  195.                 "object_client_id"            =>    $mission->getReceiver()->getId(),
  196.                 "object_client_display"        =>    $mission->getReceiver()->getName(),
  197.                 "info"                        =>    $info,
  198.                 "society_group"                =>    $mission->getSocietyGroupAuthor(),
  199.                 "society"                    =>    $mission->getSociety(),
  200.                 "old_value"                    =>    $phone->displayWithLabel(),
  201.                 "special_author"            =>    $specialAuthor,
  202.             );
  203.             $pendingLogArgs[] = $args;
  204.         }
  205.         return $pendingLogArgs;
  206.     }
  207. }