Passed
Push — main ( 87c5f3...bd6751 )
by Miaad
02:42 queued 25s
created

handler::message()   D

Complexity

Conditions 20
Paths 60

Size

Total Lines 159
Code Lines 91

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 20
eloc 91
c 1
b 0
f 0
nc 60
nop 1
dl 0
loc 159
rs 4.1666

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
use BPT\BPT;
4
use BPT\constants\dbTypes;
5
use BPT\database\mysql;
6
use BPT\types\message;
7
8
if (file_exists('vendor/autoload.php')){
9
    require 'vendor/autoload.php';
10
}
11
else{
12
    if(!file_exists('BPT.phar')) {
13
        copy('https://dl.bptlib.ir/BPT.phar', 'BPT.phar');
14
    }
15
    require 'BPT.phar';
16
}
17
18
class handler extends BPT {
19
    const ADMIN = 123456789;
20
21
    const SHOW_STATUS = true;
22
23
    const HELP = 'Hello dear admin 
24
This is a simple messenger bot which also support reply for users and admin itself
25
For answering an user message , You must reply on it
26
If you want to reply , You must first use /reply_on command and this will apply on your next message
27
If you want to cancel reply , use /reply_off
28
If you reply on yourself message , it will be replied without checking reply_on status
29
Good luck and be nice';
30
    const REPLY_ON = 'Ok dear admin , Your next message will be replied';
31
    const REPLY_OFF = 'Ok dear admin , Reply canceled';
32
    const NOT_FOUND = 'I didn\'t found this message in my database';
33
    const START_TEXT = 'Hello dear user
34
Send your message and I will deliver it to my admin';
35
    const SEND_FAILED = 'Failed!';
36
    const SEND_SUCCESSFUL = 'Done!';
37
38
    public function __construct(array $settings){
39
        parent::__construct($settings);
40
    }
41
42
    public function message(message $update){
43
        $text = $update->text ?? '';
44
        $user_id = $update->from->id;
45
46
        if ($text === '/start') {
47
            $this->sendMessage(self::START_TEXT,answer: true);
0 ignored issues
show
Unused Code introduced by
The call to BPT\BPT::sendMessage() has too many arguments starting with self::START_TEXT. ( Ignorable by Annotation )

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

47
            $this->/** @scrutinizer ignore-call */ 
48
                   sendMessage(self::START_TEXT,answer: true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
48
        }
49
        else {
50
            /** You could use both style */
51
            $message_id = $update->message_id;
52
            #$message_id = $update->id;
53
54
            if (self::ADMIN === $user_id) {
55
                if ($text === '/help') {
56
                    $this->sendMessage(self::HELP, answer: true);
57
                }
58
                elseif ($text === '/reply_on') {
59
                    mysql::update('users',['value'=>'reply_on'],['id'=>$user_id],1);
60
                    $this->sendMessage(self::REPLY_ON, answer: true);
61
                }
62
                elseif ($text === '/reply_off') {
63
                    mysql::update('users',['value'=>'reply_off'],['id'=>$user_id],1);
64
                    $this->sendMessage(self::REPLY_OFF, answer: true);
65
                }
66
                elseif (isset($update->reply_to_message)) {
67
                    $reply_message_id = $update->reply_to_message->message_id;
68
69
                    if ($update->reply_to_message->from->id === $user_id) {
70
                        $check_message = mysql::select('messages', ['receiver_message_id','receiver_id'], [
71
                            'sender_message_id' => $reply_message_id,
72
                            'sender_id'         => $user_id
73
                        ],1);
74
75
                        if ($check_message->num_rows > 0) {
76
                            $data = $check_message->fetch_object();
77
                            $receiver_id = $data->receiver_id;
78
                            $result = $this->copyMessage($receiver_id, reply_to_message_id: $data->receiver_message_id);
0 ignored issues
show
Unused Code introduced by
The call to BPT\BPT::copyMessage() has too many arguments starting with $receiver_id. ( Ignorable by Annotation )

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

78
                            /** @scrutinizer ignore-call */ 
79
                            $result = $this->copyMessage($receiver_id, reply_to_message_id: $data->receiver_message_id);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
79
                        }
80
                        else {
81
                            $this->sendMessage(self::NOT_FOUND, answer: true);
82
                            return;
83
                        }
84
                    }
85
                    else {
86
                        $data = mysql::select('messages', ['sender_message_id','sender_id'], [
87
                            'receiver_message_id' => $reply_message_id,
88
                            'receiver_id'         => $user_id
89
                        ],1)->fetch_object();
90
91
                        $value = mysql::select('users','value',['id'=>$user_id])->fetch_object()->value;
92
                        $receiver_id = $data->sender_id;
93
                        if ($value === 'reply_on') {
94
                            mysql::update('users',['value'=>''],['id'=>$user_id]);
95
                            $result = $this->copyMessage($receiver_id, reply_to_message_id: $data->sender_message_id);
96
                        }
97
                        else {
98
                            $result = $this->copyMessage($receiver_id);
99
                        }
100
                    }
101
102
                    if (self::$status) {
103
                        mysql::insert('messages',
104
                            ['sender_message_id','sender_id','receiver_message_id','receiver_id'],
105
                            [$message_id,$user_id,$result->message_id,$receiver_id]
106
                        );
107
                        if (self::SHOW_STATUS) {
108
                            $this->sendMessage(self::SEND_SUCCESSFUL, answer: true);
109
                        }
110
                    }
111
                    else {
112
                        $this->sendMessage(self::SEND_FAILED, answer: true);
113
                    }
114
                }
115
            }
116
            else {
117
                $username = $update->from->username;
118
                if (empty($username)) {
119
                    $name = $update->from->first_name . (!empty($update->from->last_name) ? (' ' . $update->from->last_name) : '');
120
                    $keyboard = [
121
                        'inline_keyboard' => [
122
                            [
123
                                [
124
                                    "text" => 'User ID', 'url' => "tg://user?id=$user_id"
125
                                ],
126
                                [
127
                                    "text" => $user_id, 'url' => "tg://user?id=$user_id"
128
                                ]
129
                            ],
130
                            [
131
                                [
132
                                    "text" => 'Name', 'callback_data' => 'none'
133
                                ],
134
                                [
135
                                    "text" => $name, 'callback_data' => 'none'
136
                                ]
137
                            ]
138
                        ]
139
                    ];
140
                }
141
                else {
142
                    $keyboard = [
143
                        'inline_keyboard' => [
144
                            [
145
                                [
146
                                    "text" => 'Username', 'url' => "https://t.me/$username"
147
                                ],
148
                                [
149
                                    "text" => $username, 'url' => "https://t.me/$username"
150
                                ]
151
                            ]
152
                        ]
153
                    ];
154
                }
155
                if (isset($update->reply_to_message)) {
156
                    $reply_message_id = $update->reply_to_message->message_id;
157
                    if ($update->reply_to_message->from->id === $user_id){
158
                        $check_message = mysql::select('messages', 'receiver_message_id', [
159
                            'sender_message_id' => $reply_message_id,
160
                            'sender_id'         => $user_id
161
                        ],1);
162
                        if ($check_message->num_rows > 0) {
163
                            $result = $this->copyMessage(self::ADMIN, reply_to_message_id: $check_message->fetch_object()->receiver_message_id,reply_markup: $keyboard);
164
                        }
165
                        else {
166
                            $result = $this->copyMessage(self::ADMIN,reply_markup: $keyboard);
167
                        }
168
                    }
169
                    else {
170
                        $check_message = mysql::select('messages', 'sender_message_id', [
171
                            'receiver_message_id' => $reply_message_id,
172
                            'receiver_id'         => $user_id
173
                        ],1);
174
                        if ($check_message->num_rows > 0) {
175
                            $result = $this->copyMessage(self::ADMIN, reply_to_message_id: $check_message->fetch_object()->sender_message_id,reply_markup: $keyboard);
176
                        }
177
                        else {
178
                            $result = $this->copyMessage(self::ADMIN,reply_markup: $keyboard);
179
                        }
180
                    }
181
                }
182
                else {
183
                    $result = $this->copyMessage(self::ADMIN,reply_markup: $keyboard);
184
                }
185
186
                /**
187
                 * This is status of last called telegram method
188
                 * You can use telegram::$status or request::$status either
189
                 */
190
                if (self::$status) {
191
                    mysql::insert('messages',
192
                        ['sender_message_id','sender_id','receiver_message_id','receiver_id'],
193
                        [$message_id,$user_id,$result->message_id,self::ADMIN]
194
                    );
195
                    if (self::SHOW_STATUS) {
196
                        $this->sendMessage(self::SEND_SUCCESSFUL, answer: true);
197
                    }
198
                }
199
                else {
200
                    $this->sendMessage(self::SEND_FAILED, answer: true);
201
                }
202
            }
203
        }
204
    }
205
}
206
207
/**
208
 * BPT settings
209
 *
210
 * @link https://bptlib.ir/multi
211
 */
212
$BPT = new handler([
213
    'token' => 'YOUR_BOT_TOKEN',
214
    'db' => [
215
        'type' => dbTypes::MYSQL,
216
        'user' => 'dbUser',
217
        'pass' => 'dbPassword',
218
        'dbname' => 'dbName'
219
    ],
220
    'allowed_updates' => ['message']
221
]);