mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
78 lines
2.2 KiB
PHP
78 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Chill\MainBundle\Notification;
|
|
|
|
use Chill\MainBundle\Entity\Notification;
|
|
use Chill\MainBundle\Entity\User;
|
|
use Chill\MainBundle\Repository\NotificationRepository;
|
|
use Symfony\Component\Security\Core\Security;
|
|
use function array_key_exists;
|
|
|
|
/**
|
|
* Helps to find if a notification exist for a given entity.
|
|
*/
|
|
class NotificationPresence
|
|
{
|
|
private array $cache = [];
|
|
|
|
private NotificationRepository $notificationRepository;
|
|
|
|
private Security $security;
|
|
|
|
public function __construct(Security $security, NotificationRepository $notificationRepository)
|
|
{
|
|
$this->security = $security;
|
|
$this->notificationRepository = $notificationRepository;
|
|
}
|
|
|
|
public function countNotificationsForClassAndEntity(string $relatedEntityClass, int $relatedEntityId): array
|
|
{
|
|
if (array_key_exists($relatedEntityClass, $this->cache) && array_key_exists($relatedEntityId, $this->cache[$relatedEntityClass])) {
|
|
return $this->cache[$relatedEntityClass][$relatedEntityId];
|
|
}
|
|
|
|
$user = $this->security->getUser();
|
|
|
|
if ($user instanceof User) {
|
|
$counter = $this->notificationRepository->countNotificationByRelatedEntityAndUserAssociated(
|
|
$relatedEntityClass,
|
|
$relatedEntityId,
|
|
$user
|
|
);
|
|
|
|
$this->cache[$relatedEntityClass][$relatedEntityId] = $counter;
|
|
|
|
return $counter;
|
|
}
|
|
|
|
return ['unread' => 0, 'read' => 0];
|
|
}
|
|
|
|
/**
|
|
* @return array|Notification[]
|
|
*/
|
|
public function getNotificationsForClassAndEntity(string $relatedEntityClass, int $relatedEntityId): array
|
|
{
|
|
$user = $this->security->getUser();
|
|
|
|
if ($user instanceof User) {
|
|
return $this->notificationRepository->findNotificationByRelatedEntityAndUserAssociated(
|
|
$relatedEntityClass,
|
|
$relatedEntityId,
|
|
$user
|
|
);
|
|
}
|
|
|
|
return [];
|
|
}
|
|
}
|