Completed
Push — fix/reset-previous-full-sync-w... ( d1037b )
by
unknown
209:23 queued 198:18
created

Jetpack_JSON_API_Sync_Endpoint   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 3
loc 21
rs 10
wmc 4
lcom 0
cbo 2

1 Method

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