|
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
|
|
|
} |