Passed
Push — develop ( ef53f3...80bc53 )
by BENARD
08:34
created

ReadTopicSubscriber   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 8
dl 0
loc 30
rs 10
c 1
b 0
f 0
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
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 21 at column 6
Loading history...
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