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

TopicUserRepository::setNotRead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace ProjetNormandie\ForumBundle\Repository;
4
5
use Doctrine\DBAL\Exception;
6
use Doctrine\ORM\EntityRepository;
7
use Doctrine\ORM\NonUniqueResultException;
8
use Doctrine\ORM\NoResultException;
9
10
/**
11
 * Specific repository that serves the Forum entity.
12
 */
13
class TopicUserRepository extends EntityRepository
14
{
15
16
    /**
17
     * @param $user
18
     * @throws Exception
19
     */
20
    public function init($user)
21
    {
22
        $query ="INSERT INTO forum_topic_user (idTopic, idUser)
23
                 SELECT id, :idUser FROM forum_topic";
24
        $this->_em->getConnection()->executeStatement($query, array('idUser' => $user->getId()));
25
    }
26
27
28
    /**
29
     * @param $user
30
     * @param $forum
31
     * @throws Exception
32
     */
33
    public function readForum($user, $forum)
34
    {
35
        $this->_em->getConnection()->executeStatement(
36
            "UPDATE forum_topic_user 
37
                SET boolRead = 1 
38
                WHERE idUser=:idUser
39
                AND idTopic IN (SELECT id FROM forum_topic WHERE idForum=:idForum)",
40
            ['idUser' => $user->getId(), 'idForum' => $forum->getId()]
41
        );
42
    }
43
44
    /**
45
     * @param $topic
46
     * @param $user
47
     */
48
    public function setNotRead($topic, $user)
49
    {
50
         $qb = $this->_em->createQueryBuilder();
51
         $query = $qb->update('ProjetNormandie\ForumBundle\Entity\TopicUser', 'tu')
52
            ->set('tu.boolRead', ':boolRead')
53
            ->where('tu.user != :user')
54
            ->andWhere('tu.topic = :topic')
55
            ->setParameter('boolRead', 0)
56
            ->setParameter('topic', $topic)
57
            ->setParameter('user', $user);
58
59
        $query->getQuery()->execute();
60
    }
61
62
    /**
63
     * @param $forum
64
     * @param $user
65
     * @return mixed
66
     * @throws NonUniqueResultException
67
     * @throws NoResultException
68
     */
69
    public function countNotRead($forum, $user)
70
    {
71
         $query = $this->createQueryBuilder('tu')
72
             ->select('COUNT(tu.id)')
73
             ->join('tu.topic', 't')
74
             ->where('t.forum = :forum')
75
             ->andWhere('tu.user = :user')
76
             ->andWhere('tu.boolRead = 0')
77
             ->setParameter('forum', $forum)
78
             ->setParameter('user', $user);
79
80
        return $query->getQuery()->getSingleScalarResult();
81
    }
82
}
83