ChatsCommand::execute()   D
last analyzed

Complexity

Conditions 16
Paths 18

Size

Total Lines 83
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 83
ccs 0
cts 67
cp 0
rs 4.8945
cc 16
eloc 53
nc 18
nop 0
crap 272

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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.1.0';
39
40
    /**
41
     * @var bool
42
     */
43
    protected $need_mysql = true;
44
45
    /**
46
     * Command execute method
47
     *
48
     * @return mixed
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
            true, //Select groups (group chat)
60
            true, //Select supergroups (super group chat)
61
            true, //Select users (single chat)
62
            null, //'yyyy-mm-dd hh:mm:ss' date range from
63
            null, //'yyyy-mm-dd hh:mm:ss' date range to
64
            null, //Specific chat_id to select
65
            ($text === '' || $text === '*') ? null : $text //Text to search in user/group name
66
        );
67
68
        $user_chats        = 0;
69
        $group_chats       = 0;
70
        $super_group_chats = 0;
71
72
        if ($text === '') {
73
            $text_back = '';
74
        } elseif ($text === '*') {
75
            $text_back = 'List of all bot chats:' . PHP_EOL;
76
        } else {
77
            $text_back = 'Chat search results:' . PHP_EOL;
78
        }
79
80
        if (is_array($results)) {
81
            foreach ($results as $result) {
82
                //Initialize a chat object
83
                $result['id'] = $result['chat_id'];
84
                $chat         = new Chat($result);
85
86
                $whois = $chat->getId();
87
                if ($this->telegram->getCommandObject('whois')) {
88
                    // We can't use '-' in command because part of it will become unclickable
89
                    $whois = '/whois' . str_replace('-', 'g', $chat->getId());
90
                }
91
92
                if ($chat->isPrivateChat()) {
93
                    if ($text !== '') {
94
                        $text_back .= '- P ' . $chat->tryMention() . ' [' . $whois . ']' . PHP_EOL;
95
                    }
96
97
                    ++$user_chats;
98
                } elseif ($chat->isSuperGroup()) {
99
                    if ($text !== '') {
100
                        $text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
101
                    }
102
103
                    ++$super_group_chats;
104
                } elseif ($chat->isGroupChat()) {
105
                    if ($text !== '') {
106
                        $text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
107
                    }
108
109
                    ++$group_chats;
110
                }
111
            }
112
        }
113
114
        if (($user_chats + $group_chats + $super_group_chats) === 0) {
115
            $text_back = 'No chats found..';
116
        } else {
117
            $text_back .= PHP_EOL . 'Private Chats: ' . $user_chats;
118
            $text_back .= PHP_EOL . 'Groups: ' . $group_chats;
119
            $text_back .= PHP_EOL . 'Super Groups: ' . $super_group_chats;
120
            $text_back .= PHP_EOL . 'Total: ' . ($user_chats + $group_chats + $super_group_chats);
121
122
            if ($text === '') {
123
                $text_back .= PHP_EOL . PHP_EOL . 'List all chats: /' . $this->name . ' *' . PHP_EOL . 'Search for chats: /' . $this->name . ' <search string>';
124
            }
125
        }
126
127
        $data = [
128
            'chat_id' => $chat_id,
129
            'text'    => $text_back,
130
        ];
131
132
        return Request::sendMessage($data);
133
    }
134
}
135