| Conditions | 8 |
| Paths | 8 |
| Total Lines | 73 |
| 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 |
||
| 147 | public function sync_items( $post_ids, $clear = true ) { |
||
| 148 | |||
| 149 | $this->log->debug( sprintf( 'Synchronizing post(s) %s...', implode( ', ', $post_ids ) ) ); |
||
| 150 | |||
| 151 | debug_print_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 10 ); |
||
| 152 | |||
| 153 | // If we're starting the sync, try to clear the dataset. |
||
| 154 | if ( $clear && 0 === $this->info()->index ) { |
||
| 155 | $this->api_service->request( 'DELETE', '/middleware/dataset/delete' ); |
||
| 156 | } |
||
| 157 | |||
| 158 | $that = $this; |
||
| 159 | $request_body = array_filter( array_map( function ( $post_id ) use ( $that ) { |
||
| 160 | // Check if the post type is public. |
||
| 161 | $post_type = get_post_type( $post_id ); |
||
| 162 | $post_type_obj = get_post_type_object( $post_type ); |
||
| 163 | if ( ! $post_type_obj->public ) { |
||
| 164 | return false; |
||
| 165 | } |
||
| 166 | |||
| 167 | $is_private = ( 'publish' !== get_post_status( $post_id ) ); |
||
| 168 | $uri = get_post_meta( $post_id, 'entity_url', true ); |
||
| 169 | $object_adapter = $that->sync_object_adapter_factory->create( Object_Type_Enum::POST, $post_id ); |
||
| 170 | $jsonld = $object_adapter->get_jsonld_and_update_hash(); |
||
| 171 | $jsonld_as_string = wp_json_encode( $jsonld ); |
||
| 172 | |||
| 173 | $that->log->trace( "Posting JSON-LD:\n$jsonld_as_string" ); |
||
| 174 | |||
| 175 | return array( |
||
| 176 | 'uri' => $uri, |
||
| 177 | 'model' => $jsonld_as_string, |
||
| 178 | 'private' => $is_private |
||
| 179 | ); |
||
| 180 | }, $post_ids ) ); |
||
| 181 | |||
| 182 | // There's no point in making a request if the request is empty. |
||
| 183 | if ( empty( $request_body ) ) { |
||
| 184 | return true; |
||
| 185 | } |
||
| 186 | |||
| 187 | // Make a request to the remote endpoint. |
||
| 188 | $state = $this->info(); |
||
| 189 | $state_header_value = str_replace( "\n", '', wp_json_encode( $state ) ); |
||
| 190 | $response = $this->api_service->request( |
||
| 191 | 'POST', '/middleware/dataset/batch', |
||
| 192 | array( |
||
| 193 | 'Content-Type' => 'application/json', |
||
| 194 | 'X-Wordlift-Dataset-Sync-State-V1' => $state_header_value |
||
| 195 | ), |
||
| 196 | wp_json_encode( $request_body ) ); |
||
| 197 | |||
| 198 | $this->log->debug( "Response received: " . ( $response->is_success() ? 'yes' : 'no' ) ); |
||
| 199 | |||
| 200 | // Update the sync date in case of success, otherwise log an error. |
||
| 201 | if ( $response->is_success() ) { |
||
| 202 | |||
| 203 | foreach ( $post_ids as $post_id ) { |
||
| 204 | update_post_meta( $post_id, '_wl_synced_gmt', current_time( 'mysql', true ) ); |
||
| 205 | } |
||
| 206 | |||
| 207 | $this->log->debug( sprintf( 'Posts %s synchronized.', implode( ', ', $post_ids ) ) ); |
||
| 208 | |||
| 209 | return true; |
||
| 210 | } else { |
||
| 211 | // @@todo: should we put a limit retry here? |
||
| 212 | $response_dump = var_export( $response, true ); |
||
| 213 | $this->log->error( |
||
|
|
|||
| 214 | sprintf( 'An error occurred while synchronizing the data for post IDs %s: %s', implode( ', ', $post_ids ), $response_dump ) ); |
||
| 215 | |||
| 216 | return false; |
||
| 217 | } |
||
| 218 | |||
| 219 | } |
||
| 220 | |||
| 250 |
This check looks for function calls that miss required arguments.