0
0
Fork 0
mirror of https://github.com/verdigado/organization_folders.git synced 2024-11-24 21:50:26 +01:00
organization_folders/lib/Service/ResourceMemberService.php

117 lines
3.1 KiB
PHP
Raw Normal View History

2024-10-31 17:29:15 +01:00
<?php
namespace OCA\OrganizationFolders\Service;
use Exception;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCA\OrganizationFolders\Errors\ResourceMemberNotFound;
use OCA\OrganizationFolders\Db\ResourceMember;
use OCA\OrganizationFolders\Db\ResourceMemberMapper;
use OCA\OrganizationFolders\Enum\ResourceMemberPermissionLevel;
use OCA\OrganizationFolders\Model\Principal;
2024-10-31 17:29:15 +01:00
class ResourceMemberService {
public function __construct(
2024-11-06 19:58:57 +01:00
protected ResourceMemberMapper $mapper,
protected ResourceService $resourceService,
protected OrganizationFolderService $organizationFolderService,
2024-10-31 17:29:15 +01:00
) {
}
/**
* @param int $resourceId
* @psalm-param int $resourceId
* @return array
* @psalm-return ResourceMember[]
*/
2024-10-31 17:29:15 +01:00
public function findAll(int $resourceId): array {
return $this->mapper->findAll($resourceId);
}
private function handleException(Exception $e): void {
if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) {
throw new ResourceMemberNotFound($e->getMessage());
} else {
throw $e;
}
}
public function find(int $id): ResourceMember {
try {
return $this->mapper->find($id);
} catch (Exception $e) {
$this->handleException($e);
}
}
public function create(
int $resourceId,
ResourceMemberPermissionLevel $permissionLevel,
Principal $principal,
2024-10-31 17:29:15 +01:00
): ResourceMember {
2024-11-06 19:58:57 +01:00
$resource = $this->resourceService->find($resourceId);
2024-10-31 17:29:15 +01:00
$member = new ResourceMember();
2024-11-06 19:58:57 +01:00
$member->setResourceId($resource->getId());
2024-10-31 17:29:15 +01:00
$member->setPermissionLevel($permissionLevel->value);
$member->setPrincipal($principal);
$creationTime = time();
$member->setCreatedTimestamp($creationTime);
$member->setLastUpdatedTimestamp($creationTime);
2024-10-31 17:29:15 +01:00
2024-11-06 19:58:57 +01:00
$member = $this->mapper->insert($member);
$this->organizationFolderService->applyPermissions($resource->getOrganizationFolderId());
return $member;
2024-10-31 17:29:15 +01:00
}
public function update(int $id, ?ResourceMemberPermissionLevel $permissionLevel = null, ?Principal $principal = null): ResourceMember {
2024-10-31 17:29:15 +01:00
try {
$member = $this->mapper->find($id);
if(isset($permissionLevel)) {
$member->setPermissionLevel($permissionLevel->value);
}
if(isset($principal)) {
$member->setPrincipal($principal);
2024-10-31 17:29:15 +01:00
}
if(count($member->getUpdatedFields()) > 0) {
$member->setLastUpdatedTimestamp(time());
$member = $this->mapper->update($member);
}
2024-11-06 19:58:57 +01:00
$resource = $this->resourceService->find($member->getResourceId());
$this->organizationFolderService->applyPermissions($resource->getOrganizationFolderId());
return $member;
2024-10-31 17:29:15 +01:00
} catch (Exception $e) {
$this->handleException($e);
}
}
public function delete(int $id): ResourceMember {
try {
$member = $this->mapper->find($id);
2024-11-06 19:58:57 +01:00
2024-10-31 17:29:15 +01:00
$this->mapper->delete($member);
2024-11-06 19:58:57 +01:00
$resource = $this->resourceService->find($member->getResourceId());
$this->organizationFolderService->applyPermissions($resource->getOrganizationFolderId());
2024-10-31 17:29:15 +01:00
return $member;
} catch (Exception $e) {
$this->handleException($e);
}
}
}