-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGroupExams.php
More file actions
107 lines (95 loc) · 3.53 KB
/
GroupExams.php
File metadata and controls
107 lines (95 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace App\Model\Repository;
use App\Model\Entity\Group;
use App\Model\Entity\GroupExam;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use DateTime;
use Exception;
/**
* @extends BaseRepository<GroupExam>
*/
class GroupExams extends BaseRepository
{
public function __construct(EntityManagerInterface $em)
{
parent::__construct($em, GroupExam::class);
}
/**
* Fetch group exam entity by group-begin index. If not present, null is returned.
* @param Group $group
* @return GroupExam|null
*/
public function findPendingForGroup(Group $group): ?GroupExam
{
$begin = $group->getExamBegin();
if (!$begin || !$group->hasExamPeriodSet()) {
return null;
}
$exam = $this->findBy(["group" => $group, "begin" => $begin]);
if (count($exam) > 1) {
throw new Exception("Data corruption, there is more than one group exam starting at the same time.");
}
return $exam ? reset($exam) : null;
}
/**
* Internal helper that tries to find the exam or create it if not present.
* It returns null if creation failed due to race condition (expects retry by caller).
* @param Group $group
* @param DateTime $begin
* @param DateTime $end
* @param bool $strict
* @return GroupExam|null
*/
private function tryFindOrCreate(Group $group, DateTime $begin, DateTime $end, bool $strict): ?GroupExam
{
$exam = $this->findBy(["group" => $group, "begin" => $begin]);
if (count($exam) > 1) {
throw new Exception("Data corruption, there is more than one group exam starting at the same time.");
}
if (!$exam) {
try {
$this->em->getConnection()->executeQuery(
"INSERT INTO group_exam (group_id, begin, end, lock_strict) VALUES (:gid, :begin, :end, :strict)",
[
'gid' => $group->getId(),
'begin' => $begin->format('Y-m-d H:i:s'),
'end' => $end->format('Y-m-d H:i:s'),
'strict' => $strict ? 1 : 0
]
);
} catch (UniqueConstraintViolationException) {
// race condition, another transaction created the entity meanwhile
}
return null; // signal caller to retry
} else {
$exam = reset($exam);
}
return $exam;
}
/**
* Fetch group exam entity by group-begin index. If not present, new entity is created.
* @param Group $group
* @param DateTime|null $begin if null, exam begin from the group is taken
* @param DateTime|null $end if null, exam end from the group is taken
* @param bool|null $strict if null, examLockStrict value is taken
* @return GroupExam
*/
public function findOrCreate(
Group $group,
?DateTime $begin = null,
?DateTime $end = null,
?bool $strict = null
): GroupExam {
$begin = $begin ?? $group->getExamBegin();
$end = $end ?? $group->getExamEnd();
$strict = $strict === null ? $group->isExamLockStrict() : $strict;
for ($retries = 0; $retries < 3; $retries++) {
$exam = $this->tryFindOrCreate($group, $begin, $end, $strict);
if ($exam !== null) {
return $exam;
}
}
throw new Exception("Failed to find or create group exam after multiple attempts.");
}
}