Completed
Push — update/idc-endpoint-migration ( eee79e )
by
unknown
44:41 queued 34:21
created

REST_Endpoints   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 35.16 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 45
loc 128
rs 10
c 0
b 0
f 0
wmc 11
lcom 0
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize_rest_api() 0 36 1
A confirm_safe_mode() 16 16 2
A migrate_stats_and_subscribers() 22 22 5
A start_fresh_connection() 0 5 1
A identity_crisis_mitigation_permission_check() 7 7 2

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
 * Identity_Crisis package.
4
 *
5
 * @package  automattic/jetpack-identity-crisis
6
 */
7
8
namespace Automattic\Jetpack\IdentityCrisis;
9
10
use Jetpack;
11
use Jetpack_Options;
12
use WP_Error;
13
14
/**
15
 * This class will handle Identity Crisis Endpoints
16
 *
17
 * @since 9.8.0
18
 */
19
class REST_Endpoints {
20
21
	/**
22
	 * Initialize REST routes.
23
	 */
24
	public function initialize_rest_api() {
25
26
		// Confirm that a site in identity crisis should be in staging mode.
27
		register_rest_route(
28
			'jetpack/v4',
29
			'/identity-crisis/confirm-safe-mode',
30
			array(
31
				'methods'             => WP_REST_Server::EDITABLE,
32
				'callback'            => __CLASS__ . '::confirm_safe_mode',
33
				'permission_callback' => __CLASS__ . '::identity_crisis_mitigation_permission_check',
34
			)
35
		);
36
37
		// IDC resolve: create an entirely new shadow site for this URL.
38
		register_rest_route(
39
			'jetpack/v4',
40
			'/identity-crisis/start-fresh',
41
			array(
42
				'methods'             => WP_REST_Server::EDITABLE,
43
				'callback'            => __CLASS__ . '::start_fresh_connection',
44
				'permission_callback' => __CLASS__ . '::identity_crisis_mitigation_permission_check',
45
			)
46
		);
47
48
		// Handles the request to migrate stats and subscribers during an identity crisis.
49
		register_rest_route(
50
			'jetpack/v4',
51
			'identity-crisis/migrate',
52
			array(
53
				'methods'             => WP_REST_Server::EDITABLE,
54
				'callback'            => __CLASS__ . '::migrate_stats_and_subscribers',
55
				'permission_callback' => __CLASS__ . '::identity_crisis_mitigation_permission_check',
56
			)
57
		);
58
59
	}
60
61
	/**
62
	 * Handles identity crisis mitigation, confirming safe mode for this site.
63
	 *
64
	 * @since 4.4.0
65
	 *
66
	 * @return bool | WP_Error True if option is properly set.
67
	 */
68 View Code Duplication
	public static function confirm_safe_mode() {
69
		$updated = Jetpack_Options::update_option( 'safe_mode_confirmed', true );
70
		if ( $updated ) {
71
			return rest_ensure_response(
72
				array(
73
					'code' => 'success',
74
				)
75
			);
76
		}
77
78
		return new WP_Error(
79
			'error_setting_jetpack_safe_mode',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'error_setting_jetpack_safe_mode'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
80
			esc_html__( 'Could not confirm safe mode.', 'jetpack' ),
81
			array( 'status' => 500 )
82
		);
83
	}
84
85
	/**
86
	 * Handles identity crisis mitigation, migrating stats and subscribers from old url to this, new url.
87
	 *
88
	 * @since 4.4.0
89
	 *
90
	 * @return bool | WP_Error True if option is properly set.
91
	 */
92 View Code Duplication
	public static function migrate_stats_and_subscribers() {
93
		if ( Jetpack_Options::get_option( 'sync_error_idc' ) && ! Jetpack_Options::delete_option( 'sync_error_idc' ) ) {
94
			return new WP_Error(
95
				'error_deleting_sync_error_idc',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'error_deleting_sync_error_idc'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
96
				esc_html__( 'Could not delete sync error option.', 'jetpack' ),
97
				array( 'status' => 500 )
98
			);
99
		}
100
101
		if ( Jetpack_Options::get_option( 'migrate_for_idc' ) || Jetpack_Options::update_option( 'migrate_for_idc', true ) ) {
102
			return rest_ensure_response(
103
				array(
104
					'code' => 'success',
105
				)
106
			);
107
		}
108
		return new WP_Error(
109
			'error_setting_jetpack_migrate',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'error_setting_jetpack_migrate'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
110
			esc_html__( 'Could not confirm migration.', 'jetpack' ),
111
			array( 'status' => 500 )
112
		);
113
	}
114
115
	/**
116
	 * This IDC resolution will disconnect the site and re-connect to a completely new
117
	 * and separate shadow site than the original.
118
	 *
119
	 * It will first will disconnect the site without phoning home as to not disturb the production site.
120
	 * It then builds a fresh connection URL and sends it back along with the response.
121
	 *
122
	 * @since 4.4.0
123
	 * @return bool|WP_Error
124
	 */
125
	public static function start_fresh_connection() {
126
		// First clear the options / disconnect.
127
		Jetpack::disconnect();
128
		return self::build_connect_url();
0 ignored issues
show
Bug introduced by
The method build_connect_url() does not seem to exist on object<Automattic\Jetpac...yCrisis\REST_Endpoints>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
	}
130
131
	/**
132
	 * Verify that user can mitigate an identity crisis.
133
	 *
134
	 * @since 4.4.0
135
	 *
136
	 * @return bool Whether user has capability 'jetpack_disconnect'.
137
	 */
138 View Code Duplication
	public static function identity_crisis_mitigation_permission_check() {
139
		if ( current_user_can( 'jetpack_disconnect' ) ) {
140
			return true;
141
		}
142
143
		return new WP_Error( 'invalid_user_permission_identity_crisis', self::$user_permissions_error_msg, array( 'status' => rest_authorization_required_code() ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_user_permission_identity_crisis'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
144
	}
145
146
}
147