Completed
Push — update/sync-endpoints-availabl... ( 58436d )
by
unknown
12:18
created

Jetpack_JSON_API_Sync_Endpoint   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 36
Duplicated Lines 8.33 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 3
loc 36
rs 10
wmc 8
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate_call() 0 3 1
C result() 3 28 7

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 View Code Duplication
		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 ( isset( $args[ $module_name ] ) ) {
24
				$ids = explode( ',', $args[ $module_name ] );
25
				if ( count( $ids ) > 0 ) {
26
					$modules[ $module_name ] = $ids;
27
				}
28
			}
29
		}
30
31
		if ( empty( $modules ) ) {
32
			$modules = null;
33
		}
34
35
		Jetpack_Sync_Actions::schedule_full_sync( $modules );
36
37
		return array( 'scheduled' => true );
38
	}
39
}
40
41
// GET /sites/%s/sync/status
42
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...
43
	protected $needed_capabilities = 'manage_options';
44
45
	protected function result() {
46
		require_once dirname( __FILE__ ) . '/../../sync/class.jetpack-sync-modules.php';
47
		require_once dirname( __FILE__ ) . '/../../sync/class.jetpack-sync-sender.php';
48
49
		$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' );
50
		$sender      = Jetpack_Sync_Sender::get_instance();
51
		$queue       = $sender->get_sync_queue();
52
		$full_queue  = $sender->get_full_sync_queue();
53
54
		return array_merge(
55
			$sync_module->get_status(),
56
			array(
57
				'is_scheduled'    => Jetpack_Sync_Actions::is_scheduled_full_sync(),
58
				'queue_size'      => $queue->size(),
59
				'queue_lag'       => $queue->lag(),
60
				'full_queue_size' => $full_queue->size(),
61
				'full_queue_lag'  => $full_queue->lag()
62
			)
63
		);
64
	}
65
}
66
67
// GET /sites/%s/data-check
68
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...
69
	protected $needed_capabilities = 'manage_options';
70
71
	protected function result() {
72
		require_once dirname( __FILE__ ) . '/../../sync/class.jetpack-sync-sender.php';
73
74
		$sender     = Jetpack_Sync_Sender::get_instance();
75
		$sync_queue = $sender->get_sync_queue();
76
77
		// lock sending from the queue while we compare checksums with the server
78
		$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds
79
80
		if ( ! $result ) {
81
			$sync_queue->unlock();
82
83
			return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' );
84
		}
85
86
		if ( is_wp_error( $result ) ) {
87
			$sync_queue->unlock();
88
89
			return $result;
90
		}
91
92
		require_once dirname( __FILE__ ) . '/../../sync/class.jetpack-sync-wp-replicastore.php';
93
94
		$store = new Jetpack_Sync_WP_Replicastore();
95
96
		$result = $store->checksum_all();
97
98
		$sync_queue->unlock();
99
100
		return $result;
101
102
	}
103
}
104
105
// GET /sites/%s/data-histogram
106
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...
107
	protected $needed_capabilities = 'manage_options';
108
109
	protected function result() {
110
		require_once dirname( __FILE__ ) . '/../../sync/class.jetpack-sync-sender.php';
111
112
		$sender     = Jetpack_Sync_Sender::get_instance();
113
		$sync_queue = $sender->get_sync_queue();
114
115
		// lock sending from the queue while we compare checksums with the server
116
		$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds
117
118
		if ( ! $result ) {
119
			$sync_queue->unlock();
120
121
			return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' );
122
		}
123
124
		if ( is_wp_error( $result ) ) {
125
			$sync_queue->unlock();
126
127
			return $result;
128
		}
129
130
		$args = $this->query_args();
131
132
		if ( isset( $args['columns'] ) ) {
133
			$columns = array_map( 'trim', explode( ',', $args['columns'] ) );
134
		} else {
135
			$columns = null; // go with defaults
136
		}
137
138
		require_once dirname( __FILE__ ) . '/../../sync/class.jetpack-sync-wp-replicastore.php';
139
140
		$store = new Jetpack_Sync_WP_Replicastore();
141
142
		$result = $store->checksum_histogram( $args['object_type'], $args['buckets'], $args['start_id'], $args['end_id'], $columns );
143
144
		$sync_queue->unlock();
145
146
		return $result;
147
148
	}
149
}
150
151
// POST /sites/%s/sync/settings
152
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...
153
	protected $needed_capabilities = 'manage_options';
154
155
	protected function result() {
156
		$args = $this->input();
157
158
		require_once dirname( __FILE__ ) . '/../../sync/class.jetpack-sync-settings.php';
159
160
		$sync_settings = Jetpack_Sync_Settings::get_settings();
161
162
		foreach ( $args as $key => $value ) {
163
			if ( $value !== false ) {
164
				if ( is_numeric( $value ) ) {
165
					$value = (int) $value;
166
				}
167
				$sync_settings[ $key ] = $value;
168
			}
169
		}
170
171
		Jetpack_Sync_Settings::update_settings( $sync_settings );
172
173
		// re-fetch so we see what's really being stored
174
		return Jetpack_Sync_Settings::get_settings();
175
	}
176
}
177
178
// GET /sites/%s/sync/settings
179
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...
180
	protected $needed_capabilities = 'manage_options';
181
182
	protected function result() {
183
		require_once dirname( __FILE__ ) . '/../../sync/class.jetpack-sync-settings.php';
184
185
		return Jetpack_Sync_Settings::get_settings();
186
	}
187
}
188