Conditions | 4 |
Paths | 42 |
Total Lines | 80 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | } |
||
110 |