CommandService   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 12
eloc 53
c 3
b 1
f 1
dl 0
loc 104
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 33 9
A sendStartMessage() 0 9 1
A __construct() 0 4 1
A menuCommands() 0 27 1
1
<?php
2
3
namespace CSlant\LaravelTelegramGitNotifier\Services;
4
5
use CSlant\LaravelTelegramGitNotifier\Traits\Markup;
6
use CSlant\TelegramGitNotifier\Bot;
7
use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException;
8
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
9
10
class CommandService
11
{
12
    use Markup;
13
14
    private Bot $bot;
15
16
    protected string $viewNamespace = '';
17
18
    public function __construct(Bot $bot)
19
    {
20
        $this->bot = $bot;
21
        $this->viewNamespace = config('telegram-git-notifier.view.namespace');
22
    }
23
24
    /**
25
     * @param  Bot  $bot
26
     * @return void
27
     *
28
     * @throws EntryNotFoundException
29
     */
30
    public function sendStartMessage(Bot $bot): void
31
    {
32
        $reply = view(
33
            "$this->viewNamespace::tools.start",
34
            ['first_name' => $bot->telegram->FirstName()]
35
        );
36
        $bot->sendPhoto(
37
            __DIR__.'/../../resources/images/telegram-git-notifier-laravel.png',
38
            ['caption' => $reply]
39
        );
40
    }
41
42
    /**
43
     * @return void
44
     *
45
     * @throws EntryNotFoundException
46
     * @throws MessageIsEmptyException
47
     */
48
    public function handle(): void
49
    {
50
        $text = $this->bot->telegram->Text();
51
52
        switch ($text) {
53
            case '/start':
54
                $this->sendStartMessage($this->bot);
55
56
                break;
57
            case '/menu':
58
                $this->bot->sendMessage(
59
                    view("$this->viewNamespace::tools.menu"),
60
                    ['reply_markup' => $this->menuMarkup($this->bot->telegram)]
61
                );
62
63
                break;
64
            case '/token':
65
            case '/id':
66
            case '/usage':
67
            case '/server':
68
                $this->bot->sendMessage(view("$this->viewNamespace::tools.".trim($text, '/')));
69
70
                break;
71
            case '/settings':
72
                $this->bot->settingHandle();
73
74
                break;
75
            case '/set_menu':
76
                $this->bot->setMyCommands(self::menuCommands());
77
78
                break;
79
            default:
80
                $this->bot->sendMessage('🤨 '.__('tg-notifier::app.invalid_request'));
81
        }
82
    }
83
84
    /**
85
     * @return array<string[]>
86
     */
87
    public static function menuCommands(): array
88
    {
89
        return [
90
            [
91
                'command' => '/start',
92
                'description' => __('tg-notifier::tools/menu.start'),
93
            ], [
94
                'command' => '/menu',
95
                'description' => __('tg-notifier::tools/menu.menu'),
96
            ], [
97
                'command' => '/token',
98
                'description' => __('tg-notifier::tools/menu.token'),
99
            ], [
100
                'command' => '/id',
101
                'description' => __('tg-notifier::tools/menu.id'),
102
            ], [
103
                'command' => '/usage',
104
                'description' => __('tg-notifier::tools/menu.usage'),
105
            ], [
106
                'command' => '/server',
107
                'description' => __('tg-notifier::tools/menu.server'),
108
            ], [
109
                'command' => '/settings',
110
                'description' => __('tg-notifier::tools/menu.settings'),
111
            ], [
112
                'command' => '/set_menu',
113
                'description' => __('tg-notifier::tools/menu.set_menu'),
114
            ],
115
        ];
116
    }
117
}
118