src/Repository/App/NotificationRepository.php line 151

Open in your IDE?
  1. <?php
  2. namespace App\Repository\App;
  3. use App\Entity\Notification;
  4. use App\Entity\User;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\ORM\QueryBuilder;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. /**
  9.  * @method Notification|null find($id, $lockMode = null, $lockVersion = null)
  10.  * @method Notification|null findOneBy(array $criteria, array $orderBy = null)
  11.  * @method Notification[]    findAll()
  12.  * @method Notification[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  13.  */
  14. class NotificationRepository extends ServiceEntityRepository
  15. {
  16.     public function __construct(ManagerRegistry $registry)
  17.     {
  18.         parent::__construct($registryNotification::class);
  19.     }
  20.     public function findActiveNotificationsByUser(User $userbool $unreadOnly false)
  21.     {
  22.         $qb $this->createQueryBuilder('n');
  23.         $qb $this->_joinStatisticsTable($qb);
  24.         $qb->select('n''s');
  25.         $qb $this->_filterOngoingPublishedNotificationsByUser($qb$user->getCustomerNumber());
  26.         if ($unreadOnly === true) {
  27.             $qb $this->_filterOnlyUnreadNotification($qb);
  28.         }
  29.         return $qb
  30.             ->orderBy('n.createdAt''DESC')
  31.             ->getQuery()
  32.             ->getResult();
  33.     }
  34.     private function _joinStatisticsTable(QueryBuilder $qb): QueryBuilder
  35.     {
  36.         return $qb->join('n.notificationStatistics''s');
  37.     }
  38.     private function _filterOngoingPublishedNotificationsByUser(QueryBuilder $qb$customerNumber): QueryBuilder
  39.     {
  40.         $qb $this->_filterNotificationByCustomerNumber($qb$customerNumber);
  41.         $qb $this->_filterNotificationByStartAndEndAt($qb);
  42.         $qb $this->_filterNotificationWhereUserIsActivated($qb);
  43.         $qb $this->_filterPublishedNotification($qb);
  44.         return $qb;
  45.     }
  46.     private function _filterNotificationByCustomerNumber(QueryBuilder $qb$customerNumber): QueryBuilder
  47.     {
  48.         return $qb
  49.             ->andWhere('s.customerNumber = :customer')
  50.             ->setParameter('customer'$customerNumber);
  51.     }
  52.     private function _filterNotificationByStartAndEndAt(QueryBuilder $qb): QueryBuilder
  53.     {
  54.         return $qb
  55.             ->andWhere('n.startAt <= :now')
  56.             ->andWhere('n.endAt > :now')
  57.             ->setParameter('now', new \DateTime());
  58.     }
  59.     private function _filterNotificationWhereUserIsActivated(QueryBuilder $qb): QueryBuilder
  60.     {
  61.         return $qb
  62.             ->andWhere('s.desactivatedDate is NULL')
  63.             ->setParameter('now', new \DateTime());
  64.     }
  65.     private function _filterPublishedNotification(QueryBuilder $qb): QueryBuilder
  66.     {
  67.         return $qb
  68.             ->andWhere('n.published = true');
  69.     }
  70.     private function _filterOnlyUnreadNotification(QueryBuilder $qb): QueryBuilder
  71.     {
  72.         return $qb->andWhere($qb->expr()->orX(
  73.             $qb->expr()->isNull('s.lastSeenDate'),
  74.             $qb->expr()->eq('s.readLater'1)
  75.         ));
  76.     }
  77.     public function findPopUpNotificationsByUser(User $user, array $seenPopUps)
  78.     {
  79.         $qb $this->createQueryBuilder('n');
  80.         $qb $this->_joinStatisticsTable($qb);
  81.         $qb->select('n''s');
  82.         $qb $this->_filterOngoingPublishedNotificationsByUser($qb$user->getCustomerNumber());
  83.         $qb $this->_filterNotAnsweredOnlyNotification($qb);
  84.         $qb $this->_filterPopUpNotification($qb);
  85.         $qb $this->_filterPopUpNotificationFromSeenArray($qb$seenPopUps);
  86.         $qb $this->_filterPopUpNotificationWhereCountReadIsNotReached($qb);
  87.         return $qb->getQuery()->getResult();
  88.     }
  89.     private function _filterNotAnsweredOnlyNotification(QueryBuilder $qb): QueryBuilder
  90.     {
  91.         return $qb
  92.             ->andWhere('s.answer is NULL');
  93.     }
  94.     private function _filterPopUpNotification(QueryBuilder $qb): QueryBuilder
  95.     {
  96.         return $qb
  97.             ->andWhere('n.setPopUp = true');
  98.     }
  99.     private function _filterPopUpNotificationFromSeenArray(QueryBuilder $qb, array $seenPopUps): QueryBuilder
  100.     {
  101.         // Remove null values from the array (they would trigger a doctrine error)
  102.         $filteredSeenPopUps array_filter($seenPopUps, function($value) { return $value !== null; });
  103.         // No seen popups to exclude -> return qb without adding restrictions
  104.         if (!$filteredSeenPopUps) {
  105.             return $qb;
  106.         }
  107.         return $qb->andWhere($qb->expr()->notIn('n.id'$filteredSeenPopUps));
  108.     }
  109.     private function _filterPopUpNotificationWhereCountReadIsNotReached(QueryBuilder $qb): QueryBuilder
  110.     {
  111.         return $qb
  112.             ->andWhere($qb->expr()->orX(
  113.                     $qb->expr()->eq('s.readLater'1),
  114.                     $qb->expr()->orX(
  115.                         $qb->expr()->andX(
  116.                             $qb->expr()->isNotNull('n.insistenceCoefficient'),
  117.                             $qb->expr()->gt('n.insistenceCoefficient''s.countRead')
  118.                         ),
  119.                         $qb->expr()->andX(
  120.                             $qb->expr()->isNull('n.insistenceCoefficient'),
  121.                             $qb->expr()->eq('s.countRead'0)
  122.                         ),
  123.                 )
  124.             ));
  125.     }
  126.     public function countUnreadByUser(User $user)
  127.     {
  128.         $qb $this->createQueryBuilder('n');
  129.         $qb $this->_joinStatisticsTable($qb);
  130.         $qb->select('count(s.id)');
  131.         $qb $this->_filterOngoingPublishedNotificationsByUser($qb$user->getCustomerNumber());
  132.         $qb $this->_filterOnlyUnreadNotification($qb);
  133.         return $qb
  134.             ->getQuery()
  135.             ->getSingleScalarResult();
  136.     }
  137.     /**
  138.      * @param User $user
  139.      * @param string[] $notifications Ids of the notifications to exclude from the query
  140.      * @return mixed
  141.      */
  142.     public function findUnreadByUserWithoutGivenArray(User $user, array $notifications = [])
  143.     {
  144.         $qb $this->createQueryBuilder('n');
  145.         $qb $this->_joinStatisticsTable($qb);
  146.         $qb->select('n.id, n.messageName');
  147.         // Remove null values from the array (they would trigger a doctrine error)
  148.         $filteredNotifications array_filter($notifications, function($value) { return $value !== null; });
  149.         if ($filteredNotifications) {
  150.             $qb->andWhere($qb->expr()->notIn('n.id'$filteredNotifications));
  151.         }
  152.         $qb $this->_filterOngoingPublishedNotificationsByUser($qb$user->getCustomerNumber());
  153.         $qb $this->_filterOnlyUnreadNotification($qb);
  154.         return $qb
  155.             ->getQuery()
  156.             ->getResult();
  157.     }
  158.     public function findNewWithEmail()
  159.     {
  160.         $qb $this->createQueryBuilder('n');
  161.         $qb $qb->where('n.startAt = :todayStart')
  162.             ->andWhere('n.sendEmail = true')
  163.             ->setParameter('todayStart', new \DateTime('today'));
  164.         $qb $this->_filterPublishedNotification($qb);
  165.         return $qb
  166.             ->getQuery()
  167.             ->getResult();
  168.     }
  169. }