Passed
Push — main ( e1a2dc...e011f6 )
by Miaad
02:06
created

receiver::processHandler()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 27
rs 7.3166
c 0
b 0
f 0
cc 11
nc 11
nop 0

How to fix   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
namespace BPT\receiver;
4
5
use BPT\BPT;
6
use BPT\constants\loggerTypes;
7
use BPT\logger;
8
use BPT\settings;
9
use BPT\tools;
10
use BPT\types\update;
11
use stdClass;
12
13
class receiver {
14
    private static array $handlers = [
15
        'message' => null,
16
        'callback_query' => null,
17
        'inline_query' => null,
18
        'edited_message' => null,
19
        'something_else' => null
20
    ];
21
22
    protected static function telegramVerify(string $ip = null) {
23
        if (settings::$telegram_verify) {
24
            if (!tools::isTelegram($ip ?? $_SERVER['REMOTE_ADDR'] ?? '')) {
25
                logger::write('not authorized access denied. IP : '. $ip ?? $_SERVER['REMOTE_ADDR'] ?? 'unknown',loggerTypes::ERROR);
26
                BPT::exit();
27
            }
28
        }
29
    }
30
31
    protected static function processUpdate(string|stdClass $update = null) {
32
        if (!is_object($update)) {
33
            $update = json_decode($update ?? file_get_contents("php://input"));
34
            if (!$update) {
35
                BPT::exit();
36
            }
37
        }
38
        $update = new update($update);
0 ignored issues
show
Bug introduced by
It seems like $update can also be of type null and string; however, parameter $object of BPT\types\update::__construct() does only seem to accept 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

38
        $update = new update(/** @scrutinizer ignore-type */ $update);
Loading history...
39
        self::setMessageExtra($update);
40
        BPT::$update = $update;
41
        self::processHandler();
42
    }
43
44
    protected static function setMessageExtra (update &$update) {
45
        if ((isset($update->message) && isset($update->message->text)) || (isset($update->edited_message) && isset($update->edited_message->text))) {
46
            $type = isset($update->message) ? 'message' : 'edited_message';
47
            $text = &$update->$type->text;
48
            if (settings::$security) {
49
                $text = tools::clearText($text);
50
            }
51
            if (str_starts_with($text, '/')) {
52
                preg_match('/\/([a-zA-Z_0-9]{1,64})(@[a-zA-Z]\w{1,28}bot)?( [\S]{1,64})?/', $text, $result);
53
                $update->$type->commend = $result[1] ?? null;
54
                $update->$type->commend_username = $result[2] ?? null;
55
                $update->$type->commend_payload = $result[3] ?? null;
56
            }
57
        }
58
    }
59
60
    private static function processHandler() {
61
        if (settings::$handler) {
62
            if (isset(BPT::$update->message)) {
63
                if (self::handlerExist('message')) {
64
                    BPT::$handler->message(BPT::$update->message);
0 ignored issues
show
Bug introduced by
The method message() does not exist on BPT\BPT. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

64
                    BPT::$handler->/** @scrutinizer ignore-call */ message(BPT::$update->message);
Loading history...
65
                }
66
            }
67
            elseif (isset(BPT::$update->callback_query)) {
68
                if (self::handlerExist('callback_query')) {
69
                    BPT::$handler->callback_query(BPT::$update->callback_query);
0 ignored issues
show
Bug introduced by
The method callback_query() does not exist on BPT\BPT. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

69
                    BPT::$handler->/** @scrutinizer ignore-call */ callback_query(BPT::$update->callback_query);
Loading history...
70
                }
71
            }
72
            elseif (isset(BPT::$update->inline_query)) {
73
                if (self::handlerExist('inline_query')) {
74
                    BPT::$handler->inline_query(BPT::$update->inline_query);
0 ignored issues
show
Bug introduced by
The method inline_query() does not exist on BPT\BPT. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

74
                    BPT::$handler->/** @scrutinizer ignore-call */ inline_query(BPT::$update->inline_query);
Loading history...
75
                }
76
            }
77
            elseif (isset(BPT::$update->edited_message)) {
78
                if (self::handlerExist('edited_message')) {
79
                    BPT::$handler->edited_message(BPT::$update->edited_message);
0 ignored issues
show
Bug introduced by
The method edited_message() does not exist on BPT\BPT. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

79
                    BPT::$handler->/** @scrutinizer ignore-call */ edited_message(BPT::$update->edited_message);
Loading history...
80
                }
81
            }
82
            elseif (self::handlerExist('something_else')) {
83
                BPT::$handler->something_else(BPT::$update);
0 ignored issues
show
Bug introduced by
The method something_else() does not exist on BPT\BPT. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

83
                BPT::$handler->/** @scrutinizer ignore-call */ something_else(BPT::$update);
Loading history...
84
            }
85
            else {
86
                logger::write('Update received but handlers does not set',loggerTypes::WARNING);
87
            }
88
        }
89
    }
90
91
    private static function handlerExist(string $handler): bool {
92
        if (empty(self::$handlers[$handler])) {
93
            self::$handlers[$handler] = method_exists(BPT::$handler, $handler);
94
        }
95
        return self::$handlers[$handler];
96
    }
97
}