chore: standardize chrono provider
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -10,126 +10,90 @@ declare(strict_types=1);
|
||||
namespace KTXM\ProviderLocalChrono\Providers;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use KTXF\Chrono\Provider\IProviderBase;
|
||||
use KTXF\Chrono\Service\IServiceBase;
|
||||
use KTXF\Resource\Provider\ProviderInterface;
|
||||
use KTXF\Chrono\Provider\ProviderBaseInterface;
|
||||
use KTXF\Chrono\Service\ServiceBaseInterface;
|
||||
use KTXM\ProviderLocalChrono\Providers\Personal\PersonalService;
|
||||
|
||||
class Provider implements IProviderBase, ProviderInterface {
|
||||
/**
|
||||
* Local Storage Provider for Chrono
|
||||
*/
|
||||
class Provider implements ProviderBaseInterface
|
||||
{
|
||||
|
||||
protected const PROVIDER_ID = 'default';
|
||||
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 = 'calendar';
|
||||
protected const PROVIDER_ICON = 'mdi-calendar';
|
||||
|
||||
protected array $providerAbilities = [
|
||||
self::CAPABILITY_SERVICE_LIST => true,
|
||||
self::CAPABILITY_SERVICE_FETCH => true,
|
||||
self::CAPABILITY_SERVICE_EXTANT => true,
|
||||
];
|
||||
|
||||
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,
|
||||
];
|
||||
) {}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 === []) {
|
||||
@@ -143,45 +107,7 @@ class Provider implements IProviderBase, ProviderInterface {
|
||||
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 {
|
||||
public function serviceFetch(string $tenantId, string $userId, string|int $identifier): ?ServiceBaseInterface {
|
||||
|
||||
// check if services are cached
|
||||
if (isset($this->servicesCache[$userId][$identifier])) {
|
||||
@@ -196,4 +122,15 @@ class Provider implements IProviderBase, ProviderInterface {
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user