|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ProjetNormandie\ForumBundle\EventSubscriber; |
|
6
|
|
|
|
|
7
|
|
|
use ApiPlatform\Symfony\EventListener\EventPriorities as EventPrioritiesAlias; |
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
9
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
|
10
|
|
|
use Doctrine\ORM\NoResultException; |
|
11
|
|
|
use ProjetNormandie\ForumBundle\Entity\ForumUserLastVisit; |
|
12
|
|
|
use ProjetNormandie\ForumBundle\Entity\Topic; |
|
13
|
|
|
use ProjetNormandie\ForumBundle\Entity\TopicUserLastVisit; |
|
14
|
|
|
use ProjetNormandie\ForumBundle\Service\TopicReadService; |
|
15
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
18
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
|
19
|
|
|
use Symfony\Bundle\SecurityBundle\Security; |
|
20
|
|
|
|
|
21
|
|
|
final readonly class ReadTopicSubscriber implements EventSubscriberInterface |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
public function __construct( |
|
24
|
|
|
private Security $security, |
|
25
|
|
|
private TopicReadService $topicReadService, |
|
26
|
|
|
) { |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function getSubscribedEvents(): array |
|
30
|
|
|
{ |
|
31
|
|
|
return [ |
|
32
|
|
|
KernelEvents::REQUEST => ['setRead', EventPrioritiesAlias::POST_READ], |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Marque automatiquement un topic comme lu lors de sa consultation |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setRead(RequestEvent $event): void |
|
40
|
|
|
{ |
|
41
|
|
|
$topic = $event->getRequest()->attributes->get('data'); |
|
42
|
|
|
$method = $event->getRequest()->getMethod(); |
|
43
|
|
|
$user = $this->security->getUser(); |
|
44
|
|
|
|
|
45
|
|
|
if ($user && ($topic instanceof Topic) && $method == Request::METHOD_GET) { |
|
46
|
|
|
try { |
|
47
|
|
|
$this->topicReadService->markTopicAsRead($user, $topic); |
|
48
|
|
|
} catch (\Exception $e) { |
|
49
|
|
|
// Log l'erreur si nécessaire, mais ne pas interrompre la requête |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|