Files
lucent-laravel/src/Channel/ChannelService.php
T

55 lines
1.3 KiB
PHP
Raw Normal View History

2023-10-02 23:10:49 +03:00
<?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
{
$configJson = file_get_contents(storage_path("lucent.config.json"));
$configArray = json_decode($configJson, true);
$schemaService = new SchemaService();
$schemasCollection = (new Collection($configArray["schemas"]))->map([$schemaService, 'fromArray']);
$channel = new Channel(
name: $configArray["name"],
url: rtrim($configArray["url"], "/"),
schemas: $schemasCollection,
imageFilters: $configArray["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);
}
}