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

TuitionGradeRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 59
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A countByTuitionGradeCategory() 0 9 1
A removeForSection() 0 13 1
A persist() 0 3 1
A findAllByStudent() 0 9 1
A createDefaultQueryBuilder() 0 7 1
A findAllByTuition() 0 6 1
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
}