Passed
Push — main ( 723093...1f3d08 )
by Miaad
01:40
created

receiver   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 76
c 1
b 1
f 0
dl 0
loc 126
rs 9.1199
wmc 41

5 Methods

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

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\loggerTypes;
7
use BPT\database\db;
8
use BPT\logger;
9
use BPT\settings;
10
use BPT\tools;
11
use BPT\types\update;
12
use stdClass;
13
14
/**
15
 * receiver class , will be used in webhook and getUpdates classes
16
 */
17
class receiver {
18
    private static array $handlers = [
19
        'message' => null,
20
        'edited_message' => null,
21
        'channel_post' => null,
22
        'edited_channel_post' => null,
23
        'inline_query' => null,
24
        'callback_query' => null,
25
        'my_chat_member' => null,
26
        'chat_member' => null,
27
        'chat_join_request' => null,
28
        'something_else' => null
29
    ];
30
31
    protected static function telegramVerify(string $ip = null): void {
32
        if (settings::$telegram_verify) {
33
            $ip = $ip ?? tools::remoteIP();
34
            if (!tools::isTelegram($ip)) {
35
                logger::write('not authorized access denied. IP : '. $ip ?? 'unknown',loggerTypes::WARNING);
36
                BPT::exit();
37
            }
38
        }
39
    }
40
41
    protected static function processUpdate(string|stdClass|update $update = null): void {
42
        if (!is_object($update)) {
43
            $update = json_decode($update ?? file_get_contents("php://input"));
44
            if (!$update) {
45
                BPT::exit();
46
            }
47
        }
48
49
        if (!is_a($update,'update')) {
50
            $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

50
            $update = new update(/** @scrutinizer ignore-type */ $update);
Loading history...
51
        }
52
53
        self::setMessageExtra($update);
54
        BPT::$update = $update;
55
        db::process();
56
        self::processHandler();
57
        db::save();
58
    }
59
60
    protected static function setMessageExtra (update &$update): void {
61
        if ((isset($update->message) && isset($update->message->text)) || (isset($update->edited_message) && isset($update->edited_message->text))) {
62
            $type = isset($update->message) ? 'message' : 'edited_message';
63
            $text = &$update->$type->text;
64
            if (settings::$security) {
65
                $text = tools::clearText($text);
66
            }
67
            if (str_starts_with($text, '/')) {
68
                preg_match('/\/([a-zA-Z_0-9]{1,64})(@[a-zA-Z]\w{1,28}bot)?( [\S]{1,64})?/', $text, $result);
69
                if (isset($result[1])) {
70
                    $update->$type->commend = $result[1];
71
                }
72
                if (isset($result[2])) {
73
                    $update->$type->commend_username = $result[2];
74
                }
75
                if (isset($result[3])) {
76
                    $update->$type->commend_payload = $result[3];
77
                }
78
            }
79
        }
80
    }
81
82
    private static function processHandler(): void {
83
        if (settings::$handler) {
84
            if (isset(BPT::$update->message)) {
85
                if (self::handlerExist('message')) {
86
                    BPT::$handler->message(BPT::$update->message);
87
                }
88
            }
89
            elseif (isset(BPT::$update->edited_message)) {
90
                if (self::handlerExist('edited_message')) {
91
                    BPT::$handler->edited_message(BPT::$update->edited_message);
92
                }
93
            }
94
            elseif (isset(BPT::$update->channel_post)) {
95
                if (self::handlerExist('channel_post')) {
96
                    BPT::$handler->channel_post(BPT::$update->channel_post);
97
                }
98
            }
99
            elseif (isset(BPT::$update->edited_channel_post)) {
100
                if (self::handlerExist('edited_channel_post')) {
101
                    BPT::$handler->edited_channel_post(BPT::$update->edited_channel_post);
102
                }
103
            }
104
            elseif (isset(BPT::$update->inline_query)) {
105
                if (self::handlerExist('inline_query')) {
106
                    BPT::$handler->inline_query(BPT::$update->inline_query);
107
                }
108
            }
109
            elseif (isset(BPT::$update->callback_query)) {
110
                if (self::handlerExist('callback_query')) {
111
                    BPT::$handler->callback_query(BPT::$update->callback_query);
112
                }
113
            }
114
            elseif (isset(BPT::$update->my_chat_member)) {
115
                if (self::handlerExist('my_chat_member')) {
116
                    BPT::$handler->my_chat_member(BPT::$update->my_chat_member);
117
                }
118
            }
119
            elseif (isset(BPT::$update->chat_member)) {
120
                if (self::handlerExist('chat_member')) {
121
                    BPT::$handler->chat_member(BPT::$update->chat_member);
122
                }
123
            }
124
            elseif (isset(BPT::$update->chat_join_request)) {
125
                if (self::handlerExist('chat_join_request')) {
126
                    BPT::$handler->chat_join_request(BPT::$update->chat_join_request);
127
                }
128
            }
129
            elseif (self::handlerExist('something_else')) {
130
                BPT::$handler->something_else(BPT::$update);
131
            }
132
            else {
133
                logger::write('Update received but handlers are not set',loggerTypes::WARNING);
134
            }
135
        }
136
    }
137
138
    private static function handlerExist(string $handler): bool {
139
        if (empty(self::$handlers[$handler])) {
140
            self::$handlers[$handler] = method_exists(BPT::$handler, $handler);
141
        }
142
        return self::$handlers[$handler];
143
    }
144
}