Passed
Push — main ( e1a2dc...e011f6 )
by Miaad
02:06
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
eloc 3
dl 0
loc 4
rs 9.6111
c 0
b 0
f 0
cc 5
nc 3
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
        $token = $info['token'];
17
        $handler = $info['handler'];
18
        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

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

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

79
    private static function parseResult(/** @scrutinizer ignore-unused */ $result) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The method parseResult() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
80
81
    }
82
}