StudyGroupsGradeStringConverter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 2
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
namespace App\Converter;
4
5
use App\Entity\Grade;
6
use App\Entity\StudyGroup;
7
use App\Entity\StudyGroupType;
8
use App\Sorting\Sorter;
9
use App\Sorting\StudyGroupStrategy;
10
use Doctrine\Common\Collections\ArrayCollection;
11
use Doctrine\Common\Collections\Collection;
12
use Symfony\Contracts\Translation\TranslatorInterface;
13
14
class StudyGroupsGradeStringConverter {
15
16
    public function __construct(private TranslatorInterface $translator, private Sorter $sorter, private GradesCollapsedArrayConverter $gradeConverter)
17
    {
18
    }
19
20 14
    /**
21 14
     * @param StudyGroup[]|iterable $studyGroups
22 14
     * @param Grade[]|iterable $onlyGrades
23 14
     */
24 14
    public function convert(iterable $studyGroups, bool $sort = false, iterable $onlyGrades = [ ]): string {
25
        if($studyGroups instanceof Collection) {
26
            $studyGroups = $studyGroups->toArray();
27
        }
28
29
        if($onlyGrades instanceof Collection) {
30
            $onlyGrades = $onlyGrades->toArray();
31
        }
32
33
        /** @var int[] $onlyGrades */
34
        $onlyGrades = array_map(fn(Grade $grade) => $grade->getId(), $onlyGrades);
35
36
        if($sort === true) {
37
            $this->sorter->sort($studyGroups, StudyGroupStrategy::class);
0 ignored issues
show
Bug introduced by
It seems like $studyGroups can also be of type iterable; however, parameter $array of App\Sorting\Sorter::sort() does only seem to accept array, 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

37
            $this->sorter->sort(/** @scrutinizer ignore-type */ $studyGroups, StudyGroupStrategy::class);
Loading history...
38
        }
39
40
        $output = [ ];
0 ignored issues
show
Unused Code introduced by
The assignment to $output is dead and can be removed.
Loading history...
41
42
        // First: Grades
43
        $grades = array_filter($studyGroups, fn(StudyGroup $group) => $group->getType() === StudyGroupType::Grade);
0 ignored issues
show
Bug introduced by
It seems like $studyGroups can also be of type iterable; however, parameter $array of array_filter() does only seem to accept array, 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

43
        $grades = array_filter(/** @scrutinizer ignore-type */ $studyGroups, fn(StudyGroup $group) => $group->getType() === StudyGroupType::Grade);
Loading history...
44
45
        $output = $this->gradeConverter->convert(
46
            array_map(fn(StudyGroup $group) => $group->getGrades()->first(), $grades)
47
        );
48
49
        // Second: Individual groups
50
        $studyGroups = array_filter($studyGroups, fn(StudyGroup $group) => $group->getType() !== StudyGroupType::Grade);
51
52
        $output += array_map(fn(StudyGroup $studyGroup) => $this->translator->trans('studygroup.string', [
53
            '%name%' => $studyGroup->getName(),
54
            '%grade%' => implode(', ', $this->gradeConverter->convert($studyGroup->getGrades()->filter(function(Grade $grade) use($onlyGrades) {
55
                if(empty($onlyGrades)) {
56
                    return true;
57
                }
58
59
                return in_array($grade->getId(), $onlyGrades);
60
            })->toArray()))
61
        ]), $studyGroups);
62
63
        return implode(', ', $output);
64
    }
65
}