Passed
Push — main ( 926a07...7834cc )
by Miaad
10:19
created

curl::setData()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 3
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 4
rs 9.6111
1
<?php
2
3
namespace BPT\telegram\request;
4
5
use BPT\constants\loggerTypes;
6
use BPT\logger;
7
use BPT\settings;
8
use CurlHandle;
9
10
/**
11
 * curl class , part of request class for handling request based on curl
12
 */
13
class curl {
14
    private static CurlHandle $curl_handler;
15
16
    /**
17
     * @internal Only for BPT self usage , Don't use it in your source!
18
     */
19
    public static function init(string $method,array $data) {
20
        $info = self::getInfo($data);
21
        $data = $info['data'];
22
        $handler = $info['handler'];
23
        self::setTimeout($data,$handler,$method);
0 ignored issues
show
Bug introduced by
It seems like $handler can also be of type resource; however, parameter $curl_handler of BPT\telegram\request\curl::setTimeout() does only seem to accept CurlHandle, 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

23
        self::setTimeout($data,/** @scrutinizer ignore-type */ $handler,$method);
Loading history...
24
        self::setData($data);
25
        $data['method'] = $method;
26
        curl_setopt($handler, CURLOPT_POSTFIELDS, $data);
27
        $result = curl_exec($handler);
28
        if (curl_errno($handler)) {
29
            logger::write(curl_error($handler),loggerTypes::WARNING);
30
        }
31
        if ($info['token'] != settings::$token) {
32
            curl_close($handler);
33
        }
34
        return json_decode($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $json of json_decode() does only seem to accept 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

34
        return json_decode(/** @scrutinizer ignore-type */ $result);
Loading history...
35
    }
36
37
    private static function getInfo(array $data): array {
38
        if (isset($data['token']) && $data['token'] !== settings::$token) {
39
            $token = $data['token'];
40
            unset($data['token']);
41
            $curl_handler = curl_init(settings::$base_url."/bot$token/");
42
            curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, true);
43
            curl_setopt($curl_handler, CURLOPT_SSL_VERIFYPEER, false);
44
        }
45
        else{
46
            $token = settings::$token;
47
            if (!isset(self::$curl_handler)){
48
                self::$curl_handler = curl_init(settings::$base_url."/bot$token/");
49
                curl_setopt(self::$curl_handler, CURLOPT_RETURNTRANSFER, true);
50
                curl_setopt(self::$curl_handler, CURLOPT_SSL_VERIFYPEER, false);
51
                curl_setopt(self::$curl_handler, CURLOPT_TCP_KEEPALIVE, 1);
52
            }
53
            $curl_handler = self::$curl_handler;
54
        }
55
56
        return [
57
            'data'    => $data,
58
            'token'   => $token,
59
            'handler' => $curl_handler
60
        ];
61
    }
62
63
    private static function setTimeout(array &$data , CurlHandle $curl_handler,string $method): void {
64
        if (isset($data['forgot'])) {
65
            curl_setopt($curl_handler, CURLOPT_TIMEOUT_MS, settings::$forgot_time);
66
            unset($data['forgot']);
67
        }
68
        elseif ($method === 'getUpdates' || $method === 'setWebhook'){
69
            curl_setopt($curl_handler, CURLOPT_TIMEOUT_MS, 5000);
70
        }
71
        else{
72
            curl_setopt($curl_handler, CURLOPT_TIMEOUT_MS, settings::$base_timeout);
73
        }
74
    }
75
76
    private static function setData(array &$data): void {
77
        foreach ($data as &$value){
78
            if (is_array($value) || (is_object($value) && !is_a($value,'CURLFile'))){
79
                $value = json_encode($value);
80
            }
81
        }
82
    }
83
}