Skip to content

Commit

Permalink
Configuration codestyle. Readonly classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
luzrain committed Feb 28, 2024
1 parent c3b12ed commit 4c2f2b7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/TelegramBot/Controller/WebHookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;

final class WebHookController
final readonly class WebHookController
{
public function __construct(
private UpdateHandler $updateHandler,
Expand Down
10 changes: 5 additions & 5 deletions src/TelegramBot/UpdateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
use Luzrain\TelegramBotApi\Type\Update;
use Symfony\Component\DependencyInjection\ServiceLocator;

final class UpdateHandler
final readonly class UpdateHandler
{
public function __construct(
private ClientApi $client,
private ServiceLocator $serviceLocator,
array $controllersMap,
) {
foreach ($controllersMap as ['event' => $event, 'value' => $value, 'controller' => $controller]) {
$this->client->on($this->createClosure($event, $value, $controller));
foreach ($controllersMap as ['event' => $eventClass, 'value' => $value, 'controller' => $controller]) {
$this->client->on($this->createEvent($eventClass, $value, $controller));
}
}

Expand All @@ -34,13 +34,13 @@ public function handle(Update $update): Method|null
/**
* @param class-string<Event> $event
*/
private function createClosure(string $event, string $value, string $controller): Event
private function createEvent(string $event, string $value, string $controller): Event
{
return match ($event) {
Event\Command::class => new $event($value, function (object $update, string ...$params) use ($controller) {
return $this->runController($controller, $update, $params);
}),
Event\NamedCallbackQuery::class => new $event($value, function (object $update) use ($controller) {
Event\CallbackDataQuery::class => new $event($value, function (object $update) use ($controller) {
return $this->runController($controller, $update);
}),
default => new $event(function (object $update) use ($controller) {
Expand Down
21 changes: 10 additions & 11 deletions src/config/compilerpass.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,31 @@ public function process(ContainerBuilder $container): void
}

// Commands have the highest priority by default
usort($controllersMap, fn (array $a, array $b) => str_starts_with($a['value'], '/') ? -1 : 1);
\usort($controllersMap, fn (array $a, array $b) => \str_starts_with($a['value'], '/') ? -1 : 1);

// Sort by priority
usort($controllersMap, fn (array $a, array $b) => $b['priority'] <=> $a['priority']);
\usort($controllersMap, fn (array $a, array $b) => $b['priority'] <=> $a['priority']);

foreach ($controllersMap as $id => $row) {
unset($controllersMap[$id]['priority']);
}

$container
->register('telegram_bot.controllers_locator', ServiceLocator::class)
->setArguments([$this->referenceMap(array_keys($controllers))])
->addTag('container.service_locator')
;
->setArguments([$this->referenceMap(\array_keys($controllers))])
->addTag('container.service_locator');

$container
->register('telegram_bot.update_handler', UpdateHandler::class)
->setArgument('$client', new Reference('telegram_bot.client_api'))
->setArgument('$serviceLocator', new Reference('telegram_bot.controllers_locator'))
->setArgument('$controllersMap', $controllersMap)
;
->setArguments([
new Reference('telegram_bot.client_api'),
new Reference('telegram_bot.controllers_locator'),
$controllersMap,
]);

$container
->register('telegram_bot.command_metadata_provider', CommandMetadataProvider::class)
->setArgument('$controllersMap', $controllersMap)
;
->setArguments([$controllersMap]);
}

private function referenceMap(array $serviceClasses): array
Expand Down

0 comments on commit 4c2f2b7

Please sign in to comment.