Completed
Push — master ( 48bc46...861ddb )
by Jonathan
04:46 queued 34s
created

Object_Sync_Sf_Rest   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 97
dl 0
loc 196
rs 9.68
c 3
b 0
f 0
wmc 34

7 Methods

Rating   Name   Duplication   Size   Complexity  
A check_class() 0 5 2
A __construct() 0 16 2
C can_process() 0 38 12
A check_id() 0 5 2
A add_actions() 0 2 1
C process() 0 32 14
A register_routes() 0 23 1
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;
0 ignored issues
show
Bug Best Practice introduced by
The property sfwp_transients does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
54
		$this->namespace = $this->slug;
0 ignored issues
show
Bug Best Practice introduced by
The property namespace does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

120
	public function check_id( /** @scrutinizer ignore-unused */ $id ) {

This check looks for 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
			case 'salesforce':
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
			case 'mappings':
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
			case 'pull':
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
			case 'push':
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 Bug introduced by
The doc comment $result at position 0 could not be parsed: Unknown type name '$result' at position 0 in $result.
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();
185
		$body_params = $request->get_body_params();
186
		$class       = $request->get_url_params()['class'];
187
		$api_call    = str_replace( '/' . $this->namespace . $this->version . '/', '', $route );
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