| Conditions | 22 |
| Paths | 40 |
| Total Lines | 81 |
| Code Lines | 59 |
| 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 |
||
| 130 | protected function fromFile($image, $callApiUrl) |
||
| 131 | {
|
||
| 132 | $fileData = @file_get_contents($image); |
||
| 133 | $contentHash = md5($image); |
||
| 134 | $boundary = "XXX$contentHash"; |
||
| 135 | $nameEscaped = addslashes(basename($image)); |
||
| 136 | |||
| 137 | $options[ 'content' ] = "--$boundary\r\n" . |
||
| 138 | "Content-Disposition: form-data; name=\"file\"; filename=\"{$nameEscaped}\"\r\n" .
|
||
| 139 | "Content-Type: application/octet-stream\r\n" . |
||
| 140 | "Content-Transfer-Encoding: binary\r\n" . |
||
| 141 | "\r\n$fileData\r\n--$boundary--"; |
||
| 142 | |||
| 143 | $options[ 'header' ] = |
||
| 144 | "Accept: image/*,application/im2+json\r\n" . |
||
| 145 | "User-Agent: ImageOptim-php/1.1 PHP/" . phpversion() . |
||
| 146 | "Content-Length: " . strlen($options[ 'content' ]) . "\r\n" . |
||
| 147 | "Content-MD5: $contentHash\r\n" . |
||
| 148 | "Content-Type: multipart/form-data, boundary=$boundary\r\n"; |
||
| 149 | |||
| 150 | $options[ 'timeout' ] = 30; |
||
| 151 | $options[ 'ignore_errors' ] = true; |
||
| 152 | $options[ 'method' ] = 'POST'; |
||
| 153 | |||
| 154 | $stream = @fopen($callApiUrl, 'r', false, stream_context_create(['http' => $options])); |
||
| 155 | if ( ! $stream) {
|
||
| 156 | $error = error_get_last(); |
||
| 157 | throw new RuntimeException("Can't send HTTPS request to: $callApiUrl\n" . ($error ? $error[ 'message' ] : ''));
|
||
| 158 | } |
||
| 159 | $response = @stream_get_contents($stream); |
||
| 160 | if ( ! $response) {
|
||
| 161 | $error = error_get_last(); |
||
| 162 | fclose($stream); |
||
| 163 | throw new RuntimeException("Error reading HTTPS response from: $callApiUrl\n" . ($error ? $error[ 'message' ] : ''));
|
||
| 164 | } |
||
| 165 | $meta = @stream_get_meta_data($stream); |
||
| 166 | if ( ! $meta) {
|
||
| 167 | $error = error_get_last(); |
||
| 168 | fclose($stream); |
||
| 169 | throw new RuntimeException("Error reading HTTPS response from: $callApiUrl\n" . ($error ? $error[ 'message' ] : ''));
|
||
| 170 | } |
||
| 171 | fclose($stream); |
||
| 172 | if ( ! $meta || ! isset($meta[ 'wrapper_data' ], $meta[ 'wrapper_data' ][ 0 ])) {
|
||
| 173 | throw new RuntimeException("Unable to read headers from HTTP request to: $callApiUrl");
|
||
| 174 | } |
||
| 175 | if ( ! empty($meta[ 'timed_out' ])) {
|
||
| 176 | throw new RuntimeException("Request timed out: $callApiUrl", 504);
|
||
| 177 | } |
||
| 178 | if ( ! preg_match('/HTTP\/[\d.]+ (\d+) (.*)/', $meta[ 'wrapper_data' ][ 0 ], $status)) {
|
||
| 179 | throw new RuntimeException("Unexpected response: " . $meta[ 'wrapper_data' ][ 0 ]);
|
||
| 180 | } |
||
| 181 | $status = intval($status[ 1 ]); |
||
| 182 | $errorMessage = $status[ 2 ]; |
||
| 183 | if ($response && preg_grep('/content-type:\s*application\/im2\+json/i', $meta[ 'wrapper_data' ])) {
|
||
| 184 | $json = @json_decode($response); |
||
| 185 | if ($json) {
|
||
| 186 | if (isset($json->status)) {
|
||
| 187 | $status = $json->status; |
||
| 188 | } |
||
| 189 | if (isset($json->error)) {
|
||
| 190 | $errorMessage = $json->error; |
||
| 191 | } |
||
| 192 | if (isset($json->code) && $json->code === 'IM2ACCOUNT') {
|
||
| 193 | throw new RuntimeException($errorMessage, $status); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | if ($status >= 500) {
|
||
| 198 | throw new RuntimeException($errorMessage, $status); |
||
| 199 | } |
||
| 200 | if ($status == 404) {
|
||
| 201 | throw new RuntimeException("Could not find the image: {$image}", $status);
|
||
| 202 | } |
||
| 203 | if ($status == 403) {
|
||
| 204 | throw new RuntimeException("Origin server denied access to {$image}", $status);
|
||
| 205 | } |
||
| 206 | if ($status >= 400) {
|
||
| 207 | throw new RuntimeException($errorMessage, $status); |
||
| 208 | } |
||
| 209 | |||
| 210 | return $response; |
||
| 211 | } |
||
| 212 | } |