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

answer::deleteAdditionalData()   B

Complexity

Conditions 9
Paths 32

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 17
rs 8.0555
c 0
b 0
f 0
cc 9
nc 32
nop 1
1
<?php
2
3
namespace BPT\api\request;
4
5
use BPT\BPT;
6
use BPT\constants\loggerTypes;
7
use BPT\constants\receiver;
8
use BPT\logger;
9
use BPT\settings;
10
11
class answer {
12
    private static bool $is_answered = false;
13
14
    public static function init(string $method,array $data) {
15
        self::checkAnswered();
16
        self::checkWebhook();
17
        self::deleteAdditionalData($data);
18
        self::$is_answered = true;
19
        $data['method'] = $method;
20
        $payload = json_encode($data);
21
        header('Content-Type: application/json;Content-Length: ' . strlen($payload));
22
        echo $payload;
23
        return true;
24
    }
25
26
    private static function checkAnswered() {
27
        if (self::$is_answered) {
28
            logger::write('You can use answer mode only once for each webhook update , You already did it!',loggerTypes::ERROR);
29
            BPT::exit();
30
        }
31
    }
32
33
    private static function checkWebhook() {
34
        if(settings::$receiver === receiver::GETUPDATES) {
35
            logger::write('Answer mode only work when receiver is webhook',loggerTypes::ERROR);
36
            BPT::exit();
37
        }
38
39
        if(settings::$multi) {
40
            logger::write('You can not use answer mode when multi setting is on',loggerTypes::ERROR);
41
            BPT::exit();
42
        }
43
    }
44
45
    private static function deleteAdditionalData(array &$data) {
46
        if (isset($data['token'])) {
47
            unset($data['token']);
48
        }
49
        if (isset($data['forgot'])) {
50
            unset($data['forgot']);
51
        }
52
        if (isset($data['return_array'])) {
53
            unset($data['return_array']);
54
        }
55
        foreach ($data as $key=>&$value){
56
            if (!isset($value)){
57
                unset($data[$key]);
58
                continue;
59
            }
60
            if (is_array($value) || (is_object($value) && !is_a($value,'CURLFile'))){
61
                $value = json_encode($value);
62
            }
63
        }
64
    }
65
}