src/Services/Ding/AdvancedNotificationTools.php line 30

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Services/Ding/AdvancedNotificationTools.php
  4. //----------------------------------------------------------------------
  5. namespace App\Services\Ding;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Component\Security\Core\Security;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use App\Entity\SocietyGroup;
  10. use App\Entity\Ding\AdvancedNotification;
  11. use App\Entity\Ding\AdvancedNotificationConfig;
  12. use App\Entity\Planning\Task;
  13. use App\Entity\Planning\TaskStatus;
  14. use App\Entity\Planning\TaskType;
  15. use App\Entity\Webapp\Anomaly;
  16. use App\Entity\Webapp\AnomalyGC;
  17. use App\Entity\Webapp\Document;
  18. use App\Entity\Webapp\Rfi;
  19. use App\Entity\Webapp\RfiGC;
  20. use App\Logging\DeletionContextLogger;
  21. use App\Services\LogTools;
  22. class AdvancedNotificationTools
  23. {
  24.     public function __construct(ManagerRegistry $doctrineSecurity $securityLogTools $logToolsDeletionContextLogger $contextLogger)
  25.     {
  26.         $this->em $doctrine->getManager();
  27.         $this->logTools $logTools;
  28.         $this->security $security;
  29.         $this->contextLogger $contextLogger;
  30.     }
  31.     public function createAdvancedNotificationForDocument(Document $document)
  32.     {
  33.         if (!$this->security->isGranted('advanced_notification_is_active'))
  34.         {
  35.             return null;
  36.         }
  37.         // Nothing to test about the Document
  38.         // Just create the notification
  39.         $ding = new AdvancedNotification();
  40.         $ding->setSocietyGroup($document->getSocietyGroup());
  41.         $ding->setSociety($document->getSociety());
  42.         $ding->setDocument($document);
  43.         if ($document->getChild() instanceof Anomaly)
  44.             $ding->setAnomalyCode();
  45.         elseif ($document->getChild() instanceof AnomalyGC)
  46.             $ding->setAnomalyGCCode();
  47.         elseif ($document->getChild() instanceof Rfi)
  48.             $ding->setRfiCode();
  49.         elseif ($document->getChild() instanceof RfiGC)
  50.             $ding->setRfiGCCode();
  51.         else
  52.         {
  53.             // This should not happen
  54.             $this->logTools->errorlog("Wrong kind of Document. Not an Anomaly / AnomalyGC / Rfi / RfiGC ".$document->displayForLog());
  55.             return null;
  56.         }
  57.         $this->em->persist($ding);
  58.         return $ding;
  59.     }
  60.     // TODO : Try to find a better approach
  61.     // $skipVoter : Used to skip the voter in command context (No user = return false)
  62.     // $skipVoter is true ONLY in InterventionTools :: updateTasksRekto (Context Rekto Command ListingIntervention)
  63.     // Task must meet the following criteria at the same time
  64.     //        - Task::$type should be checked
  65.     //        - Task::$status should be checked
  66.     //        - No RFI / RFIGC
  67.     public function taskNeedsNotification(Task $task$skipVoter false)
  68.     {
  69.         if ($skipVoter == false)
  70.         {
  71.             if (!$this->security->isGranted('advanced_notification_is_active'))
  72.             {
  73.                 return null;
  74.             }
  75.         }
  76.         // Exit if the task is in future
  77.         if ($task->isFuture())
  78.         {
  79.             return false;
  80.         }
  81.         // Get config
  82.         $configs $this->em->getRepository(AdvancedNotificationConfig::class)
  83.             ->findBySocietyGroup($task->getSocietyGroup());
  84.         // Get concerned Task Statuses and concerned Task Types
  85.         $taskStatuses = new ArrayCollection();
  86.         $taskTypes = new ArrayCollection();
  87.         foreach ($configs as $config)
  88.         {
  89.             if ($config->getTaskStatus() !== null)
  90.             {
  91.                 if (!$taskStatuses->contains($config->getTaskStatus()))
  92.                 {
  93.                     if ($config->getIsActive())
  94.                     {
  95.                         $taskStatuses[] = $config->getTaskStatus();
  96.                     }
  97.                 }
  98.             }
  99.             if ($config->getTaskType() !== null)
  100.             {
  101.                 if (!$taskTypes->contains($config->getTaskType()))
  102.                 {
  103.                     if ($config->getIsActive())
  104.                     {
  105.                         $taskTypes[] = $config->getTaskType();
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.         // We need at least one status and one type
  111.         if (empty($taskStatuses) || empty($taskTypes))
  112.         {
  113.             return false;
  114.         }
  115.         // Does the Task fit the status / type criteria ?
  116.         if (!$taskStatuses->contains($task->getStatus()))
  117.         {
  118.             return false;
  119.         }
  120.         if (!$taskTypes->contains($task->getType()))
  121.         {
  122.             return false;
  123.         }
  124.         // Does the Task have an Rfi / RfiGC
  125.         // Check also imported Rfi / RfiGC
  126.         foreach ($task->getDocuments() as $document)
  127.         {
  128.             if ($document->isRfi() || $document->isRfiGC() || $document->isArchivedRfi() || $document->isArchivedRfiGC())
  129.             {
  130.                 // At least one found => Nothing to do here
  131.                 return false;
  132.             }
  133.         }
  134.         return true;
  135.     }
  136.     // TODO : Try to find a better approach
  137.     // $skipVoter : Used to skip the voter in command context (No user = return false)
  138.     // $skipVoter is true ONLY in InterventionTools :: updateTasksRekto (Context Rekto Command ListingIntervention)
  139.     public function updateAdvancedNotificationForTask(Task $task$skipVoter false)
  140.     {
  141.         if ($skipVoter == false)
  142.         {
  143.             if (!$this->security->isGranted('advanced_notification_is_active'))
  144.             {
  145.                 return null;
  146.             }
  147.         }
  148.         if ($task->isAbsence())
  149.         {
  150.             return null;
  151.         }
  152.         // Get existing Notifications if any
  153.         // Actually there should maximum one or none
  154.         $dings $this->em->getRepository(AdvancedNotification::class)
  155.             ->findByTask($task);
  156.         if (count($dings) > 1)
  157.         {
  158.             // This should not happen
  159.             $this->logTools->errorlog("Several AdvancedNotifications were found for Task ".$task->displayForLog());
  160.         }
  161.         $ding null;
  162.         if (count($dings) >= 1)
  163.         {
  164.             $ding $dings[0];
  165.         }
  166.         // Do we need a Notification ?
  167.         if ($this->taskNeedsNotification($task$skipVoter))
  168.         {
  169.             // Yes, we need a notification for this task
  170.             if ($ding !== null)
  171.             {
  172.                 // But one already exists => All good
  173.                 return $ding;
  174.             }
  175.             // If we are here it means that we need a notification
  176.             // and none has been found
  177.             // So create it
  178.             $ding = new AdvancedNotification();
  179.             $ding->setSocietyGroup($task->getSocietyGroup());
  180.             $ding->setSociety($task->getSociety());
  181.             $ding->setTask($task);
  182.             $ding->setTaskCode();
  183.             $this->em->persist($ding);
  184.             return $ding;
  185.         }
  186.         // If we are here it means the task does not need a notification
  187.         if ($ding !== null)
  188.         {
  189.             // Logging
  190.             // Plan.io Task #3922
  191.             $ding->setLoggingData([
  192.                 "info" => "rfi_auto",
  193.             ]);
  194.             $this->contextLogger->storeContext($ding, [
  195.                 'societyGroup' => $ding->getSocietyGroup(),
  196.                 'society' => $ding->getSociety(),
  197.                 'receiver' => $ding->getReceiver(),
  198.             ]);
  199.             // But one exists, so remove it
  200.             $this->em->remove($ding);
  201.             return null;
  202.         }
  203.         // If we are here it means the task does not need a notification
  204.         // And none has been found
  205.         // => All good
  206.         return null;
  207.     }
  208.     // Check that we have one config for each status / type
  209.     public function updateConfig(SocietyGroup $societyGroup)
  210.     {
  211.         $flush false;
  212.         $advancedNotificationConfigRep $this->em->getRepository(AdvancedNotificationConfig::class);
  213.         $taskStatusRepository $this->em->getRepository(TaskStatus::class);
  214.         $taskTypeRepository $this->em->getRepository(TaskType::class);
  215.         $taskStatuses $taskStatusRepository->getForITaskConfig($societyGroup);
  216.         $taskTypes $taskTypeRepository->getForITaskConfig($societyGroup);
  217.         // Check that we have one config for each status / type
  218.         $configs $advancedNotificationConfigRep->findBy(
  219.             array('societyGroup'    =>    $societyGroup)
  220.         );
  221.         // TaskStatus
  222.         foreach ($taskStatuses as $taskStatus)
  223.         {
  224.             // ITaskAuto
  225.             $found false;
  226.             foreach ($configs as $config)
  227.             {
  228.                 if ($config->getTaskStatus() === null)
  229.                 {
  230.                     continue;
  231.                 }
  232.                 if ($config->getTaskStatus()->equals($taskStatus))
  233.                 {
  234.                     $found true;
  235.                     break;
  236.                 }
  237.             }
  238.             if (!$found)
  239.             {
  240.                 $newConfig = new AdvancedNotificationConfig();
  241.                 $newConfig->setSocietyGroup($societyGroup);
  242.                 $newConfig->setTaskStatus($taskStatus);
  243.                 $this->em->persist($newConfig);
  244.                 $flush true;
  245.             }
  246.         }
  247.         // TaskType
  248.         foreach ($taskTypes as $taskType)
  249.         {
  250.             // ITaskAuto
  251.             $found false;
  252.             foreach ($configs as $config)
  253.             {
  254.                 if ($config->getTaskType() === null)
  255.                 {
  256.                     continue;
  257.                 }
  258.                 if ($config->getTaskType()->equals($taskType))
  259.                 {
  260.                     $found true;
  261.                     break;
  262.                 }
  263.             }
  264.             if (!$found)
  265.             {
  266.                 $newConfig = new AdvancedNotificationConfig();
  267.                 $newConfig->setSocietyGroup($societyGroup);
  268.                 $newConfig->setTaskType($taskType);
  269.                 $this->em->persist($newConfig);
  270.                 $flush true;
  271.             }
  272.         }
  273.         return $flush;
  274.     }
  275. }