200 lines
4.5 KiB
PHP
200 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace KTXM\ChronoProviderLocal\Providers;
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
use KTXF\Chrono\Provider\IProviderBase;
|
|
use KTXF\Chrono\Service\IServiceBase;
|
|
use KTXF\Resource\Provider\ProviderInterface;
|
|
use KTXM\ChronoProviderLocal\Providers\Personal\PersonalService;
|
|
|
|
class Provider implements IProviderBase, ProviderInterface {
|
|
|
|
protected const PROVIDER_ID = 'default';
|
|
protected const PROVIDER_LABEL = 'Default Chrono Provider';
|
|
protected const PROVIDER_DESCRIPTION = 'Provides local calendar/event storage';
|
|
protected const PROVIDER_ICON = 'calendar';
|
|
|
|
protected array $providerAbilities = [];
|
|
private ?array $servicesCache = [];
|
|
|
|
public function __construct(
|
|
private readonly ContainerInterface $container,
|
|
) {
|
|
$this->providerAbilities = [
|
|
self::CAPABILITY_SERVICE_LIST => true,
|
|
self::CAPABILITY_SERVICE_FETCH => true,
|
|
self::CAPABILITY_SERVICE_EXTANT => true,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function type(): string {
|
|
return ProviderInterface::TYPE_CHRONO;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function identifier(): string {
|
|
return self::PROVIDER_ID;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function description(): string {
|
|
return self::PROVIDER_DESCRIPTION;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function icon(): string {
|
|
return self::PROVIDER_ICON;
|
|
}
|
|
|
|
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,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Confirms if specific capability is supported
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @inheritdoc
|
|
*/
|
|
public function capable(string $value): bool {
|
|
if (isset($this->providerAbilities[$value])) {
|
|
return (bool)$this->providerAbilities[$value];
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Lists all supported capabilities
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @inheritdoc
|
|
*/
|
|
public function capabilities(): array {
|
|
return $this->providerAbilities;
|
|
}
|
|
|
|
/**
|
|
* An arbitrary unique text string identifying this provider
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @inheritdoc
|
|
*/
|
|
public function id(): string {
|
|
return self::PROVIDER_ID;
|
|
}
|
|
|
|
/**
|
|
* The localized human friendly name of this provider
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @inheritdoc
|
|
*/
|
|
public function label(): string {
|
|
return self::PROVIDER_LABEL;
|
|
}
|
|
|
|
/**
|
|
* Retrieve collection of services for a specific user
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @inheritdoc
|
|
*/
|
|
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));
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* 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,
|
|
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);
|
|
}
|
|
|
|
return $this->servicesCache[$userId][$identifier];
|
|
|
|
}
|
|
|
|
}
|