| Conditions | 8 |
| Paths | 26 |
| Total Lines | 63 |
| Code Lines | 40 |
| 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 |
||
| 30 | public function markTopicAsRead($user, Topic $topic, bool $flush = true): array |
||
| 31 | { |
||
| 32 | $now = new \DateTime(); |
||
| 33 | $forum = $topic->getForum(); |
||
| 34 | |||
| 35 | // 1. Vérifier si le topic est déjà lu |
||
| 36 | $topicVisit = $this->em->getRepository(TopicUserLastVisit::class) |
||
| 37 | ->findOneBy(['user' => $user, 'topic' => $topic]); |
||
| 38 | |||
| 39 | $wasAlreadyRead = false; |
||
| 40 | if ($topicVisit && $topic->getLastMessage()) { |
||
| 41 | $wasAlreadyRead = $topicVisit->getLastVisitedAt() >= $topic->getLastMessage()->getCreatedAt(); |
||
| 42 | } |
||
| 43 | |||
| 44 | // Si déjà lu, pas besoin de continuer |
||
| 45 | if ($wasAlreadyRead) { |
||
| 46 | return [ |
||
| 47 | 'topicMarkedAsRead' => false, |
||
| 48 | 'forumMarkedAsRead' => false, |
||
| 49 | 'wasAlreadyRead' => true |
||
| 50 | ]; |
||
| 51 | } |
||
| 52 | |||
| 53 | // 2. Mettre à jour ou créer la visite du topic |
||
| 54 | if ($topicVisit) { |
||
| 55 | $topicVisit->setLastVisitedAt($now); |
||
| 56 | } else { |
||
| 57 | $topicVisit = new TopicUserLastVisit(); |
||
| 58 | $topicVisit->setUser($user); |
||
| 59 | $topicVisit->setTopic($topic); |
||
| 60 | $topicVisit->setLastVisitedAt($now); |
||
| 61 | $this->em->persist($topicVisit); |
||
| 62 | } |
||
| 63 | |||
| 64 | // 3. Vérifier si tous les topics du forum sont maintenant lus |
||
| 65 | $unreadTopicsCount = $this->countUnreadTopicsInForum($user, $forum); |
||
| 66 | $forumMarkedAsRead = false; |
||
| 67 | |||
| 68 | // 4. Si aucun topic non lu, marquer le forum comme lu |
||
| 69 | if ($unreadTopicsCount === 0) { |
||
| 70 | $forumVisit = $this->em->getRepository(ForumUserLastVisit::class) |
||
| 71 | ->findOneBy(['user' => $user, 'forum' => $forum]); |
||
| 72 | |||
| 73 | if ($forumVisit) { |
||
| 74 | $forumVisit->setLastVisitedAt($now); |
||
| 75 | } else { |
||
| 76 | $forumVisit = new ForumUserLastVisit(); |
||
| 77 | $forumVisit->setUser($user); |
||
| 78 | $forumVisit->setForum($forum); |
||
| 79 | $forumVisit->setLastVisitedAt($now); |
||
| 80 | $this->em->persist($forumVisit); |
||
| 81 | } |
||
| 82 | $forumMarkedAsRead = true; |
||
| 83 | } |
||
| 84 | |||
| 85 | if ($flush) { |
||
| 86 | $this->em->flush(); |
||
| 87 | } |
||
| 88 | |||
| 89 | return [ |
||
| 90 | 'topicMarkedAsRead' => true, |
||
| 91 | 'forumMarkedAsRead' => $forumMarkedAsRead, |
||
| 92 | 'wasAlreadyRead' => false |
||
| 93 | ]; |
||
| 139 |