Test Failed
Pull Request — master (#238)
by Jonathan
04:22
created

Object_Sync_Sf_Rest   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 199
Duplicated Lines 13.07 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 34
lcom 1
cbo 0
dl 26
loc 199
rs 9.68
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 2
A add_actions() 0 3 1
A register_routes() 0 27 1
A check_class() 0 6 2
A check_id() 0 6 2
C can_process() 26 39 12
C process() 0 33 14

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 file for the Object_Sync_Sf_Rest class.
4
 *
5
 * @file
6
 */
7
8
if ( ! class_exists( 'Object_Sync_Salesforce' ) ) {
9
	die();
10
}
11
12
/**
13
 * Create WordPress REST API functionality
14
 */
15
class Object_Sync_Sf_Rest {
16
17
	protected $wpdb;
18
	protected $version;
19
	protected $slug;
20
	protected $option_prefix;
21
	protected $wordpress;
22
	protected $salesforce;
23
	protected $mappings;
24
	protected $push;
25
	protected $pull;
26
27
	/**
28
	* Constructor which sets up rest methods
29
	*
30
	* @param object $wpdb
31
	* @param string $version
32
	* @param string $slug
33
	* @param string $option_prefix
34
	* @param object $wordpress
35
	* @param object $salesforce
36
	* @param object $mappings
37
	* @param object $push
38
	* @param object $pull
39
	* @throws \Exception
40
	*/
41
	public function __construct( $wpdb, $version, $slug, $option_prefix, $wordpress, $salesforce, $mappings, $push, $pull ) {
42
		$this->wpdb          = $wpdb;
43
		$this->version       = $version;
44
		$this->slug          = $slug;
45
		$this->option_prefix = isset( $option_prefix ) ? $option_prefix : 'object_sync_for_salesforce_';
46
		$this->wordpress     = $wordpress;
47
		$this->salesforce    = $salesforce;
48
		$this->mappings      = $mappings;
49
		$this->push          = $push;
50
		$this->pull          = $pull;
51
52
		$this->sfwp_transients = $this->wordpress->sfwp_transients;
53
54
		$this->namespace = $this->slug;
55
56
		$this->add_actions();
57
58
	}
59
60
	/**
61
	* Create the action hooks to create the reset methods
62
	*
63
	*/
64
	public function add_actions() {
65
		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
66
	}
67
68
	/**
69
	* Register REST API routes
70
	*
71
	* @throws \Exception
72
	*/
73
	public function register_routes() {
74
		$namespace   = $this->namespace;
75
		$method_list = WP_REST_Server::ALLMETHODS;
76
		register_rest_route( $namespace, '/(?P<class>([\w-])+)/', array(
77
			array(
78
				'methods'             => $method_list,
79
				'args'                => array(
80
					'class'                  => array(
81
						'validate_callback' => array( $this, 'check_class' ),
82
						'required'          => true,
83
					),
84
					'salesforce_object_type' => array(
85
						'type' => 'string',
86
					),
87
					'salesforce_id'          => array(
88
						'type' => 'string',
89
					),
90
					'wordpress_object_type'  => array(
91
						'type' => 'string',
92
					),
93
				),
94
				'permission_callback' => array( $this, 'can_process' ),
95
				'callback'            => array( $this, 'process' ),
96
			),
97
		) );
98
99
	}
100
101
	/**
102
	* Check for a valid class from the parameter
103
	*
104
	* @param string $class
105
	* @return bool
106
	*/
107
	public function check_class( $class ) {
108
		if ( is_object( $this->{ $class } ) ) {
109
			return true;
110
		}
111
		return false;
112
	}
113
114
	/**
115
	* Check for a valid ID from the parameter
116
	*
117
	* @param string $id
118
	* @return bool
119
	*/
120
	public function check_id( $id ) {
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
		if ( is_object( $class ) ) {
122
			return true;
123
		}
124
		return false;
125
	}
126
127
	/**
128
	* Check to see if the user has permission to do this
129
	*
130
	* @param WP_REST_Request $request
131
	* @throws \Exception
132
	*/
133
	public function can_process( WP_REST_Request $request ) {
134
		// unless we specify it here, the method will not be allowed unless the user has configure_salesforce capability
135
		$http_method = $request->get_method();
136
		$class       = $request->get_url_params()['class'];
137
		switch ( $class ) {
138 View Code Duplication
			case 'salesforce':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
				if ( ! in_array( $http_method, explode( ',', WP_REST_Server::ALLMETHODS ) ) ) {
140
					return new WP_Error( 'rest_forbidden', esc_html__( 'This kind of request is not allowed.', 'object-sync-for-salesforce' ), array( 'status' => 401 ) );
141
				}
142
				if ( ! current_user_can( 'configure_salesforce' ) ) {
143
					return new WP_Error( 'rest_forbidden', esc_html__( 'You do not have permissions to view this data.', 'object-sync-for-salesforce' ), array( 'status' => 401 ) );
144
				}
145
				break;
146 View Code Duplication
			case 'mappings':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
				if ( ! in_array( $http_method, explode( ',', WP_REST_Server::ALLMETHODS ) ) ) {
148
					return new WP_Error( 'rest_forbidden', esc_html__( 'This kind of request is not allowed.', 'object-sync-for-salesforce' ), array( 'status' => 401 ) );
149
				}
150
				if ( ! current_user_can( 'configure_salesforce' ) ) {
151
					return new WP_Error( 'rest_forbidden', esc_html__( 'You do not have permissions to view this data.', 'object-sync-for-salesforce' ), array( 'status' => 401 ) );
152
				}
153
				break;
154 View Code Duplication
			case 'pull':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
				if ( ! in_array( $http_method, array( 'GET', 'POST', 'PUT' ) ) ) {
156
					return new WP_Error( 'rest_forbidden', esc_html__( 'This kind of request is not allowed.', 'object-sync-for-salesforce' ), array( 'status' => 401 ) );
157
				}
158
				break;
159 View Code Duplication
			case 'push':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
				if ( ! in_array( $http_method, array( 'POST', 'PUT' ) ) ) {
161
					return new WP_Error( 'rest_forbidden', esc_html__( 'This kind of request is not allowed.', 'object-sync-for-salesforce' ), array( 'status' => 401 ) );
162
				}
163
				break;
164
			default:
165
				if ( ! current_user_can( 'configure_salesforce' ) ) {
166
					return new WP_Error( 'rest_forbidden', esc_html__( 'You do not have permissions to view this data.', 'object-sync-for-salesforce' ), array( 'status' => 401 ) );
167
				}
168
				break;
169
		}
170
		return true;
171
	}
172
173
	/**
174
	* Process the REST API request
175
	*
176
	* @param WP_REST_Request $request
177
	* @return $result
0 ignored issues
show
Documentation introduced by
The doc-type $result could not be parsed: Unknown type name "$result" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
178
	*/
179
	public function process( WP_REST_Request $request ) {
180
		// see methods: https://developer.wordpress.org/reference/classes/wp_rest_request/
181
		//error_log( 'request is ' . print_r( $request, true ) );
182
		$http_method = $request->get_method();
183
		$route       = $request->get_route();
184
		$url_params  = $request->get_url_params();
0 ignored issues
show
Unused Code introduced by
$url_params is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
185
		$body_params = $request->get_body_params();
186
		$class       = $request->get_url_params()['class'];
187
		$api_call    = str_replace( '/' . $this->namespace . $this->version . '/', '', $route );
0 ignored issues
show
Unused Code introduced by
$api_call is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
188
		//error_log( 'api call is ' . $api_call . ' and params are ' . print_r( $params, true ) );
189
		$result = '';
190
		switch ( $class ) {
191
			case 'salesforce':
192
				break;
193
			case 'mappings':
194
				break;
195
			case 'pull':
196
				if ( 'GET' === $http_method ) {
197
					$result = $this->pull->salesforce_pull_webhook( $request );
198
				}
199
				if ( 'POST' === $http_method && isset( $body_params['salesforce_object_type'] ) && isset( $body_params['salesforce_id'] ) ) {
200
					$result = $this->pull->manual_pull( $body_params['salesforce_object_type'], $body_params['salesforce_id'] );
201
				}
202
				break;
203
			case 'push':
204
				if ( ( 'POST' === $http_method || 'PUT' === $http_method || 'DELETE' === $http_method ) && isset( $body_params['wordpress_object_type'] ) && isset( $body_params['wordpress_id'] ) ) {
205
					$result = $this->push->manual_push( $body_params['wordpress_object_type'], $body_params['wordpress_id'], $http_method );
206
				}
207
				break;
208
		}
209
210
		return $result;
211
	}
212
213
}
214