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

ResourceReservationRepository::removeBetween()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\ResourceEntity;
6
use App\Entity\ResourceReservation;
7
use App\Entity\Teacher;
8
use DateTime;
9
10
class ResourceReservationRepository extends AbstractRepository implements ResourceReservationRepositoryInterface {
11
12
    /**
13
     * @inheritDoc
14
     */
15
    public function findAll(): array {
16
        return $this->em->getRepository(ResourceReservation::class)
17
            ->findAll();
18
    }
19
20
    public function findOneByDateAndResourceAndLesson(DateTime $dateTime, ResourceEntity $resource, int $lessonNumber): ?ResourceReservation {
21
        $qb = $this->em->createQueryBuilder()
22
            ->select(['r', 'rt', 'rr'])
23
            ->from(ResourceReservation::class, 'r')
24
            ->leftJoin('r.teacher', 'rt')
25
            ->leftJoin('r.resource', 'rr')
26
            ->where('r.date = :date')
27
            ->andWhere('rr.id = :resource')
28
            ->andWhere('r.lessonStart <= :lesson')
29
            ->andWhere('r.lessonEnd >= :lesson')
30
            ->setParameter('resource', $resource->getId())
31
            ->setParameter('date', $dateTime)
32
            ->setParameter('lesson', $lessonNumber)
33
            ->setMaxResults(1);
34
35
        return $qb->getQuery()->getOneOrNullResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->getQuery()->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return App\Entity\ResourceReservation|null. Consider adding an additional type-check to rule them out.
Loading history...
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function findAllByDate(DateTime $date): array {
42
        return $this->em->getRepository(ResourceReservation::class)
43
            ->findBy([
44
                'date' => $date
45
            ]);
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    public function findAllByResourceAndDate(ResourceEntity $resource, DateTime $dateTime): array {
52
        return $this->em->getRepository(ResourceReservation::class)
53
            ->findBy([
54
                'date' => $dateTime,
55
                'resource' => $resource
56
            ]);
57
    }
58
59
    public function persist(ResourceReservation $reservation): void {
60
        $this->em->persist($reservation);
61
        $this->em->flush();
62
    }
63
64
    public function remove(ResourceReservation $reservation): void {
65
        $this->em->remove($reservation);
66
        $this->em->flush();
67
    }
68
69
    public function removeBetween(DateTime $start, DateTime $end): int {
70
        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...
71
            ->delete(ResourceReservation::class, 'r')
72
            ->where('r.date >= :start')
73
            ->andWhere('r.date <= :end')
74
            ->setParameter('start', $start)
75
            ->setParameter('end', $end)
76
            ->getQuery()
77
            ->execute();
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83
    public function findAllByRoomAndTeacher(?ResourceEntity $resource, ?Teacher $teacher, ?DateTime $from): array {
84
        $qb = $this->em->createQueryBuilder()
85
            ->select('r')
86
            ->from(ResourceReservation::class, 'r')
87
            ->leftJoin('r.resource', 'rr')
88
            ->leftJoin('r.teacher', 'rt');
89
90
        if($resource !== null) {
91
            $qb->andWhere('rr.id = :resource')
92
                ->setParameter('resource', $resource->getId());
93
        }
94
95
        if($teacher !== null) {
96
            $qb->andWhere('rt.id = :teacher')
97
                ->setParameter('teacher', $teacher->getId());
98
        }
99
100
        if($from !== null) {
101
            $qb->andWhere('r.date >= :from')
102
                ->setParameter('from', $from);
103
        }
104
105
        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...
106
    }
107
108
    /**
109
     * @inheritDoc
110
     */
111
    public function findAllByTeacherAndDate(Teacher $teacher, DateTime $date): array {
112
        $qb = $this->em->createQueryBuilder()
113
            ->select('r')
114
            ->from(ResourceReservation::class, 'r')
115
            ->leftJoin('r.resource', 'rr')
116
            ->leftJoin('r.teacher', 'rt')
117
            ->andWhere('rt.id = :teacher')
118
            ->setParameter('teacher', $teacher->getId());
119
120
        if($date !== null) {
121
            $qb->andWhere('r.date = :date')
122
                ->setParameter('date', $date);
123
        }
124
125
        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...
126
    }
127
}