MessageRepository   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 14
dl 0
loc 33
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getLastMessageId() 0 9 1
A getPosition() 0 9 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