refactor: standardize design
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -7,20 +7,21 @@ declare(strict_types=1);
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\FileProviderLocal\Providers;
|
||||
namespace KTXM\ProviderLocalDocuments\Providers;
|
||||
|
||||
use DI\Attribute\Inject;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use KTXF\Files\Provider\IProviderBase;
|
||||
use KTXF\Files\Service\IServiceBase;
|
||||
use KTXF\Resource\Provider\ProviderInterface;
|
||||
use KTXM\FileProviderLocal\Providers\Personal\PersonalService;
|
||||
use KTXF\Resource\Documents\Provider\ProviderBaseInterface;
|
||||
use KTXF\Resource\Documents\Service\ServiceBaseInterface;
|
||||
use KTXM\ProviderLocalDocuments\Module;
|
||||
use KTXM\ProviderLocalDocuments\Providers\Personal\PersonalService;
|
||||
|
||||
class Provider implements IProviderBase, ProviderInterface {
|
||||
class Provider implements ProviderBaseInterface {
|
||||
|
||||
protected const PROVIDER_ID = 'default';
|
||||
protected const PROVIDER_LABEL = 'Default File Provider';
|
||||
protected const PROVIDER_DESCRIPTION = 'Provides local file storage';
|
||||
public const JSON_TYPE = ProviderBaseInterface::JSON_TYPE;
|
||||
public const PROVIDER_IDENTIFIER = 'default';
|
||||
protected const PROVIDER_LABEL = Module::MODULE_LABEL;
|
||||
protected const PROVIDER_DESCRIPTION = Module::MODULE_DESCRIPTION;
|
||||
protected const PROVIDER_ICON = 'folder';
|
||||
|
||||
protected string $storeLocation = '/tmp/ktrix';
|
||||
@@ -30,8 +31,6 @@ class Provider implements IProviderBase, ProviderInterface {
|
||||
self::CAPABILITY_SERVICE_FETCH => true,
|
||||
self::CAPABILITY_SERVICE_EXTANT => true,
|
||||
];
|
||||
private ?array $servicesCache = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly ContainerInterface $container,
|
||||
#[Inject('rootDir')] private readonly string $rootDir,
|
||||
@@ -39,176 +38,94 @@ class Provider implements IProviderBase, ProviderInterface {
|
||||
$this->storeLocation = $this->rootDir . '/storage/';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function type(): string {
|
||||
return ProviderInterface::TYPE_FILES;
|
||||
}
|
||||
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,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function identifier(): string {
|
||||
return self::PROVIDER_ID;
|
||||
}
|
||||
public function jsonDeserialize(array|string $data): static
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function description(): string {
|
||||
return self::PROVIDER_DESCRIPTION;
|
||||
}
|
||||
public function type(): string
|
||||
{
|
||||
return self::TYPE_CHRONO;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function icon(): string {
|
||||
return self::PROVIDER_ICON;
|
||||
}
|
||||
public function identifier(): string
|
||||
{
|
||||
return self::PROVIDER_IDENTIFIER;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): mixed {
|
||||
return $this->toJson();
|
||||
}
|
||||
public function label(): string
|
||||
{
|
||||
return self::PROVIDER_LABEL;
|
||||
}
|
||||
|
||||
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 description(): string
|
||||
{
|
||||
return self::PROVIDER_DESCRIPTION;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
public function icon(): string
|
||||
{
|
||||
return self::PROVIDER_ICON;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all supported capabilities
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function capabilities(): array {
|
||||
return $this->providerAbilities;
|
||||
}
|
||||
public function capable(string $value): bool
|
||||
{
|
||||
return !empty($this->providerAbilities[$value]);
|
||||
}
|
||||
|
||||
/**
|
||||
* An arbitrary unique text string identifying this provider
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function id(): string {
|
||||
return self::PROVIDER_ID;
|
||||
}
|
||||
public function capabilities(): array
|
||||
{
|
||||
return $this->providerAbilities;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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', '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, $this->storeLocation . "$tenantId/$userId");
|
||||
$service->initialize($tenantId, $userId, $this->storeLocation . "$tenantId/$userId");
|
||||
return $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if any services are configured 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'];
|
||||
}
|
||||
// build services list
|
||||
$services = [];
|
||||
if (in_array('personal', $filter, true)) {
|
||||
$services['personal'] = $this->serviceInstancePersonal($tenantId, $userId);
|
||||
}
|
||||
return $services;
|
||||
}
|
||||
|
||||
public function serviceFetch(string $tenantId, string $userId, string|int $identifier): ?ServiceBaseInterface {
|
||||
|
||||
if ($identifier === 'personal') {
|
||||
return $this->serviceInstancePersonal($tenantId, $userId);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
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