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

ToggleNotification   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 36
c 1
b 0
f 0
dl 0
loc 62
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 48 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProjetNormandie\ForumBundle\Controller\Topic;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use ProjetNormandie\ForumBundle\Entity\Topic;
9
use ProjetNormandie\ForumBundle\Entity\TopicUserLastVisit;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Contracts\Translation\TranslatorInterface;
13
14
class ToggleNotification extends AbstractController
15
{
16
    private EntityManagerInterface $em;
17
    private TranslatorInterface $translator;
18
19
    public function __construct(EntityManagerInterface $em, TranslatorInterface $translator)
20
    {
21
        $this->em = $em;
22
        $this->translator = $translator;
23
    }
24
25
    /**
26
     * Toggle la notification pour un topic donné
27
     */
28
    public function __invoke(Topic $topic): JsonResponse
29
    {
30
        $user = $this->getUser();
31
32
        if (!$user) {
33
            return new JsonResponse([
34
                'error' => $this->translator->trans('error.authentication_required')
35
            ], 401);
36
        }
37
38
        try {
39
            $this->em->beginTransaction();
40
41
            // Récupérer ou créer la visite du topic
42
            $topicVisit = $this->em->getRepository(TopicUserLastVisit::class)
43
                ->findOneBy(['user' => $user, 'topic' => $topic]);
44
45
            if (!$topicVisit) {
46
                // Créer une nouvelle visite si elle n'existe pas
47
                $topicVisit = new TopicUserLastVisit();
48
                $topicVisit->setUser($user);
49
                $topicVisit->setTopic($topic);
50
                // La date de visite sera définie automatiquement dans le constructeur
51
                $this->em->persist($topicVisit);
52
            }
53
54
            // Toggle la notification
55
            $currentNotificationStatus = $topicVisit->getIsNotify();
56
            $newNotificationStatus = !$currentNotificationStatus;
57
            $topicVisit->setIsNotify($newNotificationStatus);
58
59
            $this->em->flush();
60
            $this->em->commit();
61
62
            return new JsonResponse([
63
                'success' => true,
64
                'isNotify' => $newNotificationStatus,
65
                'message' => $this->translator->trans($newNotificationStatus ?
66
                    'topic.notification.enabled' :
67
                    'topic.notification.disabled')
68
            ]);
69
70
        } catch (\Exception $e) {
71
            $this->em->rollback();
72
            return new JsonResponse([
73
                'success' => false,
74
                'error' => $this->translator->trans('error.notification_update_failed')
75
            ], 500);
76
        }
77
    }
78
}
79