Issues (13)

src/Octadesk.php (3 issues)

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
        if($toJson){
71
            $this->headers[] = "Content-Type: application/json";
72
            $this->headers[] = "accept: application/json";
73
        }
74
75
        $this->dispatch();
76
    }
77
78
    public function getError()
79
    {
80
        return $this->error;
81
    }
82
83
    /**
84
     * @return object|null
85
     */
86
    public function callback()
87
    {
88
        return $this->callback;
89
    }
90
91
    /**
92
     * @return object|null
93
     */
94
    public function error()
95
    {
96
        if (!empty($this->callback->errorId)) {
97
            return $this->callback->message;
98
        }
99
100
        return null;
101
    }
102
103
    /**
104
     * @param array|null $headers
105
     */
106
    private function setHeaders($headers = null)
107
    {
108
        if (!$headers) {
109
            return;
110
        }
111
112
        foreach ($headers as $key => $header) {
113
            $this->headers[] = "{$key}: {$header}";
114
        }
115
    }
116
117
    /**
118
     *
119
     */
120
    private function dispatch()
121
    {
122
        $curl = curl_init();
123
124
        $get = ($this->method === "GET" ? "?" . http_build_query($this->fields) : null);
125
        $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...
126
127
        curl_setopt_array($curl, array(
128
            CURLOPT_URL => "{$this->apiUrl}/{$this->endpoint}{$get}",
129
            CURLOPT_RETURNTRANSFER => true,
130
            CURLOPT_MAXREDIRS => 10,
131
            CURLOPT_TIMEOUT => 30,
132
            CURLOPT_FOLLOWLOCATION => true,
133
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
134
            CURLOPT_CUSTOMREQUEST => $this->method,
135
            CURLOPT_POSTFIELDS => $this->fields,
136
            CURLOPT_HTTPHEADER => $this->headers,
137
        ));
138
139
        $this->callback = json_decode(curl_exec($curl));
0 ignored issues
show
It seems like curl_exec($curl) 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

139
        $this->callback = json_decode(/** @scrutinizer ignore-type */ curl_exec($curl));
Loading history...
140
        curl_close($curl);
141
    }
142
143
    public function __clone()
144
    {
145
    }
146
}