Passed
Push — main ( 64ea8c...cb0dfb )
by Miaad
10:02
created

receiver::processUpdate()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 7
eloc 15
c 1
b 1
f 0
nc 15
nop 1
dl 0
loc 24
rs 8.8333
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\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 (settings::$use_types_classes && !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);
0 ignored issues
show
Bug introduced by
It seems like $update can also be of type null and string; however, parameter $update of BPT\receiver\receiver::setMessageExtra() does only seem to accept BPT\types\update|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

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