Passed
Push — main ( 018447...f5f6ad )
by Miaad
01:31
created

curl::parseResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BPT\api\request;
4
5
use BPT\constants\loggerTypes;
6
use BPT\logger;
7
use BPT\settings;
8
use CurlHandle;
9
10
class curl {
11
    private static CurlHandle $curl_handler;
12
13
    public static function init(string $method,array $data) {
14
        $info = self::getInfo($data);
15
        $data = $info['data'];
16
        $handler = $info['handler'];
17
        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\api\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

17
        self::setTimeout($data,/** @scrutinizer ignore-type */ $handler,$method);
Loading history...
18
        self::setData($data);
19
        $data['method'] = $method;
20
        curl_setopt($handler, CURLOPT_POSTFIELDS, $data);
21
        $result = curl_exec($handler);
22
        if (curl_errno($handler)) {
23
            logger::write(curl_error($handler),loggerTypes::WARNING);
24
        }
25
        if ($info['token'] != settings::$token) {
26
            curl_close($handler);
27
        }
28
        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

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