These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * This file is part of the TelegramBot package. |
||
4 | * |
||
5 | * (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
||
6 | * |
||
7 | * For the full copyright and license information, please view the LICENSE |
||
8 | * file that was distributed with this source code. |
||
9 | */ |
||
10 | |||
11 | namespace Longman\TelegramBot\Commands\AdminCommands; |
||
12 | |||
13 | use Longman\TelegramBot\Commands\AdminCommand; |
||
14 | use Longman\TelegramBot\DB; |
||
15 | use Longman\TelegramBot\Entities\Chat; |
||
16 | use Longman\TelegramBot\Request; |
||
17 | |||
18 | class ChatsCommand extends AdminCommand |
||
19 | { |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $name = 'chats'; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $description = 'List or search all chats stored by the bot'; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $usage = '/chats, /chats * or /chats <search string>'; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $version = '1.2.0'; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | protected $need_mysql = true; |
||
44 | |||
45 | /** |
||
46 | * Command execute method |
||
47 | * |
||
48 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
49 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
50 | */ |
||
51 | public function execute() |
||
52 | { |
||
53 | $message = $this->getMessage(); |
||
54 | |||
55 | $chat_id = $message->getChat()->getId(); |
||
56 | $text = trim($message->getText(true)); |
||
57 | |||
58 | $results = DB::selectChats([ |
||
59 | 'groups' => true, |
||
60 | 'supergroups' => true, |
||
61 | 'channels' => true, |
||
62 | 'users' => true, |
||
63 | 'text' => ($text === '' || $text === '*') ? null : $text //Text to search in user/group name |
||
64 | ]); |
||
65 | |||
66 | $user_chats = 0; |
||
67 | $group_chats = 0; |
||
68 | $supergroup_chats = 0; |
||
69 | $channel_chats = 0; |
||
70 | |||
71 | if ($text === '') { |
||
72 | $text_back = ''; |
||
73 | } elseif ($text === '*') { |
||
74 | $text_back = 'List of all bot chats:' . PHP_EOL; |
||
75 | } else { |
||
76 | $text_back = 'Chat search results:' . PHP_EOL; |
||
77 | } |
||
78 | |||
79 | if (is_array($results)) { |
||
80 | foreach ($results as $result) { |
||
81 | //Initialize a chat object |
||
82 | $result['id'] = $result['chat_id']; |
||
83 | $chat = new Chat($result); |
||
84 | |||
85 | $whois = $chat->getId(); |
||
86 | if ($this->telegram->getCommandObject('whois')) { |
||
87 | // We can't use '-' in command because part of it will become unclickable |
||
88 | $whois = '/whois' . str_replace('-', 'g', $chat->getId()); |
||
89 | } |
||
90 | |||
91 | if ($chat->isPrivateChat()) { |
||
92 | if ($text !== '') { |
||
93 | $text_back .= '- P ' . $chat->tryMention() . ' [' . $whois . ']' . PHP_EOL; |
||
94 | } |
||
95 | |||
96 | ++$user_chats; |
||
97 | View Code Duplication | } elseif ($chat->isSuperGroup()) { |
|
0 ignored issues
–
show
|
|||
98 | if ($text !== '') { |
||
99 | $text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL; |
||
100 | } |
||
101 | |||
102 | ++$supergroup_chats; |
||
103 | } elseif ($chat->isGroupChat()) { |
||
104 | if ($text !== '') { |
||
105 | $text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL; |
||
106 | } |
||
107 | |||
108 | ++$group_chats; |
||
109 | View Code Duplication | } elseif ($chat->isChannel()) { |
|
110 | if ($text !== '') { |
||
111 | $text_back .= '- C ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL; |
||
112 | } |
||
113 | |||
114 | ++$channel_chats; |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | |||
119 | if (($user_chats + $group_chats + $supergroup_chats) === 0) { |
||
120 | $text_back = 'No chats found..'; |
||
121 | } else { |
||
122 | $text_back .= PHP_EOL . 'Private Chats: ' . $user_chats; |
||
123 | $text_back .= PHP_EOL . 'Groups: ' . $group_chats; |
||
124 | $text_back .= PHP_EOL . 'Super Groups: ' . $supergroup_chats; |
||
125 | $text_back .= PHP_EOL . 'Channels: ' . $channel_chats; |
||
126 | $text_back .= PHP_EOL . 'Total: ' . ($user_chats + $group_chats + $supergroup_chats); |
||
127 | |||
128 | if ($text === '') { |
||
129 | $text_back .= PHP_EOL . PHP_EOL . 'List all chats: /' . $this->name . ' *' . PHP_EOL . 'Search for chats: /' . $this->name . ' <search string>'; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | $data = [ |
||
134 | 'chat_id' => $chat_id, |
||
135 | 'text' => $text_back, |
||
136 | ]; |
||
137 | |||
138 | return Request::sendMessage($data); |
||
139 | } |
||
140 | } |
||
141 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.