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

MessageRepository::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
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