CafeApi   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 109
rs 10
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A error() 0 7 2
A request() 0 8 1
A headers() 0 8 3
A dispatch() 0 21 3
A response() 0 3 1
A __construct() 0 6 1
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) {
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...
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(
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

106
        curl_setopt_array(/** @scrutinizer ignore-type */ $curl, array(
Loading history...
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));
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

117
        $this->response = json_decode(curl_exec(/** @scrutinizer ignore-type */ $curl));
Loading history...
118
        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

118
        curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
119
    }
120
}