Completed
Push — main ( 750cf9...cfb036 )
by Tan
28s queued 13s
created

CallbackService::handle()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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