src/Manager/NotificationManager.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Manager;
  3. use App\Diffusion\NotificationsDiffusion;
  4. use App\Entity\ListeDiffusion;
  5. use App\Entity\NotificationCategory;
  6. use App\Entity\Notifications\Notification;
  7. use App\Repository\CategoryDiffusionRepository;
  8. use App\Repository\ListeDiffusionRepository;
  9. use App\Repository\Notifications\NotificationRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. class NotificationManager
  12. {
  13.     public function __construct(
  14.         private readonly ListeDiffusionRepository    $listeDiffusionRepository,
  15.         private readonly CategoryDiffusionRepository $categoryDiffusionRepository,
  16.         private readonly EntityManagerInterface      $entityManager,
  17.         private readonly NotificationRepository      $notificationRepository
  18.     )
  19.     {
  20.     }
  21.     // Déclenche une notification si besoin
  22.     public function popNotification(
  23.         $code_adherent null// Code adhérent
  24.         $lang// Langue de l'adhérent
  25.         $categoryNotif// Catégorie de la notification
  26.         $notifIdentifier// Identifiant unique de la notification
  27.         $notifTitle// Titre de la notification
  28.         $urlRedirect// Url de redirection de la notification
  29.         $text// Texte
  30.         $thisAdhOnly false// Notification propre à l'adhérent (aucune liste de diffusion),
  31.         $listesDiffusions = [] // Liste des listes de diffusions à intégrer
  32.     ): void
  33.     {
  34.         if (!$thisAdhOnly && empty($listesDiffusions)) {
  35.             $listesDiffusions[0] = $this->listeDiffusionRepository->findOneBy(['name' => 'Diffusion : ' $notifIdentifier]);
  36.             if (is_null($listesDiffusions[0])) {
  37.                 $listesDiffusions[0] = new ListeDiffusion();
  38.                 $categoryDiffusion $this->categoryDiffusionRepository->findOneBy(['name' => 'Notification']);
  39.                 $listesDiffusions[0]->setCategoryDiffusion($categoryDiffusion);
  40.                 $listesDiffusions[0]->setType('DIFF');
  41.                 $listesDiffusions[0]->setName('Diffusion : ' $notifIdentifier);
  42.                 $listesDiffusions[0]->setAdherents([$code_adherent]);
  43.                 $listesDiffusions[0]->setReadOnly(true);
  44.             } elseif (!in_array($code_adherent$listesDiffusions[0]->getAdherents())) {
  45.                 $adhList $listesDiffusions[0]->getAdherents();
  46.                 $adhList[] = $code_adherent;
  47.                 $listesDiffusions[0]->setAdherents($adhList);
  48.                 $listesDiffusions[0]->setUpdateAt(new \DateTimeImmutable('now'));
  49.             }
  50.             $this->entityManager->persist($listesDiffusions[0]);
  51.             $this->entityManager->flush();
  52.             $newNotif $this->notificationRepository->createQueryBuilder('n')
  53.                 ->join('n.category''categ''n.category = categ.id')
  54.                 ->where('categ.name = :categ')
  55.                 ->andWhere('n.langue = :lang')
  56.                 ->andWhere('n.identifier = :identifier')
  57.                 ->setParameters(['categ' => $categoryNotif'lang' => $lang'identifier' => $notifIdentifier])
  58.                 ->getQuery()->getOneOrNullResult();
  59.         } else {
  60.             $newNotif null;
  61.         }
  62.         if (is_null($newNotif)) {
  63.             $newNotif = new Notification();
  64.             $categoryNotif $this->entityManager->getRepository(NotificationCategory::class)->findOneBy(['name' => $categoryNotif]);
  65.             $newNotif->setCategory($categoryNotif);
  66.             $newNotif->setTitle($notifTitle);
  67.             $newNotif->setContent($text);
  68.             $newNotif->setLangue($lang);
  69.             $newNotif->setPopAt(new \DateTimeImmutable('now'));
  70.             $newNotif->setUrl($urlRedirect);
  71.             $newNotif->setIdentifier($notifIdentifier);
  72.             if (!$thisAdhOnly) {
  73.                 foreach ($listesDiffusions as $listesDiff) {
  74.                     $newNotif->addListeDiffusions($listesDiff);
  75.                 }
  76.             } else {
  77.                 $newNotif->setAdherent($code_adherent);
  78.             }
  79.             $this->entityManager->persist($newNotif);
  80.             $this->entityManager->flush();
  81.         }
  82.     }
  83.     public function readNotifsByCategory(NotificationsDiffusion $notificationDiffusion$category$kAdherent): void
  84.     {
  85.         $notifsToRead $notificationDiffusion->getDatasByEntities(Notification::class, ['allNotRead' => true'category' => $category]);
  86.         $updated false;
  87.         /** @var Notification $notif */
  88.         foreach ($notifsToRead as $notif) {
  89.             $readedArray json_decode($notif->getReaded(), true) ?? [];
  90.             if (!in_array($kAdherent$readedArray)) {
  91.                 $readedArray[] = $kAdherent;
  92.                 $notif->setReaded(json_encode($readedArray));
  93.                 $updated true;
  94.             }
  95.         }
  96.         if ($updated) {
  97.             $this->entityManager->flush();
  98.         }
  99.     }
  100. }