Passed
Push — develop ( 743c4e...d8f86b )
by BENARD
08:44
created

ForumService::setRead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace ProjetNormandie\ForumBundle\Service;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\ORMException;
7
use ProjetNormandie\ForumBundle\Entity\Forum;
8
9
class ForumService
10
{
11
    private EntityManagerInterface $em;
12
    private TopicService $topicService;
13
14
    /**
15
     * ForumService constructor.
16
     * @param EntityManagerInterface $em
17
     * @param TopicService           $topicService
18
     */
19
    public function __construct(EntityManagerInterface $em, TopicService $topicService)
20
    {
21
        $this->em = $em;
22
        $this->topicService = $topicService;
23
    }
24
25
    /**
26
     * @param $forum
27
     * @return Forum
28
     */
29
    private function getForum($forum): Forum
30
    {
31
        if (!$forum instanceof Forum) {
32
            $forum = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\Forum')
33
                ->findOneBy(['id' => $forum]);
34
        }
35
        return $forum;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $forum could return the type null which is incompatible with the type-hinted return ProjetNormandie\ForumBundle\Entity\Forum. Consider adding an additional type-check to rule them out.
Loading history...
36
    }
37
38
39
    /**
40
     * @param $forum
41
     * @throws ORMException
42
     */
43
    public function majParent($forum)
44
    {
45
        $forum = $this->getForum($forum);
46
        $data = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\Forum')->getParentData($forum);
47
        $forum->setLastMessage($this->em->getReference('ProjetNormandie\ForumBundle\Entity\Message', $data['lastMessage']));
48
        $forum->setNbTopic($data['nbTopic']);
49
        $forum->setNbMessage($data['nbMessage']);
50
        $this->em->flush();
51
    }
52
53
54
    /**
55
     * @param Forum $forum
56
     * @throws ORMException
57
     */
58
    public function maj(Forum $forum)
59
    {
60
        $data = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\Topic')->getForumData($forum);
61
        $forum->setLastMessage($this->em->getReference('ProjetNormandie\ForumBundle\Entity\Message', $data['lastMessage']));
62
        $forum->setNbTopic($data['nbTopic']);
63
        $forum->setNbMessage($data['nbMessage']);
64
        $this->em->flush();
65
    }
66
67
    /**
68
     * @param $forum
69
     */
70
    public function majPosition($forum)
71
    {
72
        $forum = $this->getForum($forum);
73
        if ($forum->getIsParent()) {
74
            foreach ($forum->getChildrens() as $child) {
75
                foreach ($child->getTopics() as $topic) {
76
                    $this->topicService->majPositions($topic);
77
                }
78
            }
79
        } else {
80
            foreach ($forum->getTopics() as $topic) {
81
                $this->topicService->majPositions($topic);
82
            }
83
        }
84
    }
85
86
87
    /**
88
     * @param Forum $forum
89
     * @param       $user
90
     */
91
    public function setNotRead(Forum $forum, $user)
92
    {
93
        $forumUser = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\ForumUser')
94
                ->findOneBy(['forum' => $forum, 'user' => $user]);
95
        $forumUser->setBoolRead(false);
96
        $this->em->flush();
97
    }
98
99
100
    /**
101
     * @param Forum $forum
102
     * @param       $user
103
     * @return int
104
     */
105
    public function countTopicNotRead(Forum $forum, $user): int
106
    {
107
        return $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\TopicUser')
108
                ->countNotRead($forum,$user);
109
    }
110
111
112
    /**
113
     * @param Forum $parent
114
     * @param       $user
115
     * @return int
116
     */
117
    public function countSubForumNotRead(Forum $parent, $user): int
118
    {
119
        return $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\ForumUser')
120
                ->countSubForumNotRead($parent, $user);
121
    }
122
}
123