Passed
Push — master ( 79866c...d57250 )
by Marcel
09:45
created

TeacherAbsenceRepository::removeRange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\Teacher;
6
use App\Entity\TeacherAbsence;
7
use App\Entity\TeacherAbsenceLesson;
8
use DateTime;
9
use Doctrine\ORM\Tools\Pagination\Paginator;
10
11
class TeacherAbsenceRepository extends AbstractRepository implements TeacherAbsenceRepositoryInterface {
12
13
    public function getPaginator(int $itemsPerPage, int &$page, bool $hideProcessed, ?DateTime $startDate, ?DateTime $endDate, ?Teacher $teacher = null): Paginator {
14
        $qb = $this->em->createQueryBuilder()
15
            ->select('a')
16
            ->from(TeacherAbsence::class, 'a')
17
            ->orderBy('a.from.date', 'desc');
18
19
        if($hideProcessed) {
20
            $qb->andWhere('a.processedAt IS NULL');
21
        }
22
23
        if($startDate !== null) {
24
            $qb->andWhere('a.from.date >= :start');
25
        }
26
27
        if($endDate !== null) {
28
            $qb->andWhere('a.until.date <= :end');
29
        }
30
31
        if($teacher !== null) {
32
            $qb->andWhere('a.teacher = :teacher')
33
                ->setParameter('teacher', $teacher);
34
        }
35
36
        if($page < 1) {
37
            $page = 1;
38
        }
39
40
        $offset = ($page - 1) * $itemsPerPage;
41
        $paginator = new Paginator($qb);
42
        $paginator->getQuery()
43
            ->setMaxResults($itemsPerPage)
44
            ->setFirstResult($offset);
45
46
        return $paginator;
47
    }
48
49
    public function persist(TeacherAbsence|TeacherAbsenceLesson $absenceOrLesson): void {
50
        $this->em->persist($absenceOrLesson);
51
        $this->em->flush();
52
    }
53
54
    public function remove(TeacherAbsence|TeacherAbsenceLesson $absenceOrLesson): void {
55
        $this->em->remove($absenceOrLesson);
56
        $this->em->flush();
57
    }
58
59
    public function removeRange(DateTime $start, DateTime $end): int {
60
        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...
61
            ->delete(TeacherAbsence::class, 't')
62
            ->where('t.until.date >= :start')
63
            ->andWhere('t.until.date <= :end')
64
            ->setParameter('start', $start)
65
            ->setParameter('end', $end)
66
            ->getQuery()
67
            ->execute();
68
    }
69
}