Completed
Push — add/sync-checkout-and-close-ap... ( a7593e...e71a6e )
by
unknown
09:38
created

Jetpack_JSON_API_Sync_Unlock_Endpoint   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 13.64 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 3
loc 22
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A result() 3 20 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
// POST /sites/%s/sync
4
class Jetpack_JSON_API_Sync_Endpoint extends Jetpack_JSON_API_Endpoint {
5
	protected $needed_capabilities = 'manage_options';
6
7
	protected function validate_call( $_blog_id, $capability, $check_manage_active = true ) {
8
		parent::validate_call( $_blog_id, $capability, false );
9
	}
10
11
	protected function result() {
12
		$args = $this->input();
13
14
		$modules = null;
15
16
		// convert list of modules in comma-delimited format into an array
17
		// of "$modulename => true"
18
		if ( isset( $args['modules'] ) && ! empty( $args['modules'] ) ) {
19
			$modules = array_map( '__return_true', array_flip( array_map( 'trim', explode( ',', $args['modules'] ) ) ) );
20
		}
21
22
		foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) {
23
			if ( 'users' === $module_name && isset( $args[ $module_name ] ) && 'initial' === $args[ $module_name ] ) {
24
				$modules[ 'users' ] = 'initial';
25
			} elseif ( isset( $args[ $module_name ] ) ) {
26
				$ids = explode( ',', $args[ $module_name ] );
27
				if ( count( $ids ) > 0 ) {
28
					$modules[ $module_name ] = $ids;
29
				}
30
			}
31
		}
32
33
		if ( empty( $modules ) ) {
34
			$modules = null;
35
		}
36
37
		return array( 'scheduled' => Jetpack_Sync_Actions::schedule_full_sync( $modules ) );
38
	}
39
40
	protected function validate_queue( $query ) {
41
		if ( ! isset( $query ) ) {
42
			return new WP_Error( 'invalid_queue', 'Queue name is required', 400 );
43
		}
44
45 View Code Duplication
		if ( ! in_array( $query, array( 'sync', 'full_sync' ) ) ) {
46
			return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 );
47
		}
48
		return $query;
49
	}
50
}
51
52
// GET /sites/%s/sync/status
53
class Jetpack_JSON_API_Sync_Status_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
54
	protected function result() {
55
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-modules.php';
56
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
57
58
		$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' );
59
		$sender      = Jetpack_Sync_Sender::get_instance();
60
		$queue       = $sender->get_sync_queue();
61
		$full_queue  = $sender->get_full_sync_queue();
62
63
		return array_merge(
64
			$sync_module->get_status(),
65
			array(
66
				'is_scheduled'          => Jetpack_Sync_Actions::is_scheduled_full_sync(),
67
				'queue_size'            => $queue->size(),
68
				'queue_lag'             => $queue->lag(),
69
				'queue_next_sync'       => ( $sender->get_next_sync_time( 'sync' ) - microtime( true ) ),
70
				'full_queue_size'       => $full_queue->size(),
71
				'full_queue_lag'        => $full_queue->lag(),
72
				'full_queue_next_sync'  => ( $sender->get_next_sync_time( 'full_sync' ) - microtime( true ) ),
73
			)
74
		);
75
	}
76
}
77
78
// GET /sites/%s/data-check
79
class Jetpack_JSON_API_Sync_Check_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
80
	protected function result() {
81
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
82
83
		$sender     = Jetpack_Sync_Sender::get_instance();
84
		$sync_queue = $sender->get_sync_queue();
85
86
		// lock sending from the queue while we compare checksums with the server
87
		$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds
88
89
		if ( ! $result ) {
90
			$sync_queue->unlock();
91
92
			return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' );
93
		}
94
95
		if ( is_wp_error( $result ) ) {
96
			$sync_queue->unlock();
97
98
			return $result;
99
		}
100
101
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-wp-replicastore.php';
102
103
		$store = new Jetpack_Sync_WP_Replicastore();
104
105
		$result = $store->checksum_all();
106
107
		$sync_queue->unlock();
108
109
		return $result;
110
111
	}
