Passed
Push — main ( a7eb08...4a7474 )
by Miaad
01:30
created

webhook::setMessageExtra()   B

Complexity

Conditions 11
Paths 37

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 19
rs 7.3166
c 0
b 0
f 0
cc 11
nc 37
nop 1

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\api\telegram;
6
use BPT\BPT;
7
use BPT\lock;
8
use BPT\logger;
9
use BPT\settings;
10
use BPT\tools;
11
use BPT\types\update;
12
use CURLFile;
13
14
class webhook {
15
    public static function init () {
16
        if (settings::$multi) {
17
            multi::init();
18
        }
19
        else {
20
            if (lock::exist('BPT-HOOK')) {
21
                self::telegramVerify();
22
                BPT::$update = self::processUpdate();
23
                logger::write('Update received , lets process it ;)');
24
            }
25
            else {
26
                self::deleteOldLocks();
27
                self::setWebhook();
28
            }
29
        }
30
    }
31
32
    private static function telegramVerify() {
33
        if (settings::$telegram_verify) {
34
            if (!tools::isTelegram($_SERVER['REMOTE_ADDR'])) {
35
                logger::write('not authorized access denied. IP : '.$_SERVER['REMOTE_ADDR'],'error');
36
                BPT::exit();
37
            }
38
        }
39
    }
40
41
    private static function processUpdate(): update {
42
        $update = json_decode(file_get_contents("php://input"));
43
        if (!$update) {
44
            BPT::exit();
45
        }
46
        $update = new update($update);
47
        self::setMessageExtra($update);
48
        return $update;
49
    }
50
51
    private static function deleteOldLocks() {
52
        if (lock::exist('BPT-MULTI')) {
53
            lock::delete('BPT-MULTI');
54
        }
55
        if (lock::exist('BPT-MULTI-2')) {
56
            lock::delete('BPT-MULTI-2');
57
        }
58
        if (lock::exist('getUpdate')) {
59
            lock::delete('getUpdate');
60
        }
61
    }
62
63
    private static function setWebhook() {
64
        if (isset($_SERVER['SERVER_NAME']) && isset($_SERVER['REQUEST_URI'])) {
65
            self::setCertificate();
66
            $url = (isset(settings::$certificate) ? 'http://' : 'https://') . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
67
68
            $res = telegram::setWebhook($url, settings::$certificate, max_connections:settings::$max_connection, allowed_updates : settings::$allowed_updates);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $res is correct as BPT\api\telegram::setWeb...tings::allowed_updates) targeting BPT\api\telegram::__callStatic() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
The method setWebhook() does not exist on BPT\api\telegram. Since you implemented __callStatic, 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

68
            /** @scrutinizer ignore-call */ $res = telegram::setWebhook($url, settings::$certificate, max_connections:settings::$max_connection, allowed_updates : settings::$allowed_updates);
Loading history...
69
            if ($res->ok) {
70
                lock::set('BPT');
71
                logger::write('Webhook was set successfully','info');
72
                BPT::exit('Done');
73
            }
74
            else {
75
                logger::write("There is some problem happened , telegram response : \n".json_encode($res),'error');
76
                BPT::exit(print_r($res,true));
0 ignored issues
show
Bug introduced by
It seems like print_r($res, true) can also be of type true; however, parameter $message of BPT\BPT::exit() does only seem to accept null|string, 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

76
                BPT::exit(/** @scrutinizer ignore-type */ print_r($res,true));
Loading history...
77
            }
78
        }
79
        else {
80
            logger::write('For using webhook receiver , you should open this file in your webserver(by domain)');
81
        }
82
    }
83
84
    private static function setCertificate() {
85
        if (isset(settings::$certificate)) {
86
            if (is_string(settings::$certificate)) {
87
                if (file_exists(settings::$certificate)) {
88
                    settings::$certificate = new CURLFile(settings::$certificate);
89
                }
90
                else {
91
                    settings::$certificate = null;
92
                }
93
            }
94
        }
95
    }
96
97
    private static function setMessageExtra(update &$update) {
98
        if((isset($update->message) && isset($update->message->text)) || (isset($update->edited_message) && isset($update->edited_message->text))){
99
            if (isset($update->message)) $type = 'message';
100
            else $type = 'edited_message';
101
102
            $text = &$update->$type->text;
103
            if (settings::$security){
104
                $text = tools::clearText($text);
105
            }
106
            if (str_starts_with($text, '/')){
107
                preg_match('/\/([a-zA-Z_0-9]{1,64})(@[a-zA-Z]\w{1,28}bot)?( [\S]{1,64})?/',$text,$result);
108
                if (!empty($result[1])){
109
                    $update->$type->commend = $result[1];
110
                }
111
                if (!empty($result[2])){
112
                    $update->$type->commend_username = $result[2];
113
                }
114
                if (!empty($result[3])){
115
                    $update->$type->commend_payload = $result[3];
116
                }
117
            }
118
        }
119
    }
120
}