Passed
Push — master ( b525a2...6ed794 )
by Marcel
06:23
created

BookCommentRepository::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
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\BookComment;
6
use App\Entity\Grade;
7
use App\Entity\Section;
8
use App\Entity\Student;
9
use App\Entity\Tuition;
10
use DateTime;
11
12
class BookCommentRepository extends AbstractRepository implements BookCommentRepositoryInterface {
13
14
    /**
15
     * @inheritDoc
16
     */
17
    public function findAllByDateAndStudent(Student $student, DateTime $start, DateTime $end): array {
18
        $qb = $this->em->createQueryBuilder()
19
            ->select(['c', 's'])
20
            ->from(BookComment::class, 'c')
21
            ->leftJoin('c.students', 's')
22
            ->where('s.id = :student')
23
            ->andWhere('c.date >= :start')
24
            ->andWhere('c.date <= :end')
25
            ->setParameter('start', $start)
26
            ->setParameter('end', $end)
27
            ->setParameter('student', $student->getId());
28
29
        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...
30
    }
31
32
    public function findAllByDateAndGrade(Grade $grade, Section $section, DateTime $start, DateTime $end): array {
33
        $qbStudents = $this->em->createQueryBuilder()
34
            ->select('student.id')
35
            ->from(Grade::class, 'gInner')
36
            ->leftJoin('gInner.memberships', 'mInner')
37
            ->leftJoin('mInner.student', 'student')
38
            ->leftJoin('mInner.section', 'secInner')
39
            ->where('secInner.id = :section')
40
            ->andWhere('gInner.id = :grade');
41
42
        $qbInner = $this->em->createQueryBuilder()
43
            ->select('cInner.id')
44
            ->from(BookComment::class, 'cInner')
45
            ->leftJoin('cInner.students', 'sInner');
46
47
        $qbInner->where(
48
            $qbInner->expr()->in('sInner.id', $qbStudents->getDQL())
49
        );
50
51
        $qb = $this->em->createQueryBuilder()
52
            ->select(['c', 's'])
53
            ->from(BookComment::class, 'c')
54
            ->leftJoin('c.students', 's');
55
        $qb->where(
56
            $qb->expr()->in('c.id', $qbInner->getDQL())
57
        )
58
            ->andWhere('c.date >= :start')
59
            ->andWhere('c.date <= :end')
60
            ->setParameter('start', $start)
61
            ->setParameter('end', $end)
62
            ->setParameter('section', $section->getId())
63
            ->setParameter('grade', $grade->getId());
64
65
        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...
66
    }
67
68
    public function findAllByDateAndTuition(Tuition $tuition, DateTime $start, DateTime $end): array {
69
        $qbStudents = $this->em->createQueryBuilder()
70
            ->select('student.id')
71
            ->from(Tuition::class, 'tInner')
72
            ->leftJoin('tInner.studyGroup', 'sgInner')
73
            ->leftJoin('sgInner.memberships', 'mInner')
74
            ->leftJoin('mInner.student', 'student')
75
            ->where('tInner.id = :tuition');
76
77
        $qbInner = $this->em->createQueryBuilder()
78
            ->select('cInner.id')
79
            ->from(BookComment::class, 'cInner')
80
            ->leftJoin('cInner.students', 'sInner');
81
82
        $qbInner->where(
83
            $qbInner->expr()->in('sInner.id', $qbStudents->getDQL())
84
        );
85
86
        $qb = $this->em->createQueryBuilder()
87
            ->select(['c', 's'])
88
            ->from(BookComment::class, 'c')
89
            ->leftJoin('c.students', 's');
90
        $qb->where(
91
            $qb->expr()->in('c.id', $qbInner->getDQL())
92
        )
93
            ->andWhere('c.date >= :start')
94
            ->andWhere('c.date <= :end')
95
            ->setParameter('start', $start)
96
            ->setParameter('end', $end)
97
            ->setParameter('tuition', $tuition->getId());
98
99
        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...
100
    }
101
102
    public function persist(BookComment $comment): void {
103
        $this->em->persist($comment);
104
        $this->em->flush();
105
    }
106
107
    public function remove(BookComment $comment): void {
108
        $this->em->remove($comment);
109
        $this->em->flush();
110
    }
111
112
    public function removeRange(DateTime $start, DateTime $end): int {
113
        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...
114
            ->delete(BookComment::class, 'c')
115
            ->where('c.date >= :start')
116
            ->andWhere('c.date <= :end')
117
            ->setParameter('start', $start)
118
            ->setParameter('end', $end)
119
            ->getQuery()
120
            ->execute();
121
    }
122
}