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

GradePersister   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 46
rs 10
c 1
b 0
f 0
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
C persist() 0 43 12
1
<?php
2
3
namespace App\Book\Grade;
4
5
use App\Entity\Student;
6
use App\Entity\Tuition;
7
use App\Entity\TuitionGrade;
8
use App\Repository\StudentRepositoryInterface;
9
use App\Repository\TuitionGradeRepositoryInterface;
10
use App\Repository\TuitionRepositoryInterface;
11
use App\Security\Voter\TuitionGradeVoter;
12
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
13
14
class GradePersister {
15
    public function __construct(private readonly TuitionGradeRepositoryInterface $repository, private readonly AuthorizationCheckerInterface $authorizationChecker) { }
16
17
    public function persist(Tuition|Student $tuitionOrStudent, GradeOverview $overview, array $grades): void {
18
        $this->repository->beginTransaction();
19
20
        foreach($overview->getRows() as $row) {
21
            foreach($overview->getCategories() as $category) {
22
                $subject = $row->getTuitionOrStudent();
23
                $grade = $row->getGrade($category);
24
25
                if($grade === null) {
26
                    $grade = (new TuitionGrade())
27
                        ->setCategory($category);
28
29
                    if($tuitionOrStudent instanceof Tuition) {
30
                        $grade->setTuition($tuitionOrStudent);
31
                        $grade->setStudent($subject);
32
                    } else {
33
                        $grade->setStudent($tuitionOrStudent);
34
                        $grade->setTuition($subject);
35
                    }
36
37
                    if($this->authorizationChecker->isGranted(TuitionGradeVoter::New, $grade) !== true) {
38
                        continue;
39
                    }
40
                }
41
42
                if(isset($grades[$subject->getUuid()->toString()][$category->getUuid()->toString()])) {
43
                    $encryptedGrade = $grades[$subject->getUuid()->toString()][$category->getUuid()->toString()];
44
45
                    if(empty($encryptedGrade)) {
46
                        $grade->setEncryptedGrade(null);
47
                    } else {
48
                        $grade->setEncryptedGrade($encryptedGrade);
49
                    }
50
                }
51
52
                if(($grade->getId() === null && $this->authorizationChecker->isGranted(TuitionGradeVoter::New, $grade))
53
                   || ($grade->getId() !== null && $this->authorizationChecker->isGranted(TuitionGradeVoter::Edit, $grade))) {
54
                    $this->repository->persist($grade);
55
                }
56
            }
57
        }
58
59
        $this->repository->commit();
60
    }
61
}