Passed
Push — master ( f9ac31...c95bf2 )
by André De
01:22
created

ViaCepApi::checkNullResponse()   C

Complexity

Conditions 14
Paths 17

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 14
eloc 11
c 2
b 0
f 0
nc 17
nop 0
dl 0
loc 17
rs 6.2666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AndreDeBrito\PHPViaCep;
4
5
/**
6
 * Class ViaCepApi 
7
 *
8
 * @author André de Brito <https://github.com/andredebrito>
9
 * @package AndredeBrito\PHPViaCep
10
 */
11
abstract class ViaCepApi {
12
13
    /** @var string */
14
    private $apiUrl;
15
16
    /** @var string */
17
    private $endpoint;
18
19
    /** @var string */
20
    private $method;
21
22
    /** @var mixed */
23
    protected $response;
24
25
    /** @var string */
26
    protected $responseType;
27
28
    /** @var sring */
0 ignored issues
show
Bug introduced by
The type AndreDeBrito\PHPViaCep\sring was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
    protected $error;
30
31
    /**
32
     * ViaCepAPi constructor
33
     */
34
    public function __construct() {
35
        $this->apiUrl = "https://viacep.com.br/ws";
36
    }
37
38
    /**
39
     * Set endpoint
40
     * 
41
     * @param string $endpoint
42
     * @return void
43
     */
44
    public function setEndpoint(string $endpoint): void {
45
        $this->endpoint = $endpoint;
46
    }
47
48
    /**
49
     * Return response
50
     * 
51
     * @return mixed
52
     */
53
    public function getResponse() {
54
        return $this->response = $this->checkNullResponse();         
55
    }
56
57
    /**
58
     * 
59
     * @return string
60
     */
61
    public function getError(): ?string {
62
        return $this->error;
63
    }
64
65
    /**
66
     * Set API return type to JSON
67
     * 
68
     * @return \AndreDeBrito\PHPViaCep\ViaCepApi|null
69
     */
70
    public function json(): ?ViaCepApi {
71
        $this->endpoint .= "json/";
72
        $this->responseType = "json";
73
        return $this;
74
    }
75
76
    /**
77
     * Set API return type do JSON Unicode
78
     * 
79
     * @return \AndreDeBrito\PHPViaCep\ViaCepApi|null
80
     */
81
    public function jsonUnicode(): ?ViaCepApi {
82
        $this->endpoint .= "json/unicode/";
83
        $this->responseType = "json";
84
        return $this;
85
    }
86
87
    /**
88
     * Set API return type to XML
89
     * 
90
     * @return \AndreDeBrito\PHPViaCep\ViaCepApi|null
91
     */
92
    public function xml(): ?ViaCepApi {
93
        $this->endpoint .= "xml/";
94
        $this->responseType = "xml";
95
        return $this;
96
    }
97
98
    /**
99
     * Set API return type to PIPED
100
     * 
101
     * @return \AndreDeBrito\PHPViaCep\ViaCepApi|null
102
     */
103
    public function piped(): ?ViaCepApi {
104
        $this->endpoint .= "piped/";
105
        $this->responseType = "pided";
106
        return $this;
107
    }
108
109
    /**
110
     * Set API return type to QUERTY
111
     * 
112
     * @return \AndreDeBrito\PHPViaCep\ViaCepApi|null
113
     */
114
    public function querty(): ?ViaCepApi {
115
        $this->endpoint .= "querty/";
116
        $this->responseType = "querty";
117
        return $this;
118
    }
119
120
    /**
121
     * <b>API JSON return only</b>
122
     * Set JSON response to Object
123
     * 
124
     * @return \AndreDeBrito\PHPViaCep\ViaCepApi|null
125
     */
126
    public function jsonToObject(): ?ViaCepApi {
127
      if ($this->responseType == "json" && $this->response != "[]") {
128
            $this->responseType = "object";
129
            $this->response = (object) json_decode($this->response);
130
        }
131
       
132
       return $this;       
133
    }
134
135
    /**
136
     * Fetch the request
137
     * 
138
     * @return \AndreDeBrito\PHPViaCep\ViaCepApi|null
139
     */
140
    public function fetch(): ?ViaCepApi {
141
        if (!$this->error) {
142
            $this->request("GET", $this->endpoint);
143
            return $this;
144
        }
145
146
        return $this;
147
    }
148
149
    /**
150
     * Call the request
151
     * 
152
     * @param string $method
153
     * @param string $endpoint
154
     * @return void
155
     */
156
    protected function request(string $method, string $endpoint): void {
157
        $this->method = $method;
158
        $this->endpoint = $endpoint;
159
160
        $this->dispatch();
161
    }
162
163
    /**
164
     * Execute the request via CURL
165
     * @return void
166
     */
167
    private function dispatch(): void {
168
        $curl = curl_init();
169
170
        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

170
        curl_setopt_array(/** @scrutinizer ignore-type */ $curl, array(
Loading history...
171
            CURLOPT_URL => "{$this->apiUrl}/{$this->endpoint}",
172
            CURLOPT_RETURNTRANSFER => true,
173
            CURLOPT_MAXREDIRS => 10,
174
            CURLOPT_TIMEOUT => 30,
175
            CURLOPT_HTTP_VERSION => "CURL_HTTP_VERSION_1_1",
176
            CURLOPT_CUSTOMREQUEST => $this->method
177
        ));
178
179
        $this->response = 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

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

180
        curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
181
    }
182
183
    private function checkNullResponse() {
184
        switch ($this->responseType) {
185
186
            case "object":
187
                return (!empty($this->response->erro) ? null : $this->response);
188
189
            case "json":
190
                return ($this->response && in_array("erro", json_decode($this->response)) || ($this->response == "[]") ? null : $this->response);
191
192
            case "xml":
193
                return (!empty(simplexml_load_string($this->response)->erro) || empty(simplexml_load_string($this->response)->enderecos) ? null : $this->response);
194
195
            case "pided":
196
                return ($this->response == "erro:true" ? null : $this->response);
197
198
            case "querty":
199
                return ($this->response == "erro=true" ? null : $this->response);
200
        }
201
    }
202
203
}
204