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