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

62 lines
1.5 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
{
2023-10-06 13:07:09 +03:00
$schemasArray = [];
if(file_exists(schemas_path())){
$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-06 13:07:09 +03:00
$schemasCollection = (new Collection($schemasArray))->map([$schemaService, 'fromArray']);
2023-10-02 23:10:49 +03:00
$channel = new Channel(
2023-10-06 13:07:09 +03:00
name: config("lucent.name") ?? "",
url: rtrim( config("lucent.url") ?? "", "/"),
previewTarget: rtrim( config("lucent.previewTarget") ?? "", "/"),
generateCommand: config("lucent.generateCommand") ?? "",
2023-10-02 23:10:49 +03:00
schemas: $schemasCollection,
2023-10-06 13:07:09 +03:00
imageFilters: config("lucent.imageFilters") ?? [],
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);
}
}