MessageRepository::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProjetNormandie\ForumBundle\Repository;
6
7
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
8
use Doctrine\ORM\NonUniqueResultException;
9
use Doctrine\Persistence\ManagerRegistry;
10
use ProjetNormandie\ForumBundle\Entity\Message;
11
use ProjetNormandie\ForumBundle\Entity\Topic;
12
13
class MessageRepository extends ServiceEntityRepository
14
{
15
    public function __construct(ManagerRegistry $registry)
16
    {
17
        parent::__construct($registry, Message::class);
18
    }
19
20
    public function getPosition($message)
21
    {
22
        $qb = $this->createQueryBuilder('message')
23
            ->select('COUNT(message.id)')
24
            ->where('message.id <= :id')
25
            ->setParameter('id', $message->getId());
26
27
        return $qb->getQuery()
28
            ->getOneOrNullResult();
29
    }
30
31
32
    /**
33
     * @param Topic $topic
34
     * @return mixed
35
     * @throws NonUniqueResultException
36
     */
37
    public function getLastMessageId(Topic $topic)
38
    {
39
         $qb = $this->createQueryBuilder('m')
40
            ->select('MAX(m.id) as lastMessage')
41
            ->where('m.topic = :topic')
42
            ->setParameter('topic', $topic);
43
44
        return $qb->getQuery()
45
            ->getOneOrNullResult();
46
    }
47
}
48