137 lines
3.7 KiB
PHP
137 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace KTXM\ProviderLocalChrono\Providers;
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
use KTXF\Chrono\Provider\ProviderBaseInterface;
|
|
use KTXF\Chrono\Service\ServiceBaseInterface;
|
|
use KTXM\ProviderLocalChrono\Providers\Personal\PersonalService;
|
|
|
|
/**
|
|
* Local Storage Provider for Chrono
|
|
*/
|
|
class Provider implements ProviderBaseInterface
|
|
{
|
|
|
|
public const JSON_TYPE = ProviderBaseInterface::JSON_TYPE;
|
|
protected const PROVIDER_IDENTIFIER = 'default';
|
|
protected const PROVIDER_LABEL = 'Default Chrono Provider';
|
|
protected const PROVIDER_DESCRIPTION = 'Provides local calendar/event storage';
|
|
protected const PROVIDER_ICON = 'mdi-calendar';
|
|
|
|
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(): array
|
|
{
|
|
return [
|
|
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
|
|
self::JSON_PROPERTY_IDENTIFIER => self::PROVIDER_IDENTIFIER,
|
|
self::JSON_PROPERTY_LABEL => self::PROVIDER_LABEL,
|
|
self::JSON_PROPERTY_CAPABILITIES => $this->providerAbilities,
|
|
];
|
|
}
|
|
|
|
public function jsonDeserialize(array|string $data): static
|
|
{
|
|
return $this;
|
|
}
|
|
|
|
public function type(): string
|
|
{
|
|
return self::TYPE_MAIL;
|
|
}
|
|
|
|
public function identifier(): string
|
|
{
|
|
return self::PROVIDER_IDENTIFIER;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
protected function serviceInstancePersonal(string $tenantId, string $userId): PersonalService {
|
|
$service = $this->container->get(PersonalService::class);
|
|
$service->initialize($tenantId, $userId);
|
|
return $service;
|
|
}
|
|
|
|
public function serviceList(string $tenantId, string $userId, array $filter = []): array {
|
|
// if no filter is provided, return all services
|
|
if ($filter === []) {
|
|
$filter = ['personal'];
|
|
}
|
|
// check if services are cached
|
|
if (in_array('personal', $filter, true) && !isset($this->servicesCache[$userId]['personal'])) {
|
|
$this->servicesCache[$userId]['personal'] = $this->serviceInstancePersonal($tenantId, $userId);
|
|
}
|
|
// return requested services
|
|
return array_intersect_key($this->servicesCache[$userId],array_flip($filter));
|
|
}
|
|
|
|
public function serviceFetch(string $tenantId, string $userId, string|int $identifier): ?ServiceBaseInterface {
|
|
|
|
// 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);
|
|
}
|
|
|
|
return $this->servicesCache[$userId][$identifier];
|
|
|
|
}
|
|
|
|
public function serviceExtant(string $tenantId, string $userId, int|string ...$identifiers): array {
|
|
$data = [];
|
|
foreach ($identifiers as $id) {
|
|
$data[$id] = match ($id) {
|
|
'personal' => true,
|
|
default => false,
|
|
};
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
}
|