Completed
Push — add/selective-sync-api ( 400103...9ab223 )
by
unknown
09:25
created

Jetpack_JSON_API_Sync_Endpoint   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 8.11 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
C result() 3 32 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
class Jetpack_JSON_API_Sync_Endpoint extends Jetpack_JSON_API_Endpoint {
4
	// POST /sites/%s/sync
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::getInstance();
17
			$sync_queue = $sender->get_sync_queue();
18
			$sync_queue->reset();
19
		}
20
21
		if ( isset( $args['force'] ) && $args['force'] ) {
22
			// reset full sync lock
23
			require_once dirname(__FILE__).'/../../sync/class.jetpack-sync-modules.php';
24
25
			$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' );
26
			$sync_module->clear_status();
27
		}
28
29 View Code Duplication
		if ( isset( $args['modules'] ) && !empty( $args['modules'] ) ) {
30
			$modules = array_map('trim', explode( ',', $args['modules'] ) );
31
		}
32
33
		/** This action is documented in class.jetpack-sync-sender.php */
34
		Jetpack_Sync_Actions::schedule_full_sync( $modules );
35
		spawn_cron();
36
37
		return array( 'scheduled' => true );
38
	}
39
}
40
41
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...
42
	// GET /sites/%s/sync/status
43
	protected $needed_capabilities = 'manage_options';
44
45
	protected function result() {
46
		require_once dirname(__FILE__).'/../../sync/class.jetpack-sync-modules.php';
47
48
		$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' );
49
		return array_merge(
50
			$sync_module->get_status(),
51
			array( 'is_scheduled' => (bool) wp_next_scheduled( 'jetpack_sync_full' ) )
52
		);
53
	}
54
}
55
56
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...
57
	// GET /sites/%s/cached-data-check
58
	protected $needed_capabilities = 'manage_options';
59
60
	protected function result() {
61
		require_once dirname(__FILE__).'/../../sync/class.jetpack-sync-sender.php';
62
63
		$sender = Jetpack_Sync_Sender::getInstance();
64
		$sync_queue = $sender->get_sync_queue();
65
66
		// lock sending from the queue while we compare checksums with the server
67
		$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds
68
69
		if ( !$result ) {
70
			$sync_queue->unlock();
71
			return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' );
72
		}
73
74
		if ( is_wp_error( $result ) ) {
75
			$sync_queue->unlock();
76
			return $result;
77
		}
78
79
		require_once dirname(__FILE__).'/../../sync/class.jetpack-sync-wp-replicastore.php';
80
81
		$store = new Jetpack_Sync_WP_Replicastore();
82
83
		$result = $store->checksum_all();
84
85
		$sync_queue->unlock();
86
87
		return $result;
88
89
	}
90
}
91
92
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...
93
	// POST /sites/%s/sync/settings
94
	protected $needed_capabilities = 'manage_options';
95
96
	protected function result() {
97
		$args = $this->input();
98
99
		require_once dirname(__FILE__).'/../../sync/class.jetpack-sync-settings.php';
100
101
		$sync_settings = Jetpack_Sync_Settings::get_settings();
102
103
		foreach( $args as $key => $value ) {
104
			if ( $value !== false ) {
105
				if ( is_numeric( $value ) ) {
106
					$value = (int) $value;
107
				}
108
				$sync_settings[ $key ] = $value;
109
			}
110
		}
111
112
		Jetpack_Sync_Settings::update_settings( $sync_settings );
113
114
		// re-fetch so we see what's really being stored
115
		return Jetpack_Sync_Settings::get_settings();
116
	}
117
}
118
119
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...
120
	// GET /sites/%s/sync/settings
121
	protected $needed_capabilities = 'manage_options';
122
123
	protected function result() {
124
		require_once dirname(__FILE__).'/../../sync/class.jetpack-sync-settings.php';
125
		return Jetpack_Sync_Settings::get_settings();
126
	}
127
}
128