Completed
Push — add/google-maps/api-key ( 5599f7...90239a )
by
unknown
26:57 queued 16:47
created

Jetpack_JSON_API_Sync_Histogram_Endpoint   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 11.9 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 5
loc 42
rs 10
wmc 4
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B result() 5 38 4

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