Passed
Push — master ( f3637e...ec11f6 )
by Gabriel
01:55
created

Octadesk::request()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace codingFive0\octadesk;
4
5
/**
6
 * Class Octadesk
7
 */
8
abstract class Octadesk
9
{
10
    /** @var string <b>E-mail</b> cadastrado na octadesk */
11
    protected $userName;
12
    /** @var string <b>Senha</b> de acesso na octadesk */
13
    protected $userPassword;
14
    /** @var string <b>Token</b> obtido apartir do painel de controle da octadesk */
15
    protected $apiToken;
16
    /** @var string <b><em>xxxxx</em>.octadesk.com<b/> nome do sub-dominio na octadesk. (apenas até o primeiro ponto "." ) */
17
    protected $subDomain;
18
19
    /** @var string <b>Base</b> da API. Todas as requisições serão direceionadas a esta URL. Com a adição do endpoint final */
20
    private $apiUrl;
21
    /** @var string <b>Endpoint</b> Independende a cada solicitação e cada objetivo. */
22
    private $endpoint;
23
    /** @var string <b>Metodo<b/> da requisição */
24
    private $method;
25
    /** @var array <b>Campos</b> para postagem no metodo <em>POST</em> */
26
    private $fields;
27
28
    /** @var array <b>headers</b> para envio junto a requisição CURL */
29
    private $headers;
30
31
    /** @var ?object Retorno de dados por parte da octadesk */
32
    protected $callback;
33
34
    /** @var string <b>Erro</b> por mensagem do <em>Componente</em>. Sem interação com a API */
35
    protected $error;
36
37
    /** @var string <b>Token</b> retornado a partir do login no API */
38
    protected $accesToken;
39
40
41
    public function __construct($apiToken = null, $userName = null, $userPassword = null, $subDomain = null)
42
    {
43
        $this->userName = $userName;
44
        $this->apiToken = $apiToken;
45
        $this->userPassword = $userPassword;
46
        $this->subDomain = $subDomain;
47
48
        $this->apiUrl = "https://api.octadesk.services";
49
    }
50
51
    protected function setAccesToken($accToken)
52
    {
53
        $this->headers[] = "Authorization: Bearer " . $accToken;
54
        return $this;
55
    }
56
57
    /**
58
     * @param string $method
59
     * @param string $endpoint
60
     * @param array|null $fields
61
     * @param array|null $headers
62
     */
63
    protected function request($method, $endpoint, $fields = null, $headers = null, $toJson = false)
64
    {
65
        $this->method = $method;
66
        $this->endpoint = $endpoint;
67
        $this->fields = (empty($toJson) ? $fields : json_encode($fields));
0 ignored issues
show
Documentation Bug introduced by
It seems like empty($toJson) ? $fields : json_encode($fields) 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...
68
        $this->setHeaders($headers);
69
70
        $this->dispatch();
71
    }
72
73
    public function getError()
74
    {
75
        return $this->error;
76
    }
77
78
    /**
79
     * @return object|null
80
     */
81
    public function callback()
82
    {
83
        return $this->callback;
84
    }
85
86
    /**
87
     * @return object|null
88
     */
89
    public function error()
90
    {
91
        if (!empty($this->callback->errorId)) {
92
            return $this->callback->message;
93
        }
94
95
        return null;
96
    }
97
98
    /**
99
     * @param array|null $headers
100
     */
101
    private function setHeaders($headers = null)
102
    {
103
        if (!$headers) {
104
            return;
105
        }
106
107
        foreach ($headers as $key => $header) {
108
            $this->headers[] = "{$key}: {$header}";
109
        }
110
    }
111
112
    /**
113
     *
114
     */
115
    private function dispatch()
116
    {
117
        $curl = curl_init();
118
119
        $get = ($this->method === "GET" ? "?" . http_build_query($this->fields) : null);
120
        $this->url = "{$this->apiUrl}/{$this->endpoint}{$get}";
0 ignored issues
show
Bug Best Practice introduced by
The property url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
121
122
        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

122
        curl_setopt_array(/** @scrutinizer ignore-type */ $curl, array(
Loading history...
123
            CURLOPT_URL => "{$this->apiUrl}/{$this->endpoint}{$get}",
124
            CURLOPT_RETURNTRANSFER => true,
125
            CURLOPT_MAXREDIRS => 10,
126
            CURLOPT_TIMEOUT => 30,
127
            CURLOPT_FOLLOWLOCATION => true,
128
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
129
            CURLOPT_CUSTOMREQUEST => $this->method,
130
            CURLOPT_POSTFIELDS => $this->fields,
131
            CURLOPT_HTTPHEADER => $this->headers,
132
        ));
133
134
        $this->callback = 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

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

135
        curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
136
    }
137
138
    public function __clone()
139
    {
140
    }
141
}