Issues (7)

src/CafeApi.php (1 issue)

1
<?php
2
3
namespace CarlosBrito\CafeApi;
4
5
/**
6
 * Class CafeApi
7
 * @package RobsonVLeite\CafeApi
8
 * Callse Abstract e não pode ser instanciada, apenas herdar dela suas propriedades
9
 */
10
abstract class CafeApi
11
{
12
    /** @var string */
13
    private $apiUrl;
14
15
    /** @var array */
16
    private $headers;
17
18
    /** @var array */
19
    private $fields;
20
21
    /** @var string */
22
    private $endpoint;
23
24
    /** @var string */
25
    private $method;
26
27
    /** @var object */
28
    protected $response;
29
30
    /**
31
     * CafeApi constructor.
32
     * @param string $apiUrl
33
     * @param string $email
34
     * @param string $password
35
     */
36
    public function __construct(string $apiUrl, string $email, string $password)
37
    {
38
        $this->apiUrl = $apiUrl;
39
        $this->headers([
40
            "email" => $email,
41
            "password" => $password
42
        ]);
43
    }
44
45
    /**
46
     * @param string $method
47
     * @param string $endpoint
48
     * @param array|null $fields
49
     * @param array|null $headers
50
     */
51
    protected function request(string $method, string $endpoint, array $fields = null, array $headers = null): void
52
    {
53
        $this->method = $method;
54
        $this->endpoint = $endpoint;
55
        $this->fields = $fields;
56
        $this->headers($headers);
57
58
        $this->dispatch();
59
    }
60
61
    /**
62
     * @return object|null
63
     */
64
    public function response()
65
    {
66
        return $this->response;
67
    }
68
69
    /**
70
     * @return object|null
71
     */
72
    public function error()
73
    {
74
        if (!empty($this->response->errors)) {
75
            return $this->response->errors;
76
        }
77
78
        return null;
79
    }
80
81
    /**
82
     * @param array|null $headers
83
     */
84
    private function headers(?array $headers): void
85
    {
86
        if (!$headers) {
87
            return;
88
        }
89
90
        foreach ($headers as $key => $header) {
91
            $this->headers[] = "{$key}: {$header}";
92
        }
93
    }
94
95
    /**
96
     *
97
     */
98
    private function dispatch(): void
99
    {
100
        $curl = curl_init();
101
102
        if (empty($this->fields["files"])) {
103
            $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...
104
        }
105
106
        curl_setopt_array($curl, array(
107
            CURLOPT_URL => "{$this->apiUrl}/{$this->endpoint}",
108
            CURLOPT_RETURNTRANSFER => true,
109
            CURLOPT_MAXREDIRS => 10,
110
            CURLOPT_TIMEOUT => 30,
111
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
112
            CURLOPT_CUSTOMREQUEST => $this->method,
113
            CURLOPT_POSTFIELDS => $this->fields,
114
            CURLOPT_HTTPHEADER => $this->headers,
115
        ));
116
117
        $this->response = json_decode(curl_exec($curl));
118
        curl_close($curl);
119
    }
120
}