| Conditions | 16 |
| Paths | 94 |
| Total Lines | 113 |
| 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 |
||
| 103 | protected function request( $method, $path, $body = array() ) { |
||
| 104 | |||
| 105 | if ( ! class_exists( 'Jetpack_Data' ) ) { |
||
| 106 | return new WP_Error( |
||
| 107 | 'jetpack_data_class_not_found', |
||
| 108 | __( 'Unable to send request to WooCommerce Connect server. Jetpack_Data was not found.', 'woocommerce-gateway-stripe' ) |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | |||
| 112 | if ( ! method_exists( 'Jetpack_Data', 'get_access_token' ) ) { |
||
| 113 | return new WP_Error( |
||
| 114 | 'jetpack_data_get_access_token_not_found', |
||
| 115 | __( 'Unable to send request to WooCommerce Connect server. Jetpack_Data does not implement get_access_token.', 'woocommerce-gateway-stripe' ) |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | if ( ! is_array( $body ) ) { |
||
| 120 | return new WP_Error( |
||
| 121 | 'request_body_should_be_array', |
||
| 122 | __( 'Unable to send request to WooCommerce Connect server. Body must be an array.', 'woocommerce-gateway-stripe' ) |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | |||
| 126 | $url = trailingslashit( WOOCOMMERCE_CONNECT_SERVER_URL ); |
||
| 127 | $url = apply_filters( 'wc_connect_server_url', $url ); |
||
| 128 | $url = trailingslashit( $url ) . ltrim( $path, '/' ); |
||
| 129 | |||
| 130 | // Add useful system information to requests that contain bodies. |
||
| 131 | if ( in_array( $method, array( 'POST', 'PUT' ), true ) ) { |
||
| 132 | $body = $this->request_body( $body ); |
||
| 133 | $body = wp_json_encode( apply_filters( 'wc_connect_api_client_body', $body ) ); |
||
| 134 | |||
| 135 | if ( ! $body ) { |
||
| 136 | return new WP_Error( |
||
| 137 | 'unable_to_json_encode_body', |
||
| 138 | __( 'Unable to encode body for request to WooCommerce Connect server.', 'woocommerce-gateway-stripe' ) |
||
| 139 | ); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | $headers = $this->request_headers(); |
||
|
|
|||
| 144 | if ( is_wp_error( $headers ) ) { |
||
| 145 | return $headers; |
||
| 146 | } |
||
| 147 | |||
| 148 | $http_timeout = 60; // 1 minute |
||
| 149 | if ( function_exists( 'wc_set_time_limit' ) ) { |
||
| 150 | wc_set_time_limit( $http_timeout + 10 ); |
||
| 151 | } |
||
| 152 | $args = array( |
||
| 153 | 'headers' => $headers, |
||
| 154 | 'method' => $method, |
||
| 155 | 'body' => $body, |
||
| 156 | 'redirection' => 0, |
||
| 157 | 'compress' => true, |
||
| 158 | 'timeout' => $http_timeout, |
||
| 159 | ); |
||
| 160 | |||
| 161 | $args = apply_filters( 'wc_connect_request_args', $args ); |
||
| 162 | $response = wp_remote_request( $url, $args ); |
||
| 163 | $response_code = wp_remote_retrieve_response_code( $response ); |
||
| 164 | $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
||
| 165 | |||
| 166 | if ( false === strpos( $content_type, 'application/json' ) ) { |
||
| 167 | if ( 200 !== $response_code ) { |
||
| 168 | return new WP_Error( |
||
| 169 | 'wcc_server_error', |
||
| 170 | sprintf( |
||
| 171 | // Translators: HTTP error code. |
||
| 172 | __( 'Error: The WooCommerce Connect server returned HTTP code: %d', 'woocommerce-gateway-stripe' ), |
||
| 173 | $response_code |
||
| 174 | ) |
||
| 175 | ); |
||
| 176 | } |
||
| 177 | return $response; |
||
| 178 | } |
||
| 179 | |||
| 180 | $response_body = wp_remote_retrieve_body( $response ); |
||
| 181 | if ( ! empty( $response_body ) ) { |
||
| 182 | $response_body = json_decode( $response_body ); |
||
| 183 | } |
||
| 184 | |||
| 185 | if ( 200 !== $response_code ) { |
||
| 186 | if ( empty( $response_body ) ) { |
||
| 187 | return new WP_Error( |
||
| 188 | 'wcc_server_empty_response', |
||
| 189 | sprintf( |
||
| 190 | // Translators: HTTP error code. |
||
| 191 | __( 'Error: The WooCommerce Connect server returned ( %d ) and an empty response body.', 'woocommerce-gateway-stripe' ), |
||
| 192 | $response_code |
||
| 193 | ) |
||
| 194 | ); |
||
| 195 | } |
||
| 196 | |||
| 197 | $error = property_exists( $response_body, 'error' ) ? $response_body->error : ''; |
||
| 198 | $message = property_exists( $response_body, 'message' ) ? $response_body->message : ''; |
||
| 199 | $data = property_exists( $response_body, 'data' ) ? $response_body->data : ''; |
||
| 200 | |||
| 201 | return new WP_Error( |
||
| 202 | 'wcc_server_error_response', |
||
| 203 | sprintf( |
||
| 204 | /* translators: %1$s: error code, %2$s: error message, %3$d: HTTP response code */ |
||
| 205 | __( 'Error: The WooCommerce Connect server returned: %1$s %2$s ( %3$d )', 'woocommerce-gateway-stripe' ), |
||
| 206 | $error, |
||
| 207 | $message, |
||
| 208 | $response_code |
||
| 209 | ), |
||
| 210 | $data |
||
| 211 | ); |
||
| 212 | } |
||
| 213 | |||
| 214 | return $response_body; |
||
| 215 | } |
||
| 216 | |||
| 366 |