MarkAdReadAll::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 42

Size

Total Lines 80
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 54
c 0
b 0
f 0
dl 0
loc 80
rs 9.0036
cc 4
nc 42
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProjetNormandie\ForumBundle\Controller;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use ProjetNormandie\ForumBundle\Entity\ForumUserLastVisit;
9
use ProjetNormandie\ForumBundle\Entity\TopicUserLastVisit;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpKernel\Attribute\AsController;
13
14
#[AsController]
15
class MarkAdReadAll extends AbstractController
16
{
17
    private EntityManagerInterface $em;
18
19
    public function __construct(EntityManagerInterface $em)
20
    {
21
        $this->em = $em;
22
    }
23
24
    /**
25
     * Marque tous les forums et topics comme lus pour l'utilisateur connecté
26
     */
27
    public function __invoke(): JsonResponse
28
    {
29
        $user = $this->getUser();
30
        $now = new \DateTime();
31
32
        try {
33
            $this->em->beginTransaction();
34
35
            // 1. Mettre à jour toutes les visites existantes de forums
36
            $this->em->createQueryBuilder()
37
                ->update('ProjetNormandie\ForumBundle\Entity\ForumUserLastVisit', 'fuv')
38
                ->set('fuv.lastVisitedAt', ':now')
39
                ->where('fuv.user = :user')
40
                ->setParameter('now', $now)
41
                ->setParameter('user', $user)
42
                ->getQuery()
43
                ->execute();
44
45
            // 2. Créer des visites pour les forums jamais visités qui ont des messages
46
            $forumsNeverVisited = $this->em->createQueryBuilder()
47
                ->select('f')
48
                ->from('ProjetNormandie\ForumBundle\Entity\Forum', 'f')
49
                ->where('f.lastMessage IS NOT NULL')
50
                ->andWhere('f.id NOT IN (
51
                    SELECT IDENTITY(fuv.forum) 
52
                    FROM ProjetNormandie\ForumBundle\Entity\ForumUserLastVisit fuv 
53
                    WHERE fuv.user = :user
54
                )')
55
                ->setParameter('user', $user)
56
                ->getQuery()
57
                ->getResult();
58
59
            foreach ($forumsNeverVisited as $forum) {
60
                $visit = new ForumUserLastVisit();
61
                $visit->setUser($user);
62
                $visit->setForum($forum);
63
                $visit->setLastVisitedAt($now);
64
                $this->em->persist($visit);
65
            }
66
67
            // 3. Mettre à jour toutes les visites existantes de topics
68
            $this->em->createQueryBuilder()
69
                ->update('ProjetNormandie\ForumBundle\Entity\TopicUserLastVisit', 'tuv')
70
                ->set('tuv.lastVisitedAt', ':now')
71
                ->where('tuv.user = :user')
72
                ->setParameter('now', $now)
73
                ->setParameter('user', $user)
74
                ->getQuery()
75
                ->execute();
76
77
            // 4. Créer des visites pour les topics jamais visités qui ont des messages
78
            $topicsNeverVisited = $this->em->createQueryBuilder()
79
                ->select('t')
80
                ->from('ProjetNormandie\ForumBundle\Entity\Topic', 't')
81
                ->where('t.lastMessage IS NOT NULL')
82
                ->andWhere('t.id NOT IN (
83
                    SELECT IDENTITY(tuv.topic) 
84
                    FROM ProjetNormandie\ForumBundle\Entity\TopicUserLastVisit tuv 
85
                    WHERE tuv.user = :user
86
                )')
87
                ->setParameter('user', $user)
88
                ->getQuery()
89
                ->getResult();
90
91
            foreach ($topicsNeverVisited as $topic) {
92
                $visit = new TopicUserLastVisit();
93
                $visit->setUser($user);
94
                $visit->setTopic($topic);
95
                $visit->setLastVisitedAt($now);
96
                $this->em->persist($visit);
97
            }
98
99
            $this->em->flush();
100
            $this->em->commit();
101
102
            return new JsonResponse(['success' => true]);
103
104
        } catch (\Exception $e) {
105
            $this->em->rollback();
106
            return new JsonResponse(['success' => false, 'error' => $e->getMessage()], 500);
107
        }
108
    }
109
}
110