src/Logging/Activity/InstallmentLog.php line 24

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/InstallmentLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Access;
  8. use App\Entity\Platform\Installment;
  9. use App\Logging\DeletionContextLogger;
  10. use App\Logging\Tools;
  11. use App\Services\LogTools;
  12. class InstallmentLog
  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(Installment $installment)
  23.     {
  24.         $pendingLogArgs = [];
  25.         //----------------------------------------------------------------------
  26.         // Fetch eventual info stored in the object
  27.         $loggingData $this->logTools->handleLoggingData($installment);
  28.         $info $loggingData['info'];
  29.         $specialAuthor $loggingData['special_author'];
  30.         $ignore $loggingData['ignore'];
  31.         if ($ignore) return $pendingLogArgs;
  32.         //----------------------------------------------------------------------
  33.         if ($installment->getDevis() !== null)
  34.         {
  35.             $module "devis";
  36.             $action "devis_edit_installment";
  37.             $object $installment->getDevis();
  38.             $objectClient $installment->getDevis()->getReceiver();
  39.             $objectMission $installment->getDevis()->getMission();
  40.             $objectEntity "Devis";
  41.         }
  42.         else
  43.         {
  44.             // Plan.io Task #4326 : Exclude Drafts
  45.             if ($installment->getInvoice() !== null)
  46.             {
  47.                 if ($installment->getInvoice()->isDraft())
  48.                 {
  49.                     // Nothing to do here
  50.                     return $pendingLogArgs;
  51.                 }
  52.                 $module "invoice";
  53.                 $action "invoice_edit_installment";
  54.                 $object $installment->getInvoice();
  55.                 $objectClient $installment->getInvoice()->getReceiver();
  56.                 $objectMission $installment->getInvoice()->getMission();
  57.                 $objectEntity "Invoice";
  58.             }
  59.             else
  60.             {
  61.                 if ($installment->getCommand() !== null)
  62.                 {
  63.                     $module "command";
  64.                     $action "command_edit_installment";
  65.                     $object $installment->getCommand();
  66.                     $objectClient $installment->getCommand()->getReceiver();
  67.                     $objectMission null;
  68.                     $objectEntity "Command";
  69.                 }
  70.                 else
  71.                 {
  72.                     // Nothing to do here
  73.                     return $pendingLogArgs;
  74.                 }
  75.             }
  76.         }
  77.         // Fill the common arguments
  78.         $args = array(
  79.             "action"                    =>    $action,
  80.             "object_id"                    =>    $object->getId(),
  81.             "object_bundle"                =>    "Platform",
  82.             "object_entity"                =>    $objectEntity,
  83.             "object_display"            =>    $object->getRef(),
  84.             "object_client_id"            =>    $objectClient !== null $objectClient->getId() : null,
  85.             "object_client_display"        =>    $objectClient !== null $objectClient->getName() : null,
  86.             "object_mission_id"            =>    $objectMission !== null $objectMission->getId() : null,
  87.             "object_mission_display"    =>    $objectMission !== null $objectMission->getRef() : null,
  88.             "info"                        =>    $info,
  89.             "society_group"                =>    $object->getSociety()->getGroup(),
  90.             "society"                    =>    $object->getSociety(),
  91.             "special_author"            =>    $specialAuthor,
  92.         );
  93.         $args["action"]        = $action."_add";
  94.         $args["new_value"]    = $installment->getValue();
  95.         $pendingLogArgs[] = $args;
  96.         return $pendingLogArgs;
  97.     }
  98.     public function logChanges(Installment $installment$changes)
  99.     {
  100.         $pendingLogArgs = [];
  101.         //----------------------------------------------------------------------
  102.         // Fetch eventual info stored in the object
  103.         $loggingData $this->logTools->handleLoggingData($installment);
  104.         $info $loggingData['info'];
  105.         $specialAuthor $loggingData['special_author'];
  106.         $ignore $loggingData['ignore'];
  107.         if ($ignore) return $pendingLogArgs;
  108.         //----------------------------------------------------------------------
  109.         if ($installment->getDevis() !== null)
  110.         {
  111.             $module "devis";
  112.             $action "devis_edit_installment";
  113.             $object $installment->getDevis();
  114.             $objectClient $installment->getDevis()->getReceiver();
  115.             $objectMission $installment->getDevis()->getMission();
  116.             $objectEntity "Devis";
  117.         }
  118.         else
  119.         {
  120.             if ($installment->getInvoice() !== null)
  121.             {
  122.                 if ($installment->getInvoice()->isDraft())
  123.                 {
  124.                     // Nothing to do here
  125.                     return $pendingLogArgs;
  126.                 }
  127.                 $module "invoice";
  128.                 $action "invoice_edit_installment";
  129.                 $object $installment->getInvoice();
  130.                 $objectClient $installment->getInvoice()->getReceiver();
  131.                 $objectMission $installment->getInvoice()->getMission();
  132.                 $objectEntity "Invoice";
  133.             }
  134.             else
  135.             {
  136.                 if ($installment->getCommand() !== null)
  137.                 {
  138.                     $module "command";
  139.                     $action "command_edit_installment";
  140.                     $object $installment->getCommand();
  141.                     $objectClient $installment->getCommand()->getReceiver();
  142.                     $objectMission null;
  143.                     $objectEntity "Command";
  144.                 }
  145.                 else
  146.                 {
  147.                     // Nothing to do here
  148.                     return $pendingLogArgs;
  149.                 }
  150.             }
  151.         }
  152.         // Fill the common arguments
  153.         $args = array(
  154.             "action"                    =>    $action,
  155.             "object_id"                    =>    $object->getId(),
  156.             "object_bundle"                =>    "Platform",
  157.             "object_entity"                =>    $objectEntity,
  158.             "object_display"            =>    $object->getRef(),
  159.             "object_client_id"            =>    $objectClient !== null $objectClient->getId() : null,
  160.             "object_client_display"        =>    $objectClient !== null $objectClient->getName() : null,
  161.             "object_mission_id"            =>    $objectMission !== null $objectMission->getId() : null,
  162.             "object_mission_display"    =>    $objectMission !== null $objectMission->getRef() : null,
  163.             "info"                        =>    $info,
  164.             "society_group"                =>    $object->getSociety()->getGroup(),
  165.             "society"                    =>    $object->getSociety(),
  166.             "special_author"            =>    $specialAuthor,
  167.         );
  168.         $basicChanges = array(
  169.             "value",
  170.             "info"
  171.         );
  172.         $labelChanges = array(
  173.             "status",
  174.             "paymentType",
  175.         );
  176.         $dateChanges = array(
  177.             "paymentDate",
  178.             "deadlineDate",
  179.         );
  180.         // See what changed and log (members first)
  181.         foreach ($changes as $key => $change)
  182.         {
  183.             $name $this->logTools->camelToSnakeCase($key);
  184.             $args["action"]    = $action."_".$name;
  185.             $before $change[0];
  186.             $after $change[1];
  187.             if (in_array($key$basicChanges))
  188.             {
  189.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  190.                 continue;
  191.             }
  192.             if (in_array($key$labelChanges))
  193.             {
  194.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  195.                 continue;
  196.             }
  197.             if (in_array($key$dateChanges))
  198.             {
  199.                 // dateChanges can be null
  200.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  201.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  202.                 continue;
  203.             }
  204.         }
  205.         return $pendingLogArgs;
  206.     }
  207.     public function logRemoval(Installment $installment)
  208.     {
  209.         $pendingLogArgs = [];
  210.         //----------------------------------------------------------------------
  211.         // Fetch eventual info stored in the object
  212.         $loggingData $this->logTools->handleLoggingData($installment);
  213.         $info $loggingData['info'];
  214.         $specialAuthor $loggingData['special_author'];
  215.         $ignore $loggingData['ignore'];
  216.         if ($ignore) return $pendingLogArgs;
  217.         //----------------------------------------------------------------------
  218.         // So this is kinda interesting
  219.         // I don't want to log the removal of installments when validation of a draft
  220.         // But when a draft is validated and its installments deleted
  221.         // no data is saved to the context for those drafts
  222.         // So this method will simply fail grafully :)
  223.         $cachedData $this->contextLogger->getContext($installment);
  224.         if ($cachedData === null || empty($cachedData))
  225.         {
  226.             // Nothing to do here
  227.             return $pendingLogArgs;
  228.         }
  229.         if (array_key_exists('devis'$cachedData) && $cachedData['devis'] !== null)
  230.         {
  231.             $module "devis";
  232.             $action "devis_edit_installment";
  233.             $object $cachedData['devis'];
  234.             $objectClient $object->getReceiver();
  235.             $objectMission $object->getMission();
  236.             $objectEntity "Devis";
  237.         }
  238.         else
  239.         {
  240.             if (array_key_exists('invoice'$cachedData) && $cachedData['invoice'] !== null)
  241.             {
  242.                 $module "invoice";
  243.                 $action "invoice_edit_installment";
  244.                 $object $cachedData['invoice'];
  245.                 $objectClient $cachedData['invoice']->getReceiver();
  246.                 $objectMission $object->getMission();
  247.                 $objectEntity "Invoice";
  248.             }
  249.             else
  250.             {
  251.                 if (array_key_exists('command'$cachedData) && $cachedData['command'] !== null)
  252.                 {
  253.                     $module "command";
  254.                     $action "command_edit_installment";
  255.                     $object $cachedData['command'];
  256.                     $objectClient $object->getReceiver();
  257.                     $objectMission null;
  258.                     $objectEntity "Command";
  259.                 }
  260.                 else
  261.                 {
  262.                     // Nothing to do here
  263.                     return $pendingLogArgs;
  264.                 }
  265.             }
  266.         }
  267.         // Fill the common arguments
  268.         $args = array(
  269.             "action"                    =>    $action,
  270.             "object_id"                    =>    $object->getId(),
  271.             "object_bundle"                =>    "Platform",
  272.             "object_entity"                =>    $objectEntity,
  273.             "object_display"            =>    $object->getRef(),
  274.             "object_client_id"            =>    $objectClient !== null $objectClient->getId() : null,
  275.             "object_client_display"        =>    $objectClient !== null $objectClient->getName() : null,
  276.             "object_mission_id"            =>    $objectMission !== null $objectMission->getId() : null,
  277.             "object_mission_display"    =>    $objectMission !== null $objectMission->getRef() : null,
  278.             "info"                        =>    $info,
  279.             "society_group"                =>    $object->getSociety()->getGroup(),
  280.             "society"                    =>    $object->getSociety(),
  281.             "special_author"            =>    $specialAuthor,
  282.         );
  283.         $args["action"]        = $action."_delete";
  284.         $args["old_value"]    = $installment->getValue();
  285.         $pendingLogArgs[] = $args;
  286.         return $pendingLogArgs;
  287.     }
  288. }