| Conditions | 7 |
| Paths | 12 |
| Total Lines | 57 |
| Code Lines | 27 |
| 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 |
||
| 122 | private function request( $method, $endpoint, $token, $object = null ) { |
||
| 123 | // URL. |
||
| 124 | $url = $this->get_url() . $endpoint; |
||
| 125 | |||
| 126 | // Arguments. |
||
| 127 | $args = array( |
||
| 128 | 'method' => $method, |
||
| 129 | 'headers' => array( |
||
| 130 | 'Authorization' => 'Bearer ' . $token, |
||
| 131 | ), |
||
| 132 | ); |
||
| 133 | |||
| 134 | if ( null !== $object ) { |
||
| 135 | $args['headers']['Content-Type'] = 'application/json'; |
||
| 136 | |||
| 137 | $args['body'] = wp_json_encode( $object ); |
||
| 138 | } |
||
| 139 | |||
| 140 | // Request. |
||
| 141 | $response = wp_remote_request( $url, $args ); |
||
| 142 | |||
| 143 | if ( is_wp_error( $response ) ) { |
||
| 144 | $this->error = $response; |
||
| 145 | |||
| 146 | $this->error->add( 'omnikassa_2_error', 'HTTP Request Failed' ); |
||
| 147 | |||
| 148 | return false; |
||
| 149 | } |
||
| 150 | |||
| 151 | // Body. |
||
| 152 | $body = wp_remote_retrieve_body( $response ); |
||
| 153 | |||
| 154 | $data = json_decode( $body ); |
||
| 155 | |||
| 156 | if ( ! is_object( $data ) ) { |
||
| 157 | $this->error = new \WP_Error( 'omnikassa_2_error', 'Could not parse response.', $data ); |
||
| 158 | |||
| 159 | return false; |
||
| 160 | } |
||
| 161 | |||
| 162 | // Error. |
||
| 163 | if ( isset( $data->errorCode ) ) { |
||
| 164 | $message = 'Unknown error.'; |
||
| 165 | |||
| 166 | if ( isset( $data->consumerMessage ) ) { |
||
| 167 | $message = $data->consumerMessage; |
||
| 168 | } elseif ( isset( $data->errorMessage ) ) { |
||
| 169 | $message = $data->errorMessage; |
||
| 170 | } |
||
| 171 | |||
| 172 | $this->error = new \WP_Error( 'omnikassa_2_error', $message, $data ); |
||
| 173 | |||
| 174 | return false; |
||
| 175 | } |
||
| 176 | |||
| 177 | // Ok. |
||
| 178 | return $data; |
||
| 179 | } |
||
| 217 |