Passed
Push — master ( a5432c...54cbff )
by Marcel
16:00
created

TuitionGradebookController::index()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 28
nc 15
nop 8
dl 0
loc 43
rs 8.5386
c 1
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace App\Controller;
4
5
use App\Book\Grade\GradeOverviewHelper;
6
use App\Book\Grade\GradePersister;
7
use App\Entity\Section;
8
use App\Entity\StudyGroupMembership;
9
use App\Entity\Tuition;
10
use App\Entity\User;
11
use App\Repository\TuitionRepositoryInterface;
12
use App\Settings\TuitionGradebookSettings;
13
use App\View\Filter\SectionFilter;
14
use App\View\Filter\StudentFilter;
15
use App\View\Filter\TuitionFilter;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\Routing\Annotation\Route;
19
20
#[Route('/book/gradebook')]
21
class TuitionGradebookController extends AbstractController {
22
23
    #[Route('', name: 'gradebook')]
24
    public function index(Request $request, TuitionFilter $tuitionFilter, StudentFilter $studentFilter,
25
                          SectionFilter $sectionFilter, TuitionRepositoryInterface $tuitionRepository,
26
                          GradeOverviewHelper $gradeOverviewHelper, TuitionGradebookSettings $gradebookSettings, GradePersister $gradePersister): Response {
27
        /** @var User $user */
28
        $user = $this->getUser();
29
        
30
        $sectionFilterView = $sectionFilter->handle($request->query->get('section'));
31
        $tuitionFilterView = $tuitionFilter->handle($request->query->get('tuition'), $sectionFilterView->getCurrentSection(), $user);
32
        $studentFilterView = $studentFilter->handle($request->query->get('student'), $sectionFilterView->getCurrentSection(), $user, true);
33
34
        $ownTuitions = $this->resolveOwnTuitions($sectionFilterView->getCurrentSection(), $user, $tuitionRepository);
35
36
        $overview = null;
37
38
        if($tuitionFilterView->getCurrentTuition() !== null) {
39
            $overview = $gradeOverviewHelper->computeOverviewForTuition($tuitionFilterView->getCurrentTuition());
40
        } else if($studentFilterView->getCurrentStudent() !== null) {
41
            $overview = $gradeOverviewHelper->computeOverviewForStudent($studentFilterView->getCurrentStudent(), $sectionFilterView->getCurrentSection());
0 ignored issues
show
Bug introduced by
It seems like $sectionFilterView->getCurrentSection() can also be of type null; however, parameter $section of App\Book\Grade\GradeOver...uteOverviewForStudent() does only seem to accept App\Entity\Section, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
            $overview = $gradeOverviewHelper->computeOverviewForStudent($studentFilterView->getCurrentStudent(), /** @scrutinizer ignore-type */ $sectionFilterView->getCurrentSection());
Loading history...
42
        }
43
44
        if($request->isMethod('POST')) {
45
            if($this->isCsrfTokenValid('gradebook', $request->request->get('_csrf_token')) !== true) {
46
                $this->addFlash('error', 'CSRF token invalid.');
47
            } else {
48
                if($tuitionFilterView->getCurrentTuition() !== null) {
49
                    $gradePersister->persist($tuitionFilterView->getCurrentTuition(), $overview, $request->request->all('grades'));
0 ignored issues
show
Bug introduced by
It seems like $overview can also be of type null; however, parameter $overview of App\Book\Grade\GradePersister::persist() does only seem to accept App\Book\Grade\GradeOverview, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
                    $gradePersister->persist($tuitionFilterView->getCurrentTuition(), /** @scrutinizer ignore-type */ $overview, $request->request->all('grades'));
Loading history...
50
                } else if($studentFilterView->getCurrentStudent() !== null) {
51
                    $gradePersister->persist($studentFilterView->getCurrentStudent(), $overview, $request->request->all('grades'));
52
                }
53
54
                $this->addFlash('success', 'book.grades.success');
55
                return $this->redirectToRequestReferer('gradebook');
56
            }
57
        }
58
59
        return $this->render('books/grades/overview.html.twig', [
60
            'sectionFilter' => $sectionFilterView,
61
            'tuitionFilter' => $tuitionFilterView,
62
            'studentFilter' => $studentFilterView,
63
            'ownTuitions' => $ownTuitions,
64
            'overview' => $overview,
65
            'key' => $gradebookSettings->getEncryptedMasterKey()
66
        ]);
67
    }
68
69
    /**
70
     * @return Tuition[]
71
     */
72
    private function resolveOwnTuitions(?Section $currentSection, User $user, TuitionRepositoryInterface $tuitionRepository): array {
73
        if($currentSection === null) {
74
            return [ ];
75
        }
76
77
        $tuitions = [ ];
78
79
        if ($user->isStudentOrParent()) {
80
            $tuitions = $tuitionRepository->findAllByStudents($user->getStudents()->toArray(), $currentSection);
81
        } else if ($user->isTeacher()) {
82
            $tuitions = $tuitionRepository->findAllByTeacher($user->getTeacher(), $currentSection);
0 ignored issues
show
Bug introduced by
It seems like $user->getTeacher() can also be of type null; however, parameter $teacher of App\Repository\TuitionRe...ace::findAllByTeacher() does only seem to accept App\Entity\Teacher, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
            $tuitions = $tuitionRepository->findAllByTeacher(/** @scrutinizer ignore-type */ $user->getTeacher(), $currentSection);
Loading history...
83
        }
84
85
        return array_filter($tuitions, fn(Tuition $tuition) => $tuition->getGradeCategories()->count() > 0);
86
    }
87
}