|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CSlant\LaravelTelegramGitNotifier\Services; |
| 4 | + |
| 5 | +use CSlant\LaravelTelegramGitNotifier\Traits\Markup; |
| 6 | +use CSlant\TelegramGitNotifier\Bot; |
| 7 | +use CSlant\TelegramGitNotifier\Constants\SettingConstant; |
| 8 | +use CSlant\TelegramGitNotifier\Exceptions\BotException; |
| 9 | +use CSlant\TelegramGitNotifier\Exceptions\CallbackException; |
| 10 | +use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException; |
| 11 | +use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException; |
| 12 | + |
| 13 | +class CallbackService |
| 14 | +{ |
| 15 | + use Markup; |
| 16 | + |
| 17 | + private Bot $bot; |
| 18 | + |
| 19 | + protected string $viewNamespace = ''; |
| 20 | + |
| 21 | + public function __construct(Bot $bot) |
| 22 | + { |
| 23 | + $this->bot = $bot; |
| 24 | + $this->viewNamespace = config('telegram-git-notifier.view.namespace'); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Answer the back button. |
| 29 | + * |
| 30 | + * @param string $callback |
| 31 | + * @return void |
| 32 | + * |
| 33 | + * @throws MessageIsEmptyException |
| 34 | + * @throws BotException |
| 35 | + * @throws CallbackException |
| 36 | + */ |
| 37 | + public function answerBackButton(string $callback): void |
| 38 | + { |
| 39 | + $callback = str_replace(SettingConstant::SETTING_BACK, '', $callback); |
| 40 | + switch ($callback) { |
| 41 | + case 'settings': |
| 42 | + $view = view("$this->viewNamespace::tools.settings"); |
| 43 | + $markup = $this->bot->settingMarkup(); |
| 44 | + |
| 45 | + break; |
| 46 | + case 'settings.custom_events.github': |
| 47 | + $view = view("$this->viewNamespace::tools.custom_event", ['platform' => 'github']); |
| 48 | + $markup = $this->bot->eventMarkup(); |
| 49 | + |
| 50 | + break; |
| 51 | + case 'settings.custom_events.gitlab': |
| 52 | + $view = view("$this->viewNamespace::tools.custom_event", ['platform' => 'gitlab']); |
| 53 | + $markup = $this->bot->eventMarkup(null, 'gitlab'); |
| 54 | + |
| 55 | + break; |
| 56 | + case 'menu': |
| 57 | + $view = view("$this->viewNamespace::tools.menu"); |
| 58 | + $markup = $this->menuMarkup($this->bot->telegram); |
| 59 | + |
| 60 | + break; |
| 61 | + default: |
| 62 | + $this->bot->answerCallbackQuery(__('tg-notifier::app.unknown_callback')); |
| 63 | + |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + $this->bot->editMessageText($view, [ |
| 68 | + 'reply_markup' => $markup, |
| 69 | + ]); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @return void |
| 74 | + * |
| 75 | + * @throws MessageIsEmptyException |
| 76 | + * @throws InvalidViewTemplateException |
| 77 | + * @throws BotException|CallbackException |
| 78 | + */ |
| 79 | + public function handle(): void |
| 80 | + { |
| 81 | + $callback = $this->bot->telegram->Callback_Data(); |
| 82 | + |
| 83 | + if (str_contains($callback, SettingConstant::SETTING_CUSTOM_EVENTS)) { |
| 84 | + $this->bot->eventHandle($callback); |
| 85 | + |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + if (str_contains($callback, SettingConstant::SETTING_BACK)) { |
| 90 | + $this->answerBackButton($callback); |
| 91 | + |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + $callback = str_replace(SettingConstant::SETTING_PREFIX, '', $callback); |
| 96 | + |
| 97 | + $settings = $this->bot->setting->getSettings(); |
| 98 | + if (array_key_exists($callback, $settings) |
| 99 | + && $this->bot->setting->updateSetting( |
| 100 | + $callback, |
| 101 | + !$settings[$callback] |
| 102 | + ) |
| 103 | + ) { |
| 104 | + $this->bot->editMessageReplyMarkup([ |
| 105 | + 'reply_markup' => $this->bot->settingMarkup(), |
| 106 | + ]); |
| 107 | + } else { |
| 108 | + $this->bot->answerCallbackQuery(__('tg-notifier::app.unknown_callback')); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments