Passed
Push — master ( 862b31...3c96ea )
by Marcel
05:54
created

TuitionGradeRepository::removeForSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 13
rs 9.9666
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\Section;
6
use App\Entity\Student;
7
use App\Entity\Tuition;
8
use App\Entity\TuitionGrade;
9
use App\Entity\TuitionGradeCategory;
10
use Doctrine\ORM\QueryBuilder;
11
12
class TuitionGradeRepository extends AbstractTransactionalRepository implements TuitionGradeRepositoryInterface {
13
14
    private function createDefaultQueryBuilder(): QueryBuilder {
15
        return $this->em->createQueryBuilder()
16
            ->select(['g', 't', 's', 'c'])
17
            ->from(TuitionGrade::class, 'g')
18
            ->leftJoin('g.category', 'c')
19
            ->leftJoin('g.student', 's')
20
            ->leftJoin('g.tuition', 't');
21
    }
22
23
    public function findAllByTuition(Tuition $tuition): array {
24
        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...
25
            ->where('t.id = :tuition')
26
            ->setParameter('tuition', $tuition->getId())
27
            ->getQuery()
28
            ->getResult();
29
    }
30
31
    public function findAllByStudent(Student $student, Section $section): array {
32
        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...
33
            ->leftJoin('t.section', 'sec')
34
            ->where('s.id = :student')
35
            ->andWhere('sec.id = :section')
36
            ->setParameter('student', $student->getId())
37
            ->setParameter('section', $section->getId())
38
            ->getQuery()
39
            ->getResult();
40
    }
41
42
    public function countByTuitionGradeCategory(TuitionGradeCategory $category): int {
43
        return $this->em->createQueryBuilder()
44
            ->select('COUNT(DISTINCT g.id)')
45
            ->from(TuitionGrade::class, 'g')
46
            ->leftJoin('g.category', 'c')
47
            ->where('c.id = :category')
48
            ->setParameter('category', $category->getId())
49
            ->getQuery()
50
            ->getSingleScalarResult();
51
    }
52
53
    public function persist(TuitionGrade $grade): void {
54
        $this->em->persist($grade);
55
        $this->flushIfNotInTransaction();
56
    }
57
58
    public function removeForSection(Section $section): int {
59
        $qb = $this->em->createQueryBuilder()
60
            ->delete(TuitionGrade::class, 'g');
61
62
        $qbInner = $this->em->createQueryBuilder()
63
            ->select('tInner.id')
64
            ->from(Tuition::class, 'tInner')
65
            ->where('tInner.section = :section');
66
67
        $qb->where($qb->expr()->in('g.tuition', $qbInner->getDQL()))
68
            ->setParameter('section', $section->getId());
69
70
        return $qb->getQuery()->execute();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->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
    }
72
}