Conditions | 16 |
Paths | 53 |
Total Lines | 47 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
20 | public function request($method, $url, $api_key, $data = NULL, $headers= array("Content-Type" => "application/json", "Accept" => "application/json") ) { |
||
21 | try { |
||
22 | $options = array( |
||
23 | 'auth' => new AuthBearer($api_key), |
||
24 | 'timeout' => 120 |
||
25 | ); |
||
26 | if($method == "GET") { |
||
27 | $url_params = is_array($data) ? '?' . http_build_query($data) : ''; |
||
28 | $response = \Requests::get(Culqi::$api_base . $url . $url_params, $headers, $options); |
||
29 | } else if($method == "POST") { |
||
30 | $response = \Requests::post(Culqi::$api_base . $url, $headers, json_encode($data), $options); |
||
31 | |||
32 | |||
33 | } else if($method == "PATCH") { |
||
34 | $response = \Requests::patch(Culqi::$api_base . $url, $headers, json_encode($data), $options); |
||
35 | } else if($method == "DELETE") { |
||
36 | $response = \Requests::delete(Culqi::$api_base, $options); |
||
37 | } |
||
38 | } catch (\Exception $e) { |
||
39 | throw new Errors\UnableToConnect(); |
||
40 | } |
||
41 | if ($response->status_code >= 200 && $response->status_code <= 206) { |
||
42 | if ($method == "DELETE") { |
||
43 | return $response->status_code == 204 || $response->status_code == 200; |
||
44 | } |
||
45 | return json_decode($response->body); |
||
46 | } |
||
47 | if ($response->status_code == 400) { |
||
48 | $code = 0; |
||
49 | $message = ""; |
||
50 | |||
51 | throw new Errors\UnhandledError($response->body, $response->status_code); |
||
52 | } |
||
53 | if ($response->status_code == 401) { |
||
54 | throw new Errors\AuthenticationError(); |
||
55 | } |
||
56 | if ($response->status_code == 404) { |
||
57 | throw new Errors\NotFound(); |
||
58 | } |
||
59 | if ($response->status_code == 403) { |
||
60 | throw new Errors\InvalidApiKey(); |
||
61 | } |
||
62 | if ($response->status_code == 405) { |
||
63 | throw new Errors\MethodNotAllowed(); |
||
64 | } |
||
65 | throw new Errors\UnhandledError($response->body, $response->status_code); |
||
66 | } |
||
67 | } |
||
68 |