TeachersFilter   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 49
ccs 0
cts 25
cp 0
rs 10
c 1
b 0
f 0
wmc 15

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
C handle() 0 43 14
1
<?php
2
3
namespace App\View\Filter;
4
5
use App\Entity\Section;
6
use App\Entity\Teacher;
7
use App\Entity\User;
8
use App\Entity\UserType;
9
use App\Grouping\Grouper;
10
use App\Repository\SectionRepositoryInterface;
11
use App\Repository\TeacherRepositoryInterface;
12
use App\Settings\GeneralSettings;
13
use App\Sorting\Sorter;
14
use App\Sorting\TeacherStrategy;
15
use App\Utils\ArrayUtils;
16
17
class TeachersFilter {
18
19
    public function __construct(private Sorter $sorter, private TeacherRepositoryInterface $teacherRepository, private readonly GeneralSettings $generalSettings, private readonly SectionRepositoryInterface $sectionRepository)
20
    {
21
    }
22
23
    public function handle(?array $teacherUuids, ?Section $section, User $user, bool $setDefaultTeacher): TeachersFilterView {
24
        if($teacherUuids === null) {
0 ignored issues
show
introduced by
The condition $teacherUuids === null is always false.
Loading history...
25
            $teacherUuids = [ ];
26
        }
27
28
        $isStudentOrParent = $user->isStudentOrParent();
29
        $teachers = [ ];
30
31
        if($isStudentOrParent !== true && $section !== null) {
32
            $teachers = $this->teacherRepository->findAllBySection($section);
33
34
            if(count($teachers) === 0 && $this->generalSettings->getCurrentSectionId() !== null && $section->getId() !== $this->generalSettings->getCurrentSectionId()) {
35
                $section = $this->sectionRepository->findOneById($this->generalSettings->getCurrentSectionId());
36
37
                if($section !== null) {
38
                    $teachers = $this->teacherRepository->findAllBySection($section);
39
                }
40
            }
41
        }
42
43
        $teachers = ArrayUtils::createArrayWithKeys(
44
            $teachers,
45
            fn(Teacher $teacher) => (string)$teacher->getUuid()
46
        );
47
48
        $fallbackTeacher = $setDefaultTeacher ? $user->getTeacher() : null;
49
50
        $currentTeachers = [ ];
51
52
        /** @var Teacher $teacher */
53
        foreach($teachers as $teacher) {
54
            if(in_array((string)$teacher->getUuid(), $teacherUuids)) {
55
                $currentTeachers[] = $teacher;
56
            }
57
        }
58
59
        if(count($currentTeachers) === 0 && $setDefaultTeacher === true && $fallbackTeacher !== null) {
60
            $currentTeachers[] = $fallbackTeacher;
61
        }
62
63
        $this->sorter->sort($teachers, TeacherStrategy::class);
64
65
        return new TeachersFilterView($teachers, $currentTeachers);
66
    }
67
}