Passed
Push — develop ( 8cbecb...4958fb )
by BENARD
02:34
created

TopicService::setNotRead()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 10
rs 10
c 1
b 0
f 0
cc 2
nc 2
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\Topic;
8
9
class TopicService
10
{
11
    private EntityManagerInterface $em;
12
13
    /**
14
     * TopicService constructor.
15
     * @param EntityManagerInterface $em
16
     */
17
    public function __construct(EntityManagerInterface $em)
18
    {
19
        $this->em = $em;
20
    }
21
22
    /**
23
     * @param $topic
24
     * @return Topic
25
     */
26
    private function getTopic($topic): Topic
0 ignored issues
show
Unused Code introduced by
The method getTopic() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
27
    {
28
        if (!$topic instanceof Topic) {
29
            $topic = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\Topic')
30
                ->findOneBy(['id' => $topic]);
31
        }
32
        return $topic;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $topic could return the type null which is incompatible with the type-hinted return ProjetNormandie\ForumBundle\Entity\Topic. Consider adding an additional type-check to rule them out.
Loading history...
33
    }
34
35
    /**
36
     * @param Topic $topic
37
     */
38
    public function majPositions(Topic $topic)
39
    {
40
        $list = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\Message')->findBy(['topic' => $topic], ['id' => 'ASC']);
41
        $i = 1;
42
        foreach ($list as $message) {
43
            $message->setPosition($i);
44
            $i++;
45
        }
46
        $this->em->flush();
47
    }
48
49
    /**
50
     * @param Topic $topic
51
     * @param       $user
52
     */
53
    public function setNotRead(Topic $topic, $user)
54
    {
55
        //
56
        // Topic
57
        $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\TopicUser')->setNotRead($topic, $user);
58
        // Forum
59
        $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\ForumUser')->setNotRead($topic->getForum(), $user);
60
        // Forum Parent
61
        if ($topic->getForum()->getParent() != null) {
62
            $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\ForumUser')->setNotRead($topic->getForum()->getParent(), $user);
63
        }
64
    }
65
66
    /**
67
     * @param Topic $topic
68
     * @throws ORMException
69
     */
70
    public function maj(Topic $topic)
71
    {
72
        $data = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\Message')->getTopicData($topic);
73
        $topic->setLastMessage($this->em->getReference('ProjetNormandie\ForumBundle\Entity\Message', $data['lastMessage']));
74
        $topic->setNbMessage($data['nbMessage']);
75
        $this->em->flush();
76
    }
77
}
78