-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathRestrictionService.php
More file actions
125 lines (97 loc) · 3.4 KB
/
RestrictionService.php
File metadata and controls
125 lines (97 loc) · 3.4 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace OCA\BigBlueButton\Service;
use Exception;
use OCA\BigBlueButton\Db\Restriction;
use OCA\BigBlueButton\Db\RestrictionMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
class RestrictionService {
/** @var RestrictionMapper */
private $mapper;
public function __construct(RestrictionMapper $mapper) {
$this->mapper = $mapper;
}
public function findAll(): array {
return $this->mapper->findAll();
}
public function existsByGroupId(string $groupId): bool {
try {
$this->mapper->findByGroupId($groupId);
return true;
} catch (DoesNotExistException $e) {
return false;
}
}
public function findByGroupIds(array $groupIds): Restriction {
$restrictions = $this->mapper->findByGroupIds($groupIds);
try {
$restriction = $this->mapper->findByGroupId(Restriction::ALL_ID);
} catch (DoesNotExistException $e) {
$restriction = new Restriction();
}
$roomTypes = \json_decode($restriction->getRoomTypes());
foreach ($restrictions as $r) {
if ($restriction->getMaxRooms() > -1 && ($r->getMaxRooms() === -1 || $restriction->getMaxRooms() < $r->getMaxRooms())) {
$restriction->setMaxRooms($r->getMaxRooms());
}
$rRoomTypes = \json_decode($r->getRoomTypes());
$roomTypes = array_intersect($roomTypes, $rRoomTypes);
if ($restriction->getMaxParticipants() > -1 && ($r->getMaxParticipants() === -1 || $restriction->getMaxParticipants() < $r->getMaxParticipants())) {
$restriction->setMaxParticipants($r->getMaxParticipants());
}
if (!$restriction->getAllowRecording() && $r->getAllowRecording()) {
$restriction->setAllowRecording($r->getAllowRecording());
}
if (!$restriction->getallowLogoutURL() && $r->getallowLogoutURL()) {
$restriction->setallowLogoutURL($r->getallowLogoutURL());
}
}
$restriction->setId(0);
$restriction->setGroupId('__cumulative');
$restriction->setRoomTypes(\json_encode(\array_values($roomTypes)));
return $restriction;
}
public function find(int $id): Restriction {
try {
return $this->mapper->find($id);
} catch (Exception $e) {
$this->handleException($e);
}
}
public function create(string $groupId): Restriction {
$restriction = new Restriction();
$restriction->setGroupId($groupId);
return $this->mapper->insert($restriction);
}
public function update(int $id, string $groupId, int $maxRooms, array $roomTypes, int $maxParticipants, bool $allowRecording, bool $allowLogoutURL): Restriction {
try {
$restriction = $this->mapper->find($id);
$restriction->setGroupId($groupId);
$restriction->setMaxRooms(\max($maxRooms, -1));
$restriction->setRoomTypes(\json_encode($roomTypes));
$restriction->setMaxParticipants(\max($maxParticipants, -1));
$restriction->setAllowRecording($allowRecording);
$restriction->setallowLogoutURL($allowLogoutURL);
return $this->mapper->update($restriction);
} catch (Exception $e) {
$this->handleException($e);
}
}
public function delete(int $id): Restriction {
try {
$restriction = $this->mapper->find($id);
$this->mapper->delete($restriction);
return $restriction;
} catch (Exception $e) {
$this->handleException($e);
}
}
private function handleException(Exception $e): void {
if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) {
throw new RestrictionNotFound($e->getMessage());
} else {
throw $e;
}
}
}