WhoisCommand::execute()   F
last analyzed

Complexity

Conditions 20
Paths 534

Size

Total Lines 131
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 420

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 131
ccs 0
cts 104
cp 0
rs 2.7422
cc 20
eloc 84
nc 534
nop 0
crap 420

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
 * Written by Jack'lul <[email protected]>
11
 */
12
13
namespace Longman\TelegramBot\Commands\AdminCommands;
14
15
use Longman\TelegramBot\Commands\AdminCommand;
16
use Longman\TelegramBot\DB;
17
use Longman\TelegramBot\Entities\Chat;
18
use Longman\TelegramBot\Entities\PhotoSize;
19
use Longman\TelegramBot\Entities\UserProfilePhotos;
20
use Longman\TelegramBot\Request;
21
22
/**
23
 * Admin "/whois" command
24
 */
25
class WhoisCommand extends AdminCommand
26
{
27
    /**
28
     * @var string
29
     */
30
    protected $name = 'whois';
31
32
    /**
33
     * @var string
34
     */
35
    protected $description = 'Lookup user or group info';
36
37
    /**
38
     * @var string
39
     */
40
    protected $usage = '/whois <id> or /whois <search string>';
41
42
    /**
43
     * @var string
44
     */
45
    protected $version = '1.2.0';
46
47
    /**
48
     * @var bool
49
     */
50
    protected $need_mysql = true;
51
52
    /**
53
     * Command execute method
54
     *
55
     * @return mixed
56
     * @throws \Longman\TelegramBot\Exception\TelegramException
57
     */
58
    public function execute()
59
    {
60
        $message = $this->getMessage();
61
62
        $chat_id = $message->getChat()->getId();
63
        $command = $message->getCommand();
64
        $text    = trim($message->getText(true));
65
66
        $data = ['chat_id' => $chat_id];
67
68
        //No point in replying to messages in private chats
69
        if (!$message->getChat()->isPrivateChat()) {
70
            $data['reply_to_message_id'] = $message->getMessageId();
71
        }
72
73
        if ($command !== 'whois') {
74
            $text = substr($command, 5);
75
76
            //We need that '-' now, bring it back
77
            if (strpos($text, 'g') === 0) {
78
                $text = str_replace('g', '-', $text);
79
            }
80
        }
81
82
        if ($text === '') {
83
            $text = 'Provide the id to lookup: /whois <id>';
84
        } else {
85
            $user_id    = $text;
86
            $chat       = null;
87
            $created_at = null;
88
            $updated_at = null;
89
            $result     = null;
90
91
            if (is_numeric($text)) {
92
                $results = DB::selectChats(
93
                    true, //Select groups (group chat)
94
                    true, //Select supergroups (super group chat)
95
                    true, //Select users (single chat)
96
                    null, //'yyyy-mm-dd hh:mm:ss' date range from
97
                    null, //'yyyy-mm-dd hh:mm:ss' date range to
98
                    $user_id //Specific chat_id to select
99
                );
100
101
                if (!empty($results)) {
102
                    $result = reset($results);
103
                }
104
            } else {
105
                $results = DB::selectChats(
106
                    true, //Select groups (group chat)
107
                    true, //Select supergroups (super group chat)
108
                    true, //Select users (single chat)
109
                    null, //'yyyy-mm-dd hh:mm:ss' date range from
110
                    null, //'yyyy-mm-dd hh:mm:ss' date range to
111
                    null, //Specific chat_id to select
112
                    $text //Text to search in user/group name
113
                );
114
115
                if (is_array($results) && count($results) === 1) {
116
                    $result = reset($results);
117
                }
118
            }
119
120
            if (is_array($result)) {
121
                $result['id'] = $result['chat_id'];
122
                $chat         = new Chat($result);
123
124
                $user_id    = $result['id'];
125
                $created_at = $result['chat_created_at'];
126
                $updated_at = $result['chat_updated_at'];
127
                $old_id     = $result['old_id'];
128
            }
129
130
            if ($chat !== null) {
131
                if ($chat->isPrivateChat()) {
132
                    $text = 'User ID: ' . $user_id . PHP_EOL;
133
                    $text .= 'Name: ' . $chat->getFirstName() . ' ' . $chat->getLastName() . PHP_EOL;
134
135
                    $username = $chat->getUsername();
136
                    if ($username !== null && $username !== '') {
137
                        $text .= 'Username: @' . $username . PHP_EOL;
138
                    }
139
140
                    $text .= 'First time seen: ' . $created_at . PHP_EOL;
141
                    $text .= 'Last activity: ' . $updated_at . PHP_EOL;
142
143
                    //Code from Whoami command
144
                    $limit    = 10;
145
                    $offset   = null;
146
                    $response = Request::getUserProfilePhotos(
147
                        [
148
                            'user_id' => $user_id,
149
                            'limit'   => $limit,
150
                            'offset'  => $offset,
151
                        ]
152
                    );
153
154
                    if ($response->isOk()) {
155
                        /** @var UserProfilePhotos $user_profile_photos */
156
                        $user_profile_photos = $response->getResult();
157
158
                        if ($user_profile_photos->getTotalCount() > 0) {
159
                            $photos = $user_profile_photos->getPhotos();
160
161
                            /** @var PhotoSize $photo */
162
                            $photo   = $photos[0][2];
163
                            $file_id = $photo->getFileId();
164
165
                            $data['photo']   = $file_id;
166
                            $data['caption'] = $text;
167
168
                            return Request::sendPhoto($data);
169
                        }
170
                    }
171
                } elseif ($chat->isGroupChat()) {
172
                    $text = 'Chat ID: ' . $user_id . (!empty($old_id) ? ' (previously: ' . $old_id . ')' : '') . PHP_EOL;
173
                    $text .= 'Type: ' . ucfirst($chat->getType()) . PHP_EOL;
174
                    $text .= 'Title: ' . $chat->getTitle() . PHP_EOL;
175
                    $text .= 'First time added to group: ' . $created_at . PHP_EOL;
176
                    $text .= 'Last activity: ' . $updated_at . PHP_EOL;
177
                }
178
            } elseif (is_array($results) && count($results) > 1) {
179
                $text = 'Multiple chats matched!';
180
            } else {
181
                $text = 'Chat not found!';
182
            }
183
        }
184
185
        $data['text'] = $text;
186
187
        return Request::sendMessage($data);
188
    }
189
}
190