112
}
113
114
// GET /sites/%s/data-histogram
115
class Jetpack_JSON_API_Sync_Histogram_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
116
	protected function result() {
117
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
118
119
		$sender     = Jetpack_Sync_Sender::get_instance();
120
		$sync_queue = $sender->get_sync_queue();
121
122
		// lock sending from the queue while we compare checksums with the server
123
		$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds
124
125
		if ( ! $result ) {
126
			$sync_queue->unlock();
127
128
			return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' );
129
		}
130
131
		if ( is_wp_error( $result ) ) {
132
			$sync_queue->unlock();
133
134
			return $result;
135
		}
136
137
		$args = $this->query_args();
138
139
		if ( isset( $args['columns'] ) ) {
140
			$columns = array_map( 'trim', explode( ',', $args['columns'] ) );
141
		} else {
142
			$columns = null; // go with defaults
143
		}
144
145
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-wp-replicastore.php';
146
147
		$store = new Jetpack_Sync_WP_Replicastore();
148
149
		$result = $store->checksum_histogram( $args['object_type'], $args['buckets'], $args['start_id'], $args['end_id'], $columns );
150
151
		$sync_queue->unlock();
152
153
		return $result;
154
155
	}
156
}
157
158
// POST /sites/%s/sync/settings
159
class Jetpack_JSON_API_Sync_Modify_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
160
	protected function result() {
161
		$args = $this->input();
162
163
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-settings.php';
164
165
		$sync_settings = Jetpack_Sync_Settings::get_settings();
166
167
		foreach ( $args as $key => $value ) {
168
			if ( $value !== false ) {
169
				if ( is_numeric( $value ) ) {
170
					$value = (int) $value;
171
				}
172
				
173
				// special case for sending empty arrays - a string with value 'empty'
174
				if ( $value === 'empty' ) {
175
					$value = array();
176
				}
177
178
				$sync_settings[ $key ] = $value;
179
			}
180
		}
181
182
		Jetpack_Sync_Settings::update_settings( $sync_settings );
183
184
		// re-fetch so we see what's really being stored
185
		return Jetpack_Sync_Settings::get_settings();
186
	}
187
}
188
189
// GET /sites/%s/sync/settings
190
class Jetpack_JSON_API_Sync_Get_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
191
	protected function result() {
192
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-settings.php';
193
194
		return Jetpack_Sync_Settings::get_settings();
195
	}
196
}
197
198
// GET /sites/%s/sync/object
199
class Jetpack_JSON_API_Sync_Object extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
200
	protected function result() {
201
		$args = $this->query_args();
202
203
		$module_name = $args['module_name'];
204
205
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-modules.php';
206
207
		if ( ! $sync_module = Jetpack_Sync_Modules::get_module( $module_name ) ) {
208
			return new WP_Error( 'invalid_module', 'You specified an invalid sync module' );
209
		}
210
211
		$object_type = $args['object_type'];
212
		$object_ids  = $args['object_ids'];
213
214
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
215
		$codec = Jetpack_Sync_Sender::get_instance()->get_codec();
216
217
		return array(
218
			'objects' => $codec->encode( $sync_module->get_objects_by_id( $object_type, $object_ids ) )
219
		);
220
	}
221
}
222
223
class Jetpack_JSON_API_Sync_Now_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
224
	protected function result() {
225
		$args = $this->input();
226
		$queue_name = $this->validate_queue( $args['queue'] );
227
228
		if ( is_wp_error( $queue_name ) ){
229
			return $queue_name;
230
		}
231
232
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
233
234
		$sender = Jetpack_Sync_Sender::get_instance();
235
		$response = $sender->do_sync_for_queue( new Jetpack_Sync_Queue( $args['queue'] ) );
236
237
		return array(
238
			'response' => $response
239
		);
240
	}
241
}
242
243
class Jetpack_JSON_API_Sync_Checkout_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
244
	protected function result() {
245
		$args = $this->input();
246
		$queue_name = $this->validate_queue( $args['queue'] );
247
248
		if ( is_wp_error( $queue_name ) ){
249
			return $queue_name;
250
		}
251
252
		if ( $args[ 'number_of_items' ] < 1 || $args[ 'number_of_items' ] > 100  ) {
253
			return new WP_Error( 'invalid_number_of_items', 'Number of items needs to be an integer that is larger than 0 and less then 100', 400 );
254
		}
255
256
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php';
257
		$queue = new Jetpack_Sync_Queue( $queue_name );
258
259
		if ( 0 === $queue->size() ) {
260
			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 );
261
		}
262
263
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
264
		$sender = Jetpack_Sync_Sender::get_instance();
265
		
266
		// let's delete the checkin state
