Passed
Pull Request — master (#6053)
by
unknown
07:47
created

CAttendanceSheetRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 24
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserScore() 0 17 2
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CourseBundle\Repository;
8
9
use Chamilo\CoreBundle\Repository\ResourceRepository;
10
use Chamilo\CourseBundle\Entity\CAttendanceSheet;
11
use Doctrine\Persistence\ManagerRegistry;
12
13
final class CAttendanceSheetRepository extends ResourceRepository
14
{
15
    public function __construct(ManagerRegistry $registry)
16
    {
17
        parent::__construct($registry, CAttendanceSheet::class);
18
    }
19
20
    public function getUserScore(int $userId, int $attendanceId, ?int $groupId = null): int
21
    {
22
        $qb = $this->createQueryBuilder('sheet')
23
            ->select('SUM(sheet.presence) as score')
24
            ->join('sheet.attendanceCalendar', 'calendar')
25
            ->where('calendar.attendance = :attendanceId')
26
            ->andWhere('sheet.user = :userId')
27
            ->setParameter('attendanceId', $attendanceId)
28
            ->setParameter('userId', $userId);
29
30
        if ($groupId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $groupId of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
31
            $qb->join('calendar.groups', 'groups')
32
                ->andWhere('groups.group = :groupId')
33
                ->setParameter('groupId', $groupId);
34
        }
35
36
        return (int) $qb->getQuery()->getSingleScalarResult();
37
    }
38
}
39