Initial commit
This commit is contained in:
182
lib/Providers/Provider.php
Normal file
182
lib/Providers/Provider.php
Normal file
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\PeopleProviderLocal\Providers;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use KTXF\People\Provider\IProviderBase;
|
||||
use KTXF\People\Service\IServiceBase;
|
||||
use KTXF\Resource\Provider\ProviderInterface;
|
||||
use KTXM\PeopleProviderLocal\Providers\Personal\PersonalService;
|
||||
use KTXM\PeopleProviderLocal\Providers\Shared\SharedService;
|
||||
|
||||
class Provider implements IProviderBase, ProviderInterface {
|
||||
|
||||
protected const PROVIDER_ID = 'default';
|
||||
protected const PROVIDER_LABEL = 'Default People Provider';
|
||||
protected const PROVIDER_DESCRIPTION = 'Provides local people storage';
|
||||
protected const PROVIDER_ICON = 'user';
|
||||
|
||||
protected array $providerAbilities = [
|
||||
self::CAPABILITY_SERVICE_LIST => true,
|
||||
self::CAPABILITY_SERVICE_FETCH => true,
|
||||
self::CAPABILITY_SERVICE_EXTANT => true,
|
||||
];
|
||||
private ?array $servicesCache = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly ContainerInterface $container,
|
||||
) {}
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return $this->toJson();
|
||||
}
|
||||
|
||||
public function toJson(): array
|
||||
{
|
||||
return [
|
||||
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
|
||||
self::JSON_PROPERTY_ID => self::PROVIDER_ID,
|
||||
self::JSON_PROPERTY_LABEL => self::PROVIDER_LABEL,
|
||||
self::JSON_PROPERTY_CAPABILITIES => $this->providerAbilities,
|
||||
];
|
||||
}
|
||||
|
||||
public function type(): string
|
||||
{
|
||||
return self::TYPE_PEOPLE;
|
||||
}
|
||||
|
||||
public function identifier(): string
|
||||
{
|
||||
return self::PROVIDER_ID;
|
||||
}
|
||||
|
||||
public function label(): string {
|
||||
return self::PROVIDER_LABEL;
|
||||
}
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return self::PROVIDER_DESCRIPTION;
|
||||
}
|
||||
|
||||
public function icon(): string
|
||||
{
|
||||
return self::PROVIDER_ICON;
|
||||
}
|
||||
|
||||
public function capable(string $value): bool
|
||||
{
|
||||
return !empty($this->providerAbilities[$value]);
|
||||
}
|
||||
|
||||
public function capabilities(): array
|
||||
{
|
||||
return $this->providerAbilities;
|
||||
}
|
||||
|
||||
public function id(): string
|
||||
{
|
||||
return self::PROVIDER_ID;
|
||||
}
|
||||
|
||||
public function serviceList(string $tenantId, string $userId, array $filter = []): array
|
||||
{
|
||||
// if no filter is provided, return all services
|
||||
if ($filter === []) {
|
||||
$filter = ['personal', 'shared'];
|
||||
}
|
||||
// check if services are cached
|
||||
if (in_array('personal', $filter, true) && !isset($this->servicesCache[$userId]['personal'])) {
|
||||
$this->servicesCache[$userId]['personal'] = $this->serviceInstancePersonal($tenantId, $userId);
|
||||
}
|
||||
/*
|
||||
if (in_array('shared', $filter, true) && !isset($this->servicesCache[$userId]['shared'])) {
|
||||
$this->servicesCache[$userId]['shared'] = $this->serviceInstanceShared($tenantId, $userId);
|
||||
}
|
||||
*/
|
||||
// return requested services
|
||||
return array_intersect_key($this->servicesCache[$userId],array_flip($filter));
|
||||
}
|
||||
|
||||
/**
|
||||
* construct service object instance
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return PersonalService blank service instance
|
||||
*/
|
||||
protected function serviceInstancePersonal(string $tenantId, string $userId): PersonalService {
|
||||
$service = $this->container->get(PersonalService::class);
|
||||
$service->init($tenantId, $userId);
|
||||
return $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* construct service object instance
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return SharedService blank service instance
|
||||
*/
|
||||
protected function serviceInstanceShared(string $tenantId, string $userId): SharedService {
|
||||
$service = $this->container->get(SharedService::class);
|
||||
$service->init($tenantId, $userId);
|
||||
return $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if any services are configured for a specific user
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function serviceExtant(string $tenantId, string $userId, int|string ...$identifiers): array {
|
||||
$data = [];
|
||||
foreach ($identifiers as $id) {
|
||||
$data[$id] = match ($id) {
|
||||
'personal' => true,
|
||||
//'shared' => true,
|
||||
default => false,
|
||||
};
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a service with a specific identifier
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function serviceFetch(string $tenantId, string $userId, string|int $identifier): ?IServiceBase {
|
||||
|
||||
// check if services are cached
|
||||
if (isset($this->servicesCache[$userId][$identifier])) {
|
||||
return $this->servicesCache[$userId][$identifier];
|
||||
}
|
||||
// convert to service object
|
||||
if ($identifier === 'personal') {
|
||||
$this->servicesCache[$userId][$identifier] = $this->serviceInstancePersonal($tenantId, $userId);
|
||||
}
|
||||
/*
|
||||
if ($identifier === 'shared') {
|
||||
$this->servicesCache[$userId][$identifier] = $this->serviceInstanceShared($tenantId, $userId);
|
||||
}
|
||||
*/
|
||||
|
||||
return $this->servicesCache[$userId][$identifier];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user