2023-10-02 23:10:49 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Lucent\Channel;
|
|
|
|
|
|
|
|
|
|
|
2024-08-24 18:51:36 +03:00
|
|
|
use Lucent\Channel\Data\UserCommand;
|
2023-10-02 23:10:49 +03:00
|
|
|
use Lucent\Primitive\Collection;
|
|
|
|
|
use Lucent\Schema\Schema;
|
|
|
|
|
use Lucent\Schema\SchemaService;
|
|
|
|
|
use PhpOption\Option;
|
|
|
|
|
|
|
|
|
|
final class ChannelService
|
|
|
|
|
{
|
|
|
|
|
public Channel $channel;
|
|
|
|
|
|
|
|
|
|
private function __construct(
|
2024-08-23 20:58:45 +03:00
|
|
|
public SchemaService $schemaService,
|
2023-10-02 23:10:49 +03:00
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function fromConfig(): ChannelService
|
|
|
|
|
{
|
2023-10-06 13:07:09 +03:00
|
|
|
$schemasArray = [];
|
2023-10-17 22:57:25 +03:00
|
|
|
if (file_exists(schemas_path())) {
|
2023-10-06 13:07:09 +03:00
|
|
|
$schemasJson = file_get_contents(schemas_path());
|
|
|
|
|
$schemasArray = json_decode($schemasJson, true);
|
2023-10-04 13:32:30 +03:00
|
|
|
}
|
2023-10-02 23:10:49 +03:00
|
|
|
$schemaService = new SchemaService();
|
2023-10-17 22:57:25 +03:00
|
|
|
$schemasCollection = (new Collection($schemasArray["schemas"] ?? []))->map([$schemaService, 'fromArray']);
|
2023-10-02 23:10:49 +03:00
|
|
|
|
2024-08-24 18:51:36 +03:00
|
|
|
$userCommands = [];
|
|
|
|
|
foreach (config("lucent.commands") as $signature => $desc) {
|
|
|
|
|
$userCommands[] = new UserCommand($desc, $signature);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 23:10:49 +03:00
|
|
|
$channel = new Channel(
|
2023-10-06 13:07:09 +03:00
|
|
|
name: config("lucent.name") ?? "",
|
2023-10-17 22:57:25 +03:00
|
|
|
url: rtrim(config("lucent.url") ?? "", "/"),
|
|
|
|
|
previewTarget: rtrim(config("lucent.previewTarget") ?? "", "/"),
|
2024-08-24 18:51:36 +03:00
|
|
|
commands: Collection::make($userCommands),
|
2023-10-02 23:10:49 +03:00
|
|
|
schemas: $schemasCollection,
|
2023-10-06 13:07:09 +03:00
|
|
|
imageFilters: config("lucent.imageFilters") ?? [],
|
2023-10-17 22:57:25 +03:00
|
|
|
roles: $schemasArray["roles"] ?? []
|
2023-10-02 23:10:49 +03:00
|
|
|
);
|
2023-10-17 22:57:25 +03:00
|
|
|
|
2023-10-02 23:10:49 +03:00
|
|
|
$channelService = new ChannelService($schemaService);
|
|
|
|
|
$channelService->channel = $channel;
|
|
|
|
|
return $channelService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return Option<Schema>
|
|
|
|
|
*/
|
|
|
|
|
public function getSchema(string $name): Option
|
|
|
|
|
{
|
|
|
|
|
$schema = $this->channel->schemas->firstWhere("name", $name);
|
|
|
|
|
if (empty($schema)) {
|
|
|
|
|
return none();
|
|
|
|
|
}
|
|
|
|
|
return some($schema);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-17 22:57:25 +03:00
|
|
|
/**
|
|
|
|
|
* @param array<string> $roles
|
|
|
|
|
* @return array<string>
|
|
|
|
|
*/
|
|
|
|
|
public function schemasReadableByRoles(array $roles): array
|
|
|
|
|
{
|
2024-08-24 18:51:36 +03:00
|
|
|
$schemasAllRead = $this->channel->schemas->filter(fn(Schema $schema) => empty($schema->read))->values()->pluck("name");
|
2023-10-23 18:05:06 +03:00
|
|
|
$schemasCanRead = $this->channel->schemas->filter(fn(Schema $schema) => count(array_intersect($schema->read ?? [], $roles)) > 0)->values()->pluck("name");
|
|
|
|
|
$schemasCanWrite = $this->channel->schemas->filter(fn(Schema $schema) => count(array_intersect($schema->write ?? [], $roles)) > 0)->values()->pluck("name");
|
2023-10-17 22:57:25 +03:00
|
|
|
return $schemasAllRead->merge($schemasCanRead)->merge($schemasCanWrite)->unique()->values()->toArray();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array<string> $roles
|
|
|
|
|
* @return array<string>
|
|
|
|
|
*/
|
|
|
|
|
public function schemasWritableByRoles(array $roles): array
|
|
|
|
|
{
|
2024-08-24 18:51:36 +03:00
|
|
|
$schemasAllRead = $this->channel->schemas->filter(fn(Schema $schema) => empty($schema->write ?? []))->values()->pluck("name");
|
|
|
|
|
$schemasCanWrite = $this->channel->schemas->filter(fn(Schema $schema) => count(array_intersect($schema->write ?? [], $roles)) > 0)->values()->pluck("name");
|
2023-10-17 22:57:25 +03:00
|
|
|
return $schemasAllRead->merge($schemasCanWrite)->unique()->values()->toArray();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 23:10:49 +03:00
|
|
|
}
|