Conditions | 4 |
Paths | 42 |
Total Lines | 80 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
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 | } |
||
108 |