Passed
Push — dependabot/github_actions/code... ( 154cc6...3e3012 )
by
unknown
26:36 queued 15:59
created

TrackEOnlineRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
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\TrackEOnline;
10
use Chamilo\CoreBundle\Settings\SettingsManager;
11
use DateTime;
12
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
13
use Doctrine\ORM\NonUniqueResultException;
14
use Doctrine\Persistence\ManagerRegistry;
15
16
class TrackEOnlineRepository extends ServiceEntityRepository
17
{
18
    public function __construct(
19
        ManagerRegistry $registry,
20
        private readonly SettingsManager $settingsManager
21
    ) {
22
        parent::__construct($registry, TrackEOnline::class);
23
    }
24
25
    public function isUserOnline(int $userId): bool
26
    {
27
        $accessUrlId = 1;
28
        $timeLimit = $this->settingsManager->getSetting('display.time_limit_whosonline');
29
30
        $onlineTime = new DateTime();
31
        $onlineTime->modify("-{$timeLimit} minutes");
32
33
        $qb = $this->createQueryBuilder('t')
34
            ->select('COUNT(t.loginUserId)')
35
            ->where('t.loginUserId = :userId')
36
            ->andWhere('t.accessUrlId = :accessUrlId')
37
            ->andWhere('t.loginDate >= :limitDate')
38
            ->setParameter('userId', $userId)
39
            ->setParameter('accessUrlId', $accessUrlId)
40
            ->setParameter('limitDate', $onlineTime)
41
            ->setMaxResults(1)
42
        ;
43
44
        try {
45
            $count = $qb->getQuery()->getSingleScalarResult();
46
47
            return $count > 0;
48
        } catch (NonUniqueResultException $e) {
49
            // Handle exception
50
            return false;
51
        }
52
    }
53
}
54