Passed
Push — main ( e8ebe8...e1a2dc )
by Miaad
01:30
created

webhook::checkURL()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
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 CURLFile;
11
12
class webhook extends receiver {
13
    public static function init () {
14
        if (settings::$multi) {
15
            multi::init();
16
        }
17
        else {
18
            if (lock::exist('BPT-HOOK')) {
19
                receiver::telegramVerify();
20
                BPT::$update = receiver::processUpdate();
21
                logger::write('Update received , lets process it ;)');
22
            }
23
            else {
24
                self::deleteOldLocks();
25
                self::checkURL();
26
                self::setCertificate();
27
                $url = self::setURL();
28
                self::setWebhook($url);
29
            }
30
        }
31
    }
32
33
    private static function deleteOldLocks() {
34
        if (lock::exist('BPT-MULTI-EXEC')) {
35
            lock::delete('BPT-MULTI-EXEC');
36
        }
37
        if (lock::exist('BPT-MULTI-CURL')) {
38
            lock::delete('BPT-MULTI-CURL');
39
        }
40
        if (lock::exist('getUpdate')) {
41
            lock::delete('getUpdate');
42
        }
43
    }
44
45
    protected static function setWebhook(string $url) {
46
        $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

46
        /** @scrutinizer ignore-call */ $res = telegram::setWebhook($url, settings::$certificate, max_connections:settings::$max_connection, allowed_updates : settings::$allowed_updates);
Loading history...
47
        if ($res->ok) {
48
            lock::set('BPT');
49
            logger::write('Webhook was set successfully','info');
50
            BPT::exit('Done');
51
        }
52
        else {
53
            logger::write("There is some problem happened , telegram response : \n".json_encode($res),'error');
54
            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

54
            BPT::exit(/** @scrutinizer ignore-type */ print_r($res,true));
Loading history...
55
        }
56
    }
57
58
    protected static function checkURL() {
59
        if (!(isset($_SERVER['SERVER_NAME']) && isset($_SERVER['REQUEST_URI']))) {
60
            logger::write('For using webhook receiver , you should open this file in your webserver(by domain)','error');
61
            BPT::exit();
62
        }
63
    }
64
65
    private static function setURL(): string {
66
        return (isset(settings::$certificate) ? 'http://' : 'https://') . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
67
    }
68
69
    protected static function setCertificate() {
70
        if (isset(settings::$certificate)) {
71
            if (is_string(settings::$certificate)) {
72
                if (file_exists(settings::$certificate)) {
73
                    settings::$certificate = new CURLFile(settings::$certificate);
74
                }
75
                else {
76
                    settings::$certificate = null;
77
                }
78
            }
79
        }
80
    }
81
}