Passed
Push — main ( fce709...e7b3ac )
by Miaad
02:07
created

receiver   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 73
c 1
b 1
f 0
dl 0
loc 126
rs 8.8
wmc 45

5 Methods

Rating   Name   Duplication   Size   Complexity  
D processHandler() 0 47 19
A handlerExist() 0 5 2
A processUpdate() 0 16 4
B telegramVerify() 0 17 9
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;
0 ignored issues
show
Bug introduced by
The type BPT\database\db was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use BPT\logger;
9
use BPT\settings;
10
use BPT\tools;
11
use BPT\types\update;
12
use stdClass;
13
14
class receiver {
15
    private static array $handlers = [
16
        'message' => null,
17
        'callback_query' => null,
18
        'inline_query' => null,
19
        'edited_message' => null,
20
        'something_else' => null
21
    ];
22
23
    protected static function telegramVerify(string $ip = null): void {
24
        if (settings::$telegram_verify) {
25
            $ip = $ip ?? $_SERVER['REMOTE_ADDR'];
26
            if (settings::$cloadflare_verify) {
27
                if (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && tools::isCloudFlare($ip)) {
28
                    $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
29
                }
30
            }
31
            elseif (settings::$arvancloud_verify) {
32
                if (isset($_SERVER['HTTP_AR_REAL_IP']) && tools::isArvanCloud($ip)) {
33
                    $ip = $_SERVER['HTTP_AR_REAL_IP'];
34
                }
35
            }
36
37
            if (!tools::isTelegram($ip ?? '')) {
38
                logger::write('not authorized access denied. IP : '. $ip ?? 'unknown',loggerTypes::WARNING);
39
                BPT::exit();
40
            }
41
        }
42
    }
43
44
    protected static function processUpdate(string|stdClass|update $update = null): void {
45
        if (!is_object($update)) {
46
            $update = json_decode($update ?? file_get_contents("php://input"));
47
            if (!$update) {
48
                BPT::exit();
49
            }
50
        }
51
52
        if (!is_a($update,'update')) {
53
            $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

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