demo_organization_provider/lib/OrganizationProviders/DemoOrganizationProvider.php

98 lines
3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace OCA\DemoOrganizationProvider\OrganizationProviders;
use OCA\OrganizationFolders\Model\Organization;
use OCA\OrganizationFolders\Model\OrganizationRole;
use OCA\OrganizationFolders\Errors\OrganizationNotFound;
use OCA\OrganizationFolders\Errors\OrganizationRoleNotFound;
class DemoOrganizationProvider extends \OCA\OrganizationFolders\OrganizationProvider\OrganizationProvider {
protected $id = "demo";
private const DEMO_ORGANIZATIONS = [1, 2, 11, 12, 13, 21, 22];
/**
* @return Organization
* @throws OrganizationNotFound
*/
public function getOrganization(int $id): Organization {
if(in_array($id, self::DEMO_ORGANIZATIONS)) {
return new Organization($id, (string)$id);
} else {
throw new OrganizationNotFound("demo", $id);
}
}
/**
* @return Organization[]
*/
public function getSubOrganizations(?int $parentOrganizationId = null): array {
if(is_null($parentOrganizationId)) {
return [
new Organization(1, "1"),
new Organization(2, "2"),
];
} else if ($parentOrganizationId === 1) {
return [
new Organization(11, "11"),
new Organization(12, "12"),
new Organization(13, "13"),
];
} else if ($parentOrganizationId === 2) {
return [
new Organization(21, "21"),
new Organization(22, "22"),
];
} else {
return [];
}
}
/**
* @return OrganizationRole
* @throws OrganizationRoleNotFound
*/
public function getRole(string $id): OrganizationRole {
[$organizationIdString, $roleNumberString] = explode("_", $id, 2);
$organizationId = (int)$organizationIdString;
$roleNumber = (int)$roleNumberString;
if(in_array($organizationId, self::DEMO_ORGANIZATIONS)) {
if($roleNumber >= 1 && $roleNumber <= 3) {
return new OrganizationRole(
id: $id,
organizationId: $organizationId,
friendlyName: "Role " . $roleNumber . " of org " . $organizationId,
membersGroup: $id
);
}
}
throw new OrganizationRoleNotFound("demo", $id);
}
/**
* @return OrganizationRole[]
*/
public function getRolesOfOrganization(int $organizationId): array {
if(in_array($organizationId, self::DEMO_ORGANIZATIONS)) {
$result = [];
for($i = 1; $i <= 3; $i++) {
$result[] = new OrganizationRole(
id: $organizationId . "_" . $i,
organizationId: $organizationId,
friendlyName: "Role " . $i . " of org " . $organizationId,
membersGroup: $organizationId . "_" . $i
);
}
return $result;
} else {
return [];
}
}
}