init
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user