0
0
Fork 0
mirror of https://github.com/verdigado/organization_folders.git synced 2024-11-22 12:40:28 +01:00
organization_folders/lib/Service/ResourceService.php

150 lines
3.9 KiB
PHP
Raw Normal View History

2024-10-17 13:56:32 +02:00
<?php
namespace OCA\OrganizationFolders\Service;
use Exception;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCA\OrganizationFolders\Db\Resource;
use OCA\OrganizationFolders\Db\FolderResource;
use OCA\OrganizationFolders\Db\ResourceMapper;
use OCA\OrganizationFolders\Errors\InvalidResourceType;
use OCA\OrganizationFolders\Errors\ResourceNotFound;
2024-10-30 01:51:48 +01:00
use OCA\OrganizationFolders\Errors\ResourceNameNotUnique;
2024-10-17 13:56:32 +02:00
class ResourceService {
public function __construct(
private ResourceMapper $mapper
) {
}
2024-10-31 17:29:15 +01:00
public function findAll(int $organizationFolderId, int $parentResourceId = null) {
return $this->mapper->findAll($organizationFolderId, $parentResourceId);
2024-10-30 01:51:48 +01:00
}
2024-10-17 13:56:32 +02:00
private function handleException(Exception $e, int $id): void {
if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) {
throw new ResourceNotFound($id);
} else {
throw $e;
}
}
public function find(int $id): Resource {
try {
return $this->mapper->find($id);
} catch (Exception $e) {
$this->handleException($e, $id);
}
}
/* Use named arguments to call this function */
public function create(
string $type,
int $organizationFolderId,
2024-10-30 01:51:48 +01:00
string $name,
2024-10-17 13:56:32 +02:00
?int $parentResource = null,
bool $active = true,
?int $membersAclPermission = null,
?int $managersAclPermission = null,
?int $inheritedAclPermission = null,
) {
if($type === "folder") {
$resource = new FolderResource();
} else {
throw new InvalidResourceType($type);
}
2024-10-30 01:51:48 +01:00
if(!$this->mapper->existsWithName($organizationFolderId, $parentResource, $name)) {
2024-10-31 17:29:15 +01:00
$resource->setOrganizationFolderId($organizationFolderId);
2024-10-30 01:51:48 +01:00
$resource->setName($name);
$resource->setParentResource($parentResource);
$resource->setActive($active);
$resource->setLastUpdatedTimestamp(time());
2024-10-17 13:56:32 +02:00
2024-10-30 01:51:48 +01:00
if($type === "folder") {
if(isset($membersAclPermission, $managersAclPermission, $inheritedAclPermission)) {
$resource->setMembersAclPermission($membersAclPermission);
$resource->setManagersAclPermission($managersAclPermission);
$resource->setInheritedAclPermission($inheritedAclPermission);
} else {
throw new \InvalidArgumentException("Folder specific parameters must be included, when creating a resource of type folder");
}
2024-10-17 13:56:32 +02:00
}
2024-10-30 01:51:48 +01:00
$resource = $this->mapper->insert($resource);
2024-10-17 13:56:32 +02:00
2024-10-30 01:51:48 +01:00
return $resource;
} else {
throw new ResourceNameNotUnique();
}
2024-10-17 13:56:32 +02:00
}
/* Use named arguments to call this function */
public function update(
int $id,
2024-10-30 01:51:48 +01:00
?string $name = null,
2024-10-17 13:56:32 +02:00
?int $parentResource = null,
?bool $active = null,
?int $membersAclPermission = null,
?int $managersAclPermission = null,
?int $inheritedAclPermission = null,
): Resource {
$resource = $this->find($id);
if(isset($parentResource)) {
$resource->setParentResource($parentResource);
}
2024-10-30 01:51:48 +01:00
if(isset($name)) {
if($this->mapper->existsWithName($resource->getGroupFolderId(), $resource->getParentResource(), $name)) {
throw new ResourceNameNotUnique();
} else {
$resource->setName($name);
}
}
2024-10-17 13:56:32 +02:00
if(isset($active)) {
$resource->setActive($active);
}
if($resource->getType() === "folder") {
if(isset($membersAclPermission)) {
$resource->setMembersAclPermission($membersAclPermission);
}
if(isset($managersAclPermission)) {
$resource->setManagersAclPermission($managersAclPermission);
}
if(isset($inheritedAclPermission)) {
$resource->setInheritedAclPermission($inheritedAclPermission);
}
} else {
throw new InvalidResourceType($resource->getType());
}
if(count($resource->getUpdatedFields()) > 0) {
$resource->setLastUpdatedTimestamp(time());
}
return $this->mapper->update($resource);
}
public function delete(int $id): Resource {
try {
$resource = $this->mapper->find($id);
$this->mapper->delete($resource);
return $resource;
} catch (Exception $e) {
$this->handleException($e, $id);
}
}
}