Passed
Push — master ( b61354...af9f4d )
by Julito
19:31
created

MessageRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getFromLastOneReceived() 0 21 1
A update() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Repository;
8
9
use Chamilo\CoreBundle\Entity\Message;
10
use Chamilo\CoreBundle\Entity\User;
11
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
12
use Doctrine\Common\Collections\Criteria;
13
use Doctrine\Persistence\ManagerRegistry;
14
15
class MessageRepository extends ServiceEntityRepository
16
{
17
    public function __construct(ManagerRegistry $registry)
18
    {
19
        parent::__construct($registry, Message::class);
20
    }
21
22
    public function getFromLastOneReceived(User $user, int $lastMessageId = 0)
23
    {
24
        $qb = $this->createQueryBuilder('m');
25
26
        $qb
27
            ->where(
28
                $qb->expr()->eq('m.userReceiver', $user->getId())
29
            )
30
            ->andWhere(
31
                $qb->expr()->eq('m.msgStatus', MESSAGE_STATUS_UNREAD)
32
            )
33
            ->andWhere(
34
                $qb->expr()->gt('m.id', $lastMessageId)
35
            )
36
            ->orderBy(
37
                'm.sendDate',
38
                Criteria::DESC
39
            )
40
        ;
41
42
        return $qb->getQuery()->getResult();
43
    }
44
45
    public function update(Message $message, $andFlush = true): void
46
    {
47
        $this->getEntityManager()->persist($message);
48
        if ($andFlush) {
49
            $this->getEntityManager()->flush();
50
        }
51
    }
52
}
53