<?php
namespace App\Manager;
use App\Diffusion\NotificationsDiffusion;
use App\Entity\ListeDiffusion;
use App\Entity\NotificationCategory;
use App\Entity\Notifications\Notification;
use App\Repository\CategoryDiffusionRepository;
use App\Repository\ListeDiffusionRepository;
use App\Repository\Notifications\NotificationRepository;
use Doctrine\ORM\EntityManagerInterface;
class NotificationManager
{
public function __construct(
private readonly ListeDiffusionRepository $listeDiffusionRepository,
private readonly CategoryDiffusionRepository $categoryDiffusionRepository,
private readonly EntityManagerInterface $entityManager,
private readonly NotificationRepository $notificationRepository
)
{
}
// Déclenche une notification si besoin
public function popNotification(
$code_adherent = null, // Code adhérent
$lang, // Langue de l'adhérent
$categoryNotif, // Catégorie de la notification
$notifIdentifier, // Identifiant unique de la notification
$notifTitle, // Titre de la notification
$urlRedirect, // Url de redirection de la notification
$text, // Texte
$thisAdhOnly = false, // Notification propre à l'adhérent (aucune liste de diffusion),
$listesDiffusions = [] // Liste des listes de diffusions à intégrer
): void
{
if (!$thisAdhOnly && empty($listesDiffusions)) {
$listesDiffusions[0] = $this->listeDiffusionRepository->findOneBy(['name' => 'Diffusion : ' . $notifIdentifier]);
if (is_null($listesDiffusions[0])) {
$listesDiffusions[0] = new ListeDiffusion();
$categoryDiffusion = $this->categoryDiffusionRepository->findOneBy(['name' => 'Notification']);
$listesDiffusions[0]->setCategoryDiffusion($categoryDiffusion);
$listesDiffusions[0]->setType('DIFF');
$listesDiffusions[0]->setName('Diffusion : ' . $notifIdentifier);
$listesDiffusions[0]->setAdherents([$code_adherent]);
$listesDiffusions[0]->setReadOnly(true);
} elseif (!in_array($code_adherent, $listesDiffusions[0]->getAdherents())) {
$adhList = $listesDiffusions[0]->getAdherents();
$adhList[] = $code_adherent;
$listesDiffusions[0]->setAdherents($adhList);
$listesDiffusions[0]->setUpdateAt(new \DateTimeImmutable('now'));
}
$this->entityManager->persist($listesDiffusions[0]);
$this->entityManager->flush();
$newNotif = $this->notificationRepository->createQueryBuilder('n')
->join('n.category', 'categ', 'n.category = categ.id')
->where('categ.name = :categ')
->andWhere('n.langue = :lang')
->andWhere('n.identifier = :identifier')
->setParameters(['categ' => $categoryNotif, 'lang' => $lang, 'identifier' => $notifIdentifier])
->getQuery()->getOneOrNullResult();
} else {
$newNotif = null;
}
if (is_null($newNotif)) {
$newNotif = new Notification();
$categoryNotif = $this->entityManager->getRepository(NotificationCategory::class)->findOneBy(['name' => $categoryNotif]);
$newNotif->setCategory($categoryNotif);
$newNotif->setTitle($notifTitle);
$newNotif->setContent($text);
$newNotif->setLangue($lang);
$newNotif->setPopAt(new \DateTimeImmutable('now'));
$newNotif->setUrl($urlRedirect);
$newNotif->setIdentifier($notifIdentifier);
if (!$thisAdhOnly) {
foreach ($listesDiffusions as $listesDiff) {
$newNotif->addListeDiffusions($listesDiff);
}
} else {
$newNotif->setAdherent($code_adherent);
}
$this->entityManager->persist($newNotif);
$this->entityManager->flush();
}
}
public function readNotifsByCategory(NotificationsDiffusion $notificationDiffusion, $category, $kAdherent): void
{
$notifsToRead = $notificationDiffusion->getDatasByEntities(Notification::class, ['allNotRead' => true, 'category' => $category]);
$updated = false;
/** @var Notification $notif */
foreach ($notifsToRead as $notif) {
$readedArray = json_decode($notif->getReaded(), true) ?? [];
if (!in_array($kAdherent, $readedArray)) {
$readedArray[] = $kAdherent;
$notif->setReaded(json_encode($readedArray));
$updated = true;
}
}
if ($updated) {
$this->entityManager->flush();
}
}
}