Core::dispatch()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 21
rs 9.8333
1
<?php
2
3
4
namespace CaioLandgraf\Api;
5
6
7
abstract class Core
8
{
9
    /** @var string */
10
    private $apiUrl;
11
12
    /** @var array */
13
    private $headers;
14
15
    /** @var array */
16
    private $fields;
17
18
    /** @var string */
19
    private $endpoint;
20
21
    /** @var string */
22
    private $method;
23
24
    /** @var object */
25
    protected $response;
26
27
    /**
28
     * Core constructor.
29
     * @param string $apiUrl
30
     */
31
    public function __construct(string $apiUrl)
32
    {
33
        $this->apiUrl = $apiUrl;
34
    }
35
36
    /**
37
     * @param string $method
38
     * @param string $endpoint
39
     * @param array|null $fields
40
     * @param array|null $headers
41
     */
42
    protected function request(string $method, string $endpoint, array $fields = null, array $headers = null): void
43
    {
44
        $this->method = $method;
45
        $this->endpoint = $endpoint;
46
        $this->fields = $fields;
47
        $this->headers($headers);
48
49
        $this->dispatch();
50
    }
51
52
    /**
53
     * @return object|null
54
     */
55
    public function response()
56
    {
57
        return $this->response;
58
    }
59
60
    /**
61
     * @return object|null
62
     */
63
    public function error()
64
    {
65
        if (!empty($this->response->errors)) {
66
            return $this->response->errors;
67
        }
68
69
        return null;
70
    }
71
72
    /**
73
     * @param array|null $headers
74
     */
75
    private function headers(?array $headers): void
76
    {
77
        if (!$headers) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $headers of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
78
            return;
79
        }
80
81
        foreach ($headers as $key => $header) {
82
            $this->headers[] = "{$key}: {$header}";
83
        }
84
    }
85
86
    /**
87
     *
88
     */
89
    private function dispatch(): void
90
    {
91
        $curl = curl_init();
92
93
        if (empty($this->fields["files"])) {
94
            $this->fields = (!empty($this->fields) ? http_build_query($this->fields) : null);
0 ignored issues
show
Documentation Bug introduced by
It seems like ! empty($this->fields) ?...y($this->fields) : null can also be of type string. However, the property $fields is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
95
        }
96
97
        curl_setopt_array($curl, array(
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, 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

97
        curl_setopt_array(/** @scrutinizer ignore-type */ $curl, array(
Loading history...
98
            CURLOPT_URL => "{$this->apiUrl}/{$this->endpoint}",
99
            CURLOPT_RETURNTRANSFER => true,
100
            CURLOPT_MAXREDIRS => 10,
101
            CURLOPT_TIMEOUT => 30,
102
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
103
            CURLOPT_CUSTOMREQUEST => $this->method,
104
            CURLOPT_POSTFIELDS => $this->fields,
105
//            CURLOPT_HTTPHEADER => $this->headers,
106
        ));
107
108
        $this->response = json_decode(curl_exec($curl));
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, 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

108
        $this->response = json_decode(curl_exec(/** @scrutinizer ignore-type */ $curl));
Loading history...
109
        curl_close($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, 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

109
        curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
110
    }
111
}