267
		if ( $args['force'] ) {
268
			$queue->unlock();
269
		}
270
271
		$buffer = $this->get_buffer( $queue, $args[ 'number_of_items' ] );
272
		
273
		// Check that the $buffer is not checkout out already
274
		if ( is_wp_error( $buffer ) ) {
275
			return new WP_Error( 'buffer_open', "We couldn't get the buffer it is currently checked out", 400 );
276
		}
277
		
278
		if ( ! is_object( $buffer ) ) {
279
			return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 );
280
		}
281
282
		list( $items_to_send, $skipped_items_ids, $items ) = $sender->get_items_to_send( $buffer, $args['encode'] );
0 ignored issues
show
Unused Code introduced by
The assignment to $items is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
283
284
		return array(
285
			'buffer_id'      => $buffer->id,
286
			'items'          => $items_to_send,
287
			'skipped_items'  => $skipped_items_ids,
288
			'codec'          => $args['encode'] ? $sender->get_codec()->name() : null,
289
			'sent_timestamp' => time(),
290
		);
291
	}
292
293
	protected function get_buffer( $queue, $number_of_items ) {
294
		$start = time();
295
		$max_duration = 5; // this will try to get the buffer
296
297
		$buffer = $queue->checkout( $number_of_items );
298
		$duration = time() - $start;
299
300
		while( is_wp_error( $buffer ) && $duration < $max_duration ) {
301
			sleep( 2 );
302
			$duration = time() - $start;
303
			$buffer = $queue->checkout( $number_of_items );
304
		}
305
306
		if ( $buffer === false ) {
307
			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 );
308
		}
309
310
		return $buffer;
311
	}
312
}
313
314
class Jetpack_JSON_API_Sync_Close_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
315
	protected function result() {
316
		$request_body = $this->input();
317
		$queue_name = $this->validate_queue( $request_body['queue'] );
318
319
		if ( is_wp_error( $queue_name ) ) {
320
			return $queue_name;
321
		}
322
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php';
323
324
		if ( ! isset( $request_body['buffer_id'] ) ) {
325
			return new WP_Error( 'missing_buffer_id', 'Please provide a buffer id', 400 );
326
		}
327
328
		if ( ! isset( $request_body['item_ids'] ) || ! is_array( $request_body['item_ids'] ) ) {
329
			return new WP_Error( 'missing_item_ids', 'Please provide a list of item ids in the item_ids argument', 400 );
330
		}
331
332
		//Limit to A-Z,a-z,0-9,_,-
333
		$request_body ['buffer_id'] = preg_replace( '/[^A-Za-z0-9]/', '', $request_body['buffer_id'] );
334
		$request_body['item_ids'] = array_filter( array_map( array( 'Jetpack_JSON_API_Sync_Close_Endpoint', 'sanitize_item_ids' ), $request_body['item_ids'] ) );
335
336
		$buffer = new Jetpack_Sync_Queue_Buffer( $request_body['buffer_id'], $request_body['item_ids'] );
337
		$queue = new Jetpack_Sync_Queue( $queue_name );
338
339
		$response = $queue->close( $buffer, $request_body['item_ids'] );
340
341
		if ( is_wp_error( $response ) ) {
342
			return $response;
343
		}
344
345
		return array(
346
			'success' => $response
347
		);
348
	}
349
350
	protected static function sanitize_item_ids( $item ) {
351
		// lets not delete any options that don't start with jpsq_sync-
352
		if ( substr( $item, 0, 5 ) !== 'jpsq_' ) {
353
			return null;
354
		}
355
		//Limit to A-Z,a-z,0-9,_,-,.
356
		return preg_replace( '/[^A-Za-z0-9-_.]/', '', $item );
357
	}
358
}
359
360
class Jetpack_JSON_API_Sync_Unlock_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
361
	protected function result() {
362
		$args = $this->input();
363
364
		if ( ! isset( $args['queue'] ) ) {
365
			return new WP_Error( 'invalid_queue', 'Queue name is required', 400 );
366
		}
367
368 View Code Duplication
		if ( ! in_array( $args['queue'], array( 'sync', 'full_sync' ) ) ) {
369
			return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 );
370
		}
371
372
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php';
373
		$queue = new Jetpack_Sync_Queue( $args['queue'] );
374
375
		// If false it means that there was no lock to delete.
376
		$response = $queue->unlock();
377
		return array(
378
			'success' => $response
379
		);
380
	}
381
}