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

CommandService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace CSlant\TelegramGitNotifierApp\Services;
4
5
use CSlant\TelegramGitNotifier\Bot;
6
use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException;
7
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
8
use CSlant\TelegramGitNotifierApp\Traits\Markup;
9
10
class CommandService
11
{
12
    use Markup;
13
14
    public const MENU_COMMANDS
15
        = [
16
            [
17
                'command' => '/start',
18
                'description' => 'Welcome to the bot',
19
            ], [
20
                'command' => '/menu',
21
                'description' => 'Show menu of the bot',
22
            ], [
23
                'command' => '/token',
24
                'description' => 'Show token of the bot',
25
            ], [
26
                'command' => '/id',
27
                'description' => 'Show the ID of the current chat',
28
            ], [
29
                'command' => '/usage',
30
                'description' => 'Show step by step usage',
31
            ], [
32
                'command' => '/server',
33
                'description' => 'To get Server Information',
34
            ], [
35
                'command' => '/settings',
36
                'description' => 'Go to settings of the bot',
37
            ],
38
        ];
39
40
    private Bot $bot;
41
42
    public function __construct(Bot $bot)
43
    {
44
        $this->bot = $bot;
45
    }
46
47
    /**
48
     * @param Bot $bot
49
     *
50
     * @return void
51
     * @throws EntryNotFoundException
52
     */
53
    public function sendStartMessage(Bot $bot): void
54
    {
55
        $reply = view(
56
            'tools.start',
57
            ['first_name' => $bot->telegram->FirstName()]
58
        );
59
        $bot->sendPhoto(
60
            __DIR__ . '/../../resources/images/start.png',
61
            ['caption' => $reply]
62
        );
63
    }
64
65
    /**
66
     * @return void
67
     * @throws EntryNotFoundException
68
     * @throws MessageIsEmptyException
69
     */
70
    public function handle(): void
71
    {
72
        $text = $this->bot->telegram->Text();
73
74
        switch ($text) {
75
            case '/start':
76
                $this->sendStartMessage($this->bot);
77
78
                break;
79
            case '/menu':
80
                $this->bot->sendMessage(
81
                    view('tools.menu'),
82
                    ['reply_markup' => $this->menuMarkup($this->bot->telegram)]
83
                );
84
85
                break;
86
            case '/token':
87
            case '/id':
88
            case '/usage':
89
            case '/server':
90
                $this->bot->sendMessage(view('tools.' . trim($text, '/')));
91
92
                break;
93
            case '/settings':
94
                $this->bot->settingHandle();
95
96
                break;
97
            case '/set_menu':
98
                $this->bot->setMyCommands(CommandService::MENU_COMMANDS);
99
100
                break;
101
            default:
102
                $this->bot->sendMessage('🤨 Invalid Request!');
103
        }
104
    }
105
}
106