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

ReadAll::__invoke()   A

Complexity

Conditions 4
Paths 42

Size

Total Lines 80
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

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