2020-06-15 17:23:53 +02:00
|
|
|
<?php
|
2020-06-19 09:28:58 +02:00
|
|
|
|
2020-06-15 17:23:53 +02:00
|
|
|
namespace OCA\BigBlueButton\Service;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
|
|
|
use OCA\BigBlueButton\Db\RoomShare;
|
|
|
|
use OCA\BigBlueButton\Db\RoomShareMapper;
|
2020-09-18 14:46:11 +02:00
|
|
|
use OCA\BigBlueButton\Event\RoomShareCreatedEvent;
|
2020-09-23 12:33:09 +02:00
|
|
|
|
2020-09-18 14:46:11 +02:00
|
|
|
use OCA\BigBlueButton\Event\RoomShareDeletedEvent;
|
2020-09-23 12:33:09 +02:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
|
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2020-06-15 17:23:53 +02:00
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
class RoomShareService {
|
2020-06-15 17:23:53 +02:00
|
|
|
|
|
|
|
/** @var RoomShareMapper */
|
|
|
|
private $mapper;
|
|
|
|
|
2020-09-18 14:46:11 +02:00
|
|
|
/** @var IEventDispatcher */
|
|
|
|
private $eventDispatcher;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
RoomShareMapper $mapper,
|
|
|
|
IEventDispatcher $eventDispatcher) {
|
2020-06-15 17:23:53 +02:00
|
|
|
$this->mapper = $mapper;
|
2020-09-18 14:46:11 +02:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
2020-06-15 17:23:53 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
public function findAll(int $roomId): array {
|
2020-06-15 17:23:53 +02:00
|
|
|
return $this->mapper->findAll($roomId);
|
|
|
|
}
|
|
|
|
|
2020-09-24 11:05:51 +02:00
|
|
|
public function findByUserId(string $userId): array {
|
|
|
|
return $this->mapper->findByUserId($userId);
|
|
|
|
}
|
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
private function handleException(Exception $e): void {
|
2020-06-15 17:23:53 +02:00
|
|
|
if ($e instanceof DoesNotExistException ||
|
|
|
|
$e instanceof MultipleObjectsReturnedException) {
|
|
|
|
throw new RoomShareNotFound($e->getMessage());
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
public function find($id): RoomShare {
|
2020-06-15 17:23:53 +02:00
|
|
|
try {
|
|
|
|
return $this->mapper->find($id);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->handleException($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
public function create(int $roomId, int $shareType, string $shareWith, int $permission): RoomShare {
|
2020-06-16 16:54:50 +02:00
|
|
|
try {
|
|
|
|
$roomShare = $this->mapper->findByRoomAndEntity($roomId, $shareWith, $shareType);
|
2020-06-15 17:23:53 +02:00
|
|
|
|
2020-06-16 16:54:50 +02:00
|
|
|
return $this->update($roomShare->getId(), $roomId, $shareType, $shareWith, $permission);
|
|
|
|
} catch (DoesNotExistException $e) {
|
|
|
|
$roomShare = new RoomShare();
|
2020-06-15 17:23:53 +02:00
|
|
|
|
2020-06-16 16:54:50 +02:00
|
|
|
$roomShare->setRoomId($roomId);
|
|
|
|
$roomShare->setShareType($shareType);
|
|
|
|
$roomShare->setShareWith($shareWith);
|
|
|
|
$roomShare->setPermission($permission);
|
|
|
|
|
2020-09-18 14:46:11 +02:00
|
|
|
$createdRoomShare = $this->mapper->insert($roomShare);
|
|
|
|
|
|
|
|
$this->eventDispatcher->dispatch(RoomShareCreatedEvent::class, new RoomShareCreatedEvent($createdRoomShare));
|
|
|
|
|
|
|
|
return $createdRoomShare;
|
2020-06-16 16:54:50 +02:00
|
|
|
}
|
2020-06-15 17:23:53 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
public function update(int $id, int $roomId, int $shareType, string $shareWith, int $permission): RoomShare {
|
2020-06-15 17:23:53 +02:00
|
|
|
try {
|
|
|
|
$roomShare = $this->mapper->find($id);
|
|
|
|
|
|
|
|
$roomShare->setRoomId($roomId);
|
|
|
|
$roomShare->setShareType($shareType);
|
|
|
|
$roomShare->setShareWith($shareWith);
|
|
|
|
$roomShare->setPermission($permission);
|
|
|
|
|
|
|
|
return $this->mapper->update($roomShare);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->handleException($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 09:28:58 +02:00
|
|
|
public function delete(int $id): RoomShare {
|
2020-06-15 17:23:53 +02:00
|
|
|
try {
|
|
|
|
$roomShare = $this->mapper->find($id);
|
|
|
|
$this->mapper->delete($roomShare);
|
|
|
|
|
2020-09-18 14:46:11 +02:00
|
|
|
$this->eventDispatcher->dispatch(RoomShareDeletedEvent::class, new RoomShareDeletedEvent($roomShare));
|
|
|
|
|
2020-06-15 17:23:53 +02:00
|
|
|
return $roomShare;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->handleException($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|