Passed
Push — master ( 2daeaa...eba4a8 )
by Marcel
16:01
created

TeacherRepository::findAllByAcronym()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
ccs 0
cts 5
cp 0
crap 2
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\Section;
6
use App\Entity\Subject;
7
use App\Entity\Teacher;
8
use App\Entity\TeacherTag;
9
use DateTime;
10
use Doctrine\ORM\QueryBuilder;
11
12 6
class TeacherRepository extends AbstractTransactionalRepository implements TeacherRepositoryInterface {
13 6
14 6
    private function createDefaultQueryBuilder(): QueryBuilder {
15 6
        $qb = $this->em->createQueryBuilder()
16 6
            ->select(['t', 's', 'g', 'tt'])
17 6
            ->from(Teacher::class, 't')
18 6
            ->leftJoin('t.subjects', 's')
19 6
            ->leftJoin('t.grades', 'g')
20
            ->leftJoin('t.tags', 'tt')
21 6
            ->leftJoin('t.sections', 'sec')
22
            ->orderBy('t.acronym', 'asc');
23
24
        return $qb;
25
    }
26
27
    /**
28
     * @inheritDoc
29
     */
30
    public function findOneById(int $id): ?Teacher {
31
        $qb = $this->createDefaultQueryBuilder();
32
33
        $qb->where('t.id = :id')
34
            ->setParameter('id', $id)
35
            ->setMaxResults(1);
36
37
        return $qb->getQuery()->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->getQuery()->getResult() also could return the type array<mixed,mixed>|integer which is incompatible with the return type mandated by App\Repository\TeacherRe...nterface::findOneById() of App\Entity\Teacher|null.
Loading history...
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public function findOneByUuid(string $uuid): ?Teacher {
44
        $qb = $this->createDefaultQueryBuilder();
45
46
        $qb->where('t.uuid = :uuid')
47
            ->setParameter('uuid', $uuid)
48
            ->setMaxResults(1);
49
50
        return $qb->getQuery()->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->getQuery()->getResult() also could return the type array<mixed,mixed>|integer which is incompatible with the return type mandated by App\Repository\TeacherRe...erface::findOneByUuid() of App\Entity\Teacher|null.
Loading history...
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function findOneByAcronym(string $acronym): ?Teacher {
57
        $qb = $this->createDefaultQueryBuilder();
58
59
        $qb->where('t.acronym = :acronym')
60
            ->setParameter('acronym', $acronym)
61
            ->setMaxResults(1);
62
63
        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\Teacher|null. Consider adding an additional type-check to rule them out.
Loading history...
64
    }
65
66 2
    /**
67 2
     * @inheritDoc
68
     */
69 2
    public function findOneByExternalId(string $externalId): ?Teacher {
70 2
        $qb = $this->createDefaultQueryBuilder();
71 2
72
        $qb->where('t.externalId = :externalId')
73 2
            ->setParameter('externalId', $externalId)
74
            ->setMaxResults(1) ;
75
76
        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\Teacher|null. Consider adding an additional type-check to rule them out.
Loading history...
77
    }
78
79 1
    public function findOneByEmailAddress(string $email): ?Teacher {
80 1
        $qb = $this->createDefaultQueryBuilder();
81
82
        $qb->where('t.email = :email')
83 1
            ->setParameter('email', $email)
84 1
            ->setMaxResults(1) ;
85
86 1
        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\Teacher|null. Consider adding an additional type-check to rule them out.
Loading history...
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92
    public function findAllByAcronym(array $acronyms): array {
93
        $qb = $this->createDefaultQueryBuilder();
94
95
        $qb
96
            ->where($qb->expr()->in('t.acronym', ':acronyms'))
97
            ->setParameter('acronyms', $acronyms);
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
    /**
103
     * @inheritDoc
104
     */
105 3
    public function findAllByExternalId(array $externalIds): array {
106 3
        $qb = $this->createDefaultQueryBuilder();
107 3
108 3
        $qb
109
            ->where($qb->expr()->in('t.externalId', ':externalIds'))
110
            ->setParameter('externalIds', $externalIds);
111
112
        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...
113
    }
114 2
115 2
    public function findAllByBirthday(DateTime $date): array {
116 2
        return $this->createDefaultQueryBuilder()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createDefa...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...
117 2
            ->where('t.birthday LIKE :date')
118
            ->andWhere('t.showBirthday = true')
119
            ->setParameter('date', $date->format('%-m-d'))
120
            ->getQuery()
121
            ->getResult();
122 2
    }
123 2
124 2
    /**
125 2
     * @inheritDoc
126
     */
127
    public function findAll() {
128
        return $this->createDefaultQueryBuilder()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createDefa...getQuery()->getResult() also could return the type integer which is incompatible with the return type mandated by App\Repository\TeacherRe...oryInterface::findAll() of App\Entity\Teacher[].
Loading history...
129
            ->getQuery()
130
            ->getResult();
131
    }
132
133
    /**
134
     * @inheritDoc
135
     */
136
    public function findAllBySection(Section $section): array {
137
        return $this->createDefaultQueryBuilder()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createDefa...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...
138
            ->andWhere('sec.id = :section')
139
            ->setParameter('section', $section->getId())
140
            ->getQuery()
141
            ->getResult();
142
    }
143
144
    /**
145
     * @inheritDoc
146
     */
147
    public function persist(Teacher $teacher): void {
148
        $this->em->persist($teacher);
149
        $this->flushIfNotInTransaction();
150
    }
151
152
    /**
153
     * @inheritDoc
154
     */
155
    public function remove(Teacher $teacher): void {
156
        $this->em->remove($teacher);
157
        $this->flushIfNotInTransaction();
158
    }
159
160
    /**
161
     * @inheritDoc
162
     */
163
    public function findAllBySubjectAndTag(?Subject $subject, ?TeacherTag $tag): array {
164
        $qb = $this->createDefaultQueryBuilder();
165
166
        $qbInner = $this->em->createQueryBuilder()
167
            ->select('tInner.id')
168
            ->from(Teacher::class, 'tInner')
169
            ->leftJoin('tInner.subjects', 'sInner')
170
            ->leftJoin('tInner.tags', 'tagsInner');
171
172
        if($subject !== null) {
173
            $qbInner
174
                ->andWhere('sInner.abbreviation = :subject');
175
            $qb->setParameter('subject', $subject->getAbbreviation());
176
        }
177
178
        if($tag !== null && $tag->getId() !== null) {
179
            $qbInner
180
                ->andWhere('tagsInner.id = :tag');
181
            $qb->setParameter('tag', $tag->getId());
182
        }
183
184
        $qb->where($qb->expr()->in('t.id', $qbInner->getDQL()));
185
186
        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...
187
    }
188
}