Conditions | 8 |
Paths | 32 |
Total Lines | 53 |
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 |
||
108 | private function send(Getnet $credentials, $url_path, $method, $json = null) |
||
109 | { |
||
110 | $curl = curl_init($this->getFullUrl($url_path)); |
||
111 | |||
112 | $defaultCurlOptions = [ |
||
113 | CURLOPT_CONNECTTIMEOUT => 60, |
||
114 | CURLOPT_RETURNTRANSFER => true, |
||
115 | CURLOPT_TIMEOUT => 60, |
||
116 | CURLOPT_HTTPHEADER => [ |
||
117 | 'Content-Type: application/json; charset=utf-8' |
||
118 | ], |
||
119 | CURLOPT_SSL_VERIFYHOST => 2, |
||
120 | CURLOPT_SSL_VERIFYPEER => 0 |
||
121 | ]; |
||
122 | |||
123 | if ($method == self::CURL_TYPE_POST) { |
||
124 | $defaultCurlOptions[CURLOPT_HTTPHEADER][] = 'Authorization: Bearer ' . $credentials->getAuthorizationToken(); |
||
125 | curl_setopt($curl, CURLOPT_POST, 1); |
||
126 | curl_setopt($curl, CURLOPT_POSTFIELDS, $json); |
||
127 | } elseif ($method == self::CURL_TYPE_PUT) { |
||
128 | $defaultCurlOptions[CURLOPT_HTTPHEADER][] = 'Authorization: Bearer ' . $credentials->getAuthorizationToken(); |
||
129 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, self::CURL_TYPE_PUT); |
||
130 | curl_setopt($curl, CURLOPT_POSTFIELDS, $json); |
||
131 | } elseif ($method == self::CURL_TYPE_AUTH) { |
||
132 | $defaultCurlOptions[CURLOPT_HTTPHEADER][0] = 'application/x-www-form-urlencoded'; |
||
133 | curl_setopt($curl, CURLOPT_USERPWD, $credentials->getClientId() . ":" . $credentials->getClientSecret()); |
||
134 | curl_setopt($curl, CURLOPT_POST, 1); |
||
135 | curl_setopt($curl, CURLOPT_POSTFIELDS, $json); |
||
136 | } |
||
137 | curl_setopt($curl, CURLOPT_ENCODING, ""); |
||
138 | curl_setopt_array($curl, $defaultCurlOptions); |
||
139 | |||
140 | try { |
||
141 | $response = curl_exec($curl); |
||
142 | } catch (Exception $e) { |
||
143 | print "ERROR"; |
||
144 | } |
||
145 | |||
146 | if (isset(json_decode($response)->error)) { |
||
147 | throw new Exception(json_decode($response)->error_description, 100); |
||
148 | } |
||
149 | |||
150 | if (curl_getinfo($curl, CURLINFO_HTTP_CODE) >= 400) { |
||
151 | throw new Exception($response, 100); |
||
152 | } |
||
153 | if (! $response) { |
||
154 | print "ERROR"; |
||
155 | exit(); |
||
156 | } |
||
157 | curl_close($curl); |
||
158 | |||
159 | return json_decode($response, true); |
||
160 | } |
||
161 | |||
220 |