Files
lucent-laravel/src/Channel/ChannelService.php
T
2023-10-06 13:07:09 +03:00

62 lines
1.5 KiB
PHP

<?php
namespace Lucent\Channel;
use Lucent\Primitive\Collection;
use Lucent\Schema\Schema;
use Lucent\Schema\SchemaService;
use PhpOption\Option;
final class ChannelService
{
public Channel $channel;
private function __construct(
public SchemaService $schemaService
)
{
}
public static function fromConfig(): ChannelService
{
$schemasArray = [];
if(file_exists(schemas_path())){
$schemasJson = file_get_contents(schemas_path());
$schemasArray = json_decode($schemasJson, true);
}
$schemaService = new SchemaService();
$schemasCollection = (new Collection($schemasArray))->map([$schemaService, 'fromArray']);
$channel = new Channel(
name: config("lucent.name") ?? "",
url: rtrim( config("lucent.url") ?? "", "/"),
previewTarget: rtrim( config("lucent.previewTarget") ?? "", "/"),
generateCommand: config("lucent.generateCommand") ?? "",
schemas: $schemasCollection,
imageFilters: config("lucent.imageFilters") ?? [],
);
$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);
}
}