Passed
Push — master ( 168c03...b525a2 )
by Marcel
14:55
created

AbsenceRepository::findAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 3
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\Absence;
6
use App\Entity\Student;
7
use DateTime;
8
9
class AbsenceRepository extends AbstractTransactionalRepository implements AbsenceRepositoryInterface {
10
11
    public function findAll(): array {
12
        return $this->em
13
            ->getRepository(Absence::class)
14
            ->findAll();
15
    }
16
17
    /**
18
     * @inheritDoc
19
     */
20
    public function findAllTeachers(DateTime $date): array {
21
        $qb = $this->em->createQueryBuilder();
22
23
        $qb
24
            ->select(['p', 't'])
25
            ->from(Absence::class, 'p')
26
            ->leftJoin('p.teacher', 't')
27
            ->where($qb->expr()->isNotNull('t.id'))
28
            ->andWhere('p.date = :date')
29
            ->setParameter('date', $date);
30
31
        return $qb->getQuery()->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37
    public function findAllStudyGroups(DateTime $date): array {
38
        $qb = $this->em->createQueryBuilder();
39
40
        $qb
41
            ->select('p')
42
            ->from(Absence::class, 'p')
43
            ->leftJoin('p.studyGroup', 'sg')
44
            ->where($qb->expr()->isNotNull('sg.id'))
45
            ->andWhere('p.date = :date')
46
            ->setParameter('date', $date);
47
48
        return $qb->getQuery()->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function findAllStudentsByDateAndLesson(DateTime $dateTime, array $students, int $lesson) {
55
        $studentIds = array_map(fn(Student $student) => $student->getId(), $students);
56
57
        $qb = $this->em->createQueryBuilder();
58
        $qbInner = $this->em->createQueryBuilder();
59
60
        $qbInner
61
            ->select('sInner.id')
62
            ->from(Absence::class, 'aInner')
63
            ->leftJoin('aInner.studyGroup', 'sgInner')
64
            ->leftJoin('sgInner.memberships', 'mInner')
65
            ->leftJoin('mInner.student', 'sInner')
66
            ->where('aInner.date = :date')
67
            ->andWhere(
68
                $qb->expr()->orX(
69
                    $qb->expr()->isNull('aInner.lessonStart'),
70
                    $qb->expr()->andX(
71
                        'aInner.lessonStart <= :lesson',
72
                        'aInner.lessonEnd >= :lesson'
73
                    )
74
                )
75
            );
76
77
        $qb
78
            ->select('s')
79
            ->from(Student::class, 's')
80
            ->where($qb->expr()->in('s.id', $qbInner->getDQL()))
81
            ->andWhere($qb->expr()->in('s.id', ':students'))
82
            ->setParameter('date', $dateTime)
83
            ->setParameter('lesson', $lesson)
84
            ->setParameter('students', $studentIds);
85
86
        return $qb->getQuery()->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->getQuery()->getResult() also could return the type integer which is incompatible with the return type mandated by App\Repository\AbsenceRe...udentsByDateAndLesson() of App\Entity\Student[].
Loading history...
87
    }
88
89
    public function persist(Absence $person): void {
90
        $this->em->persist($person);
91
        $this->flushIfNotInTransaction();
92
    }
93
94
    public function removeAll(?DateTime $dateTime = null): void {
95
        $qb = $this->em->createQueryBuilder()
96
            ->delete(Absence::class, 'p');
97
98
        if($dateTime !== null) {
99
            $qb->where('p.date = :date')
100
                ->setParameter('date', $dateTime);
101
        }
102
103
        $qb
104
            ->getQuery()
105
            ->execute();
106
107
        $this->flushIfNotInTransaction();
108
    }
109
110
    public function removeBetween(DateTime $start, DateTime $end): int {
111
        return $this->em->createQueryBuilder()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->em->create...->getQuery()->execute() could return the type array<mixed,mixed> which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
112
            ->delete(Absence::class, 'a')
113
            ->where('a.date >= :start')
114
            ->andWhere('a.date <= :end')
115
            ->setParameter('start', $start)
116
            ->setParameter('end', $end)
117
            ->getQuery()
118
            ->execute();
119
    }
120
121
}