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