Issues (36)

src/Commands/AdminCommands/ChatsCommand.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * This file is part of the TelegramBot package.
5
 *
6
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Longman\TelegramBot\Commands\AdminCommands;
13
14
use Longman\TelegramBot\Commands\AdminCommand;
15
use Longman\TelegramBot\DB;
16
use Longman\TelegramBot\Entities\Chat;
17
use Longman\TelegramBot\Entities\ServerResponse;
18
use Longman\TelegramBot\Exception\TelegramException;
19
use Longman\TelegramBot\Request;
20
21
class ChatsCommand extends AdminCommand
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $name = 'chats';
27
28
    /**
29
     * @var string
30
     */
31
    protected $description = 'List or search all chats stored by the bot';
32
33
    /**
34
     * @var string
35
     */
36
    protected $usage = '/chats, /chats * or /chats <search string>';
37
38
    /**
39
     * @var string
40
     */
41
    protected $version = '1.2.0';
42
43
    /**
44
     * @var bool
45
     */
46
    protected $need_mysql = true;
47
48
    /**
49
     * Command execute method
50
     *
51
     * @return ServerResponse
52
     * @throws TelegramException
53
     */
54
    public function execute(): ServerResponse
55
    {
56
        $message = $this->getMessage();
57
58
        $chat_id = $message->getChat()->getId();
59
        $text    = trim($message->getText(true));
0 ignored issues
show
It seems like $message->getText(true) can also be of type null; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        $text    = trim(/** @scrutinizer ignore-type */ $message->getText(true));
Loading history...
60
61
        $results = DB::selectChats([
62
            'groups'      => true,
63
            'supergroups' => true,
64
            'channels'    => true,
65
            'users'       => true,
66
            'text'        => ($text === '' || $text === '*') ? null : $text //Text to search in user/group name
67
        ]);
68
69
        $user_chats       = 0;
70
        $group_chats      = 0;
71
        $supergroup_chats = 0;
72
        $channel_chats    = 0;
73
74
        if ($text === '') {
75
            $text_back = '';
76
        } elseif ($text === '*') {
77
            $text_back = 'List of all bot chats:' . PHP_EOL;
78
        } else {
79
            $text_back = 'Chat search results:' . PHP_EOL;
80
        }
81
82
        if (is_array($results)) {
83
            foreach ($results as $result) {
84
                //Initialize a chat object
85
                $result['id'] = $result['chat_id'];
86
                $chat         = new Chat($result);
87
88
                $whois = $chat->getId();
89
                if ($this->telegram->getCommandObject('whois')) {
90
                    // We can't use '-' in command because part of it will become unclickable
91
                    $whois = '/whois' . str_replace('-', 'g', $chat->getId());
92
                }
93
94
                if ($chat->isPrivateChat()) {
95
                    if ($text !== '') {
96
                        $text_back .= '- P ' . $chat->tryMention() . ' [' . $whois . ']' . PHP_EOL;
97
                    }
98
99
                    ++$user_chats;
100
                } elseif ($chat->isSuperGroup()) {
101
                    if ($text !== '') {
102
                        $text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
103
                    }
104
105
                    ++$supergroup_chats;
106
                } elseif ($chat->isGroupChat()) {
107
                    if ($text !== '') {
108
                        $text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
109
                    }
110
111
                    ++$group_chats;
112
                } elseif ($chat->isChannel()) {
113
                    if ($text !== '') {
114
                        $text_back .= '- C ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
115
                    }
116
117
                    ++$channel_chats;
118
                }
119
            }
120
        }
121
122
        if (($user_chats + $group_chats + $supergroup_chats) === 0) {
123
            $text_back = 'No chats found..';
124
        } else {
125
            $text_back .= PHP_EOL . 'Private Chats: ' . $user_chats;
126
            $text_back .= PHP_EOL . 'Groups: ' . $group_chats;
127
            $text_back .= PHP_EOL . 'Super Groups: ' . $supergroup_chats;
128
            $text_back .= PHP_EOL . 'Channels: ' . $channel_chats;
129
            $text_back .= PHP_EOL . 'Total: ' . ($user_chats + $group_chats + $supergroup_chats);
130
131
            if ($text === '') {
132
                $text_back .= PHP_EOL . PHP_EOL . 'List all chats: /' . $this->name . ' *' . PHP_EOL . 'Search for chats: /' . $this->name . ' <search string>';
133
            }
134
        }
135
136
        $data = [
137
            'chat_id' => $chat_id,
138
            'text'    => $text_back,
139
        ];
140
141
        return Request::sendMessage($data);
142
    }
143
}
144