Passed
Push — main ( d7cb78...b591cd )
by Tan
03:13 queued 43s
created

CommandService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 2
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
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/start.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[]
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
        ];
113
    }
114
}
115