Passed
Push — main ( 6b17f2...1e3b41 )
by Miaad
11:18
created

receiver   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 80
c 1
b 1
f 0
dl 0
loc 133
rs 8.96
wmc 43

5 Methods

Rating   Name   Duplication   Size   Complexity  
D processHandler() 0 52 21
A handlerExist() 0 5 2
B setMessageExtra() 0 17 11
A processUpdate() 0 24 6
A telegramVerify() 0 6 3

How to fix   Complexity   

Complex Class

Complex classes like receiver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use receiver, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace BPT\receiver;
4
5
use BPT\BPT;
6
use BPT\constants\fields;
7
use BPT\constants\loggerTypes;
8
use BPT\database\db;
9
use BPT\logger;
10
use BPT\settings;
11
use BPT\telegram\telegram;
12
use BPT\tools;
13
use BPT\types\update;
14
use stdClass;
15
16
/**
17
 * receiver class , will be used in webhook and getUpdates classes
18
 */
19
class receiver {
20
    private static array $handlers = [
21
        'message' => null,
22
        'edited_message' => null,
23
        'channel_post' => null,
24
        'edited_channel_post' => null,
25
        'inline_query' => null,
26
        'callback_query' => null,
27
        'my_chat_member' => null,
28
        'chat_member' => null,
29
        'chat_join_request' => null,
30
        'something_else' => null
31
    ];
32
33
    protected static function telegramVerify(string $ip = null): void {
34
        if (settings::$telegram_verify) {
35
            $ip = $ip ?? tools::remoteIP();
36
            if (!tools::isTelegram($ip)) {
37
                logger::write('not authorized access denied. IP : '. $ip ?? 'unknown',loggerTypes::WARNING);
38
                BPT::exit();
39
            }
40
        }
41
    }
42
43
    protected static function processUpdate(string|stdClass|update $update = null): void {
44
        if (!is_object($update)) {
45
            $update = json_decode($update ?? file_get_contents("php://input"));
46
            if (!$update) {
47
                BPT::exit();
48
            }
49
        }
50
51
        if (settings::$ignore_updates_older_then > 0) {
52
            if (time() - settings::$ignore_updates_older_then > telegram::catchFields(fields::UPDATE_DATE)) {
53
                logger::write('Update is old, Ignored.');
54
                return;
55
            }
56
        }
57
58
        if (!is_a($update,'update')) {
59
            $update = new update($update);
0 ignored issues
show
Bug introduced by
It seems like $update can also be of type string; however, parameter $object of BPT\types\update::__construct() does only seem to accept null|stdClass, 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
            $update = new update(/** @scrutinizer ignore-type */ $update);
Loading history...
60
        }
61
62
        self::setMessageExtra($update);
63
        BPT::$update = $update;
64
        db::process();
65
        self::processHandler();
66
        db::save();
67
    }
68
69
    protected static function setMessageExtra (update &$update): void {
70
        if ((isset($update->message) && isset($update->message->text)) || (isset($update->edited_message) && isset($update->edited_message->text))) {
71
            $type = isset($update->message) ? 'message' : 'edited_message';
72
            $text = &$update->$type->text;
73
            if (settings::$security) {
74
                $text = tools::clearText($text);
75
            }
76
            if (str_starts_with($text, '/')) {
77
                preg_match('/\/([a-zA-Z_0-9]{1,64})(@[a-zA-Z]\w{1,28}bot)?( [\S]{1,64})?/', $text, $result);
78
                if (isset($result[1])) {
79
                    $update->$type->command = $result[1];
80
                }
81
                if (isset($result[2])) {
82
                    $update->$type->command_username = $result[2];
83
                }
84
                if (isset($result[3])) {
85
                    $update->$type->command_payload = trim($result[3]);
86
                }
87
            }
88
        }
89
    }
90
91
    private static function processHandler(): void {
92
        if (settings::$handler) {
93
            if (isset(BPT::$update->message)) {
94
                if (self::handlerExist('message')) {
95
                    BPT::$handler->message(BPT::$update->message);
96
                }
97
            }
98
            elseif (isset(BPT::$update->edited_message)) {
99
                if (self::handlerExist('edited_message')) {
100
                    BPT::$handler->edited_message(BPT::$update->edited_message);
101
                }
102
            }
103
            elseif (isset(BPT::$update->channel_post)) {
104
                if (self::handlerExist('channel_post')) {
105
                    BPT::$handler->channel_post(BPT::$update->channel_post);
106
                }
107
            }
108
            elseif (isset(BPT::$update->edited_channel_post)) {
109
                if (self::handlerExist('edited_channel_post')) {
110
                    BPT::$handler->edited_channel_post(BPT::$update->edited_channel_post);
111
                }
112
            }
113
            elseif (isset(BPT::$update->inline_query)) {
114
                if (self::handlerExist('inline_query')) {
115
                    BPT::$handler->inline_query(BPT::$update->inline_query);
116
                }
117
            }
118
            elseif (isset(BPT::$update->callback_query)) {
119
                if (self::handlerExist('callback_query')) {
120
                    BPT::$handler->callback_query(BPT::$update->callback_query);
121
                }
122
            }
123
            elseif (isset(BPT::$update->my_chat_member)) {
124
                if (self::handlerExist('my_chat_member')) {
125
                    BPT::$handler->my_chat_member(BPT::$update->my_chat_member);
126
                }
127
            }
128
            elseif (isset(BPT::$update->chat_member)) {
129
                if (self::handlerExist('chat_member')) {
130
                    BPT::$handler->chat_member(BPT::$update->chat_member);
131
                }
132
            }
133
            elseif (isset(BPT::$update->chat_join_request)) {
134
                if (self::handlerExist('chat_join_request')) {
135
                    BPT::$handler->chat_join_request(BPT::$update->chat_join_request);
136
                }
137
            }
138
            elseif (self::handlerExist('something_else')) {
139
                BPT::$handler->something_else(BPT::$update);
140
            }
141
            else {
142
                logger::write('Update received but handlers are not set',loggerTypes::WARNING);
143
            }
144
        }
145
    }
146
147
    private static function handlerExist(string $handler): bool {
148
        if (empty(self::$handlers[$handler])) {
149
            self::$handlers[$handler] = method_exists(BPT::$handler, $handler);
150
        }
151
        return self::$handlers[$handler];
152
    }
153
}