kbin-core/src/EventSubscriber/Post/PostShowSubscriber.php
Ernest Wiśniewski a5f15a0b55 Symfony upgrade
2023-04-15 15:06:09 +02:00

55 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\EventSubscriber\Post;
use App\Entity\Notification;
use App\Entity\Post;
use App\Event\Post\PostHasBeenSeenEvent;
use App\Repository\NotificationRepository;
use Doctrine\ORM\EntityManagerInterface;
use JetBrains\PhpStorm\ArrayShape;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Bundle\SecurityBundle\Security;
class PostShowSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly Security $security,
private readonly NotificationRepository $repository,
private readonly EntityManagerInterface $entityManager
) {
}
#[ArrayShape([PostHasBeenSeenEvent::class => 'string'])]
public static function getSubscribedEvents(): array
{
return [
PostHasBeenSeenEvent::class => 'onShowEntry',
];
}
public function onShowEntry(PostHasBeenSeenEvent $event): void
{
$this->readMessage($event->post);
}
private function readMessage(Post $post): void
{
if (!$this->security->getUser()) {
return;
}
$notifications = $this->repository->findUnreadPostNotifications($this->security->getUser(), $post);
if (!$notifications) {
return;
}
array_map(fn ($notification) => $notification->status = Notification::STATUS_READ, $notifications);
$this->entityManager->flush();
}
}