Passed
Pull Request — master (#420)
by Jonathan
04:04
created

Object_Sync_Sf_Pull_Options::set()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 3
b 0
f 0
nc 6
nop 5
dl 0
loc 28
rs 9.5555
1
<?php
2
/**
3
 * Handles getting and setting the pull options.
4
 *
5
 * @class   Object_Sync_Sf_Pull_Options
6
 * @package Object_Sync_Salesforce
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Object_Sync_Sf_Pull_Options class.
13
 */
14
class Object_Sync_Sf_Pull_Options {
15
16
	/**
17
	 * Current version of the plugin
18
	 *
19
	 * @var string
20
	 */
21
	public $version;
22
23
	/**
24
	 * The plugin's prefix when saving options to the database
25
	 *
26
	 * @var string
27
	 */
28
	public $option_prefix;
29
30
	/**
31
	 * Direction of the operation
32
	 *
33
	 * @var string
34
	 */
35
	public $direction;
36
37
	/**
38
	 * Option keys that can be upgraded
39
	 *
40
	 * @var string
41
	 */
42
	private $upgradeable_keys;
43
44
	/**
45
	 * Constructor for option records class
46
	 */
47
	public function __construct() {
48
		$this->version       = object_sync_for_salesforce()->version;
49
		$this->option_prefix = object_sync_for_salesforce()->option_prefix;
50
51
		$this->direction = 'pull';
52
53
		$this->upgradeable_keys = $this->get_upgradeable_keys();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->get_upgradeable_keys() of type array is incompatible with the declared type string of property $upgradeable_keys.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
55
	}
56
57
	/**
58
	 * Generate an option key
59
	 *
60
	 * @param array $params the pieces to put together.
61
	 * @param bool  $legacy whether this is a legacy key.
62
	 * @return string $key the full option key.
63
	 */
64
	private function generate_option_key( $params, $legacy = false ) {
65
		array_unshift( $params, substr( $this->option_prefix, 0, -1 ), $this->direction ); // add the prefixes.
66
		$params = array_filter( $params, fn( $value ) => ! is_null( $value ) && '' !== $value ); // remove null and empty values.
67
68
		// legacy keys don't have a fieldmap.
69
		if ( true === $legacy && isset( $params['fieldmap_id'] ) ) {
70
			unset( $params['fieldmap_id'] );
71
		}
72
73
		// make the key a string.
74
		$key = implode( '_', $params );
75
76
		// allow developers to filter the key.
77
		$key = apply_filters( $this->option_prefix . 'pull_option_key', $key, $params );
78
79
		/* // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
80
		add_filter( 'object_sync_for_salesforce_pull_option_key', 'change_pull_option_key', 10, 2 );
81
		function change_pull_query( $key, $params ) {
82
			$key = 'my_key_name';
83
			return $key;
84
		}
85
		*/
86
87
		if ( true === $legacy ) {
88
			// allow developers to filter the legacy key.
89
			$key = apply_filters( $this->option_prefix . 'pull_option_legacy_key', $key, $params );
90
		}
91
92
		// note: the WordPress codex indicates that option names do not need to be escaped.
93
		// see: https://developer.wordpress.org/reference/functions/update_option/.
94
95
		return $key;
96
	}
97
98
	/**
99
	 * Set individual option records for sync operations
100
	 *
101
	 * @param string $operation what is the option related to? last pull, current pull, merge, delete, etc.
102
	 * @param string $object_type the Salesforce object type.
103
	 * @param int    $fieldmap_id the ID of the specific fieldmap that is running.
104
	 * @param mixed  $value the value to be saved in the option.
105
	 * @param bool   $autoload whether to autoload the option value.
106
	 * @return bool  $result value of the save operation.
107
	 */
108
	public function set( $operation, $object_type = '', $fieldmap_id = '', $value = '', $autoload = true ) {
109
		// generate the option key parameters.
110
		$params = array(
111
			'operation'   => $operation,
112
			'object_type' => $object_type,
113
			'fieldmap_id' => $fieldmap_id,
114
		);
115
		$key    = $this->generate_option_key( $params );
116
		$value  = isset( $value ) ? $value : '';
117
118
		/*
119
		 * examples
120
		 * object_sync_for_salesforce_pull_last_sync_Contact_1
121
		 * object_sync_for_salesforce_currently_pulling_query_Contact_1
122
		 * object_sync_for_salesforce_pull_merge_last_Contact_1
123
		 * object_sync_for_salesforce_pull_delete_last_Contact_1
124
		 */
125
126
		$result = update_option( $key, $value, $autoload );
127
128
		if ( true === $result ) {
129
			$legacy_key = $this->generate_option_key( $params, true );
130
			// if the legacy key exists and the keys are not the same, we might need to upgrade.
131
			if ( get_option( $legacy_key ) && $key !== $legacy_key ) {
132
				$this->legacy_option_upgrade( $operation, $object_type, $fieldmap_id );
0 ignored issues
show
Bug introduced by
It seems like $fieldmap_id can also be of type string; however, parameter $fieldmap_id of Object_Sync_Sf_Pull_Opti...legacy_option_upgrade() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

132
				$this->legacy_option_upgrade( $operation, $object_type, /** @scrutinizer ignore-type */ $fieldmap_id );
Loading history...
133
			}
134
		}
135
		return $result;
136
	}
137
138
	/**
139
	 * Set individual option records for sync operations
140
	 *
141
	 * @param string $operation what is the option related to? last pull, current pull, merge, delete, etc.
142
	 * @param string $object_type the Salesforce object type.
143
	 * @param int    $fieldmap_id the ID of the specific fieldmap that is running.
144
	 * @param mixed  $value the value to be saved in the option.
145
	 * @param bool   $autoload whether to autoload the option value.
146
	 * @return bool  $result value of the save operation.
147
	 */
148
	private function legacy_option_upgrade( $operation, $object_type = '', $fieldmap_id = '', $value = '', $autoload = true ) {
0 ignored issues
show
Unused Code introduced by
The parameter $value 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

148
	private function legacy_option_upgrade( $operation, $object_type = '', $fieldmap_id = '', /** @scrutinizer ignore-unused */ $value = '', $autoload = true ) {

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...
Unused Code introduced by
The parameter $autoload 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

148
	private function legacy_option_upgrade( $operation, $object_type = '', $fieldmap_id = '', $value = '', /** @scrutinizer ignore-unused */ $autoload = true ) {

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...
149
		$result       = false;
150
		$legacy_value = $this->legacy_get( $operation, $object_type, $fieldmap_id );
0 ignored issues
show
Bug introduced by
It seems like $fieldmap_id can also be of type string; however, parameter $fieldmap_id of Object_Sync_Sf_Pull_Options::legacy_get() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

150
		$legacy_value = $this->legacy_get( $operation, $object_type, /** @scrutinizer ignore-type */ $fieldmap_id );
Loading history...
151
		if ( false !== $legacy_value ) {
152
			// generate the option key parameters.
153
			$params = array(
154
				'operation'   => $operation,
155
				'object_type' => $object_type,
156
				'fieldmap_id' => $fieldmap_id,
157
			);
158
			$key    = $this->generate_option_key( $params, true );
159
			$this->add_upgradeable_key( $key );
160
			$result = $this->set( $operation, $object_type, $fieldmap_id, $legacy_value );
0 ignored issues
show
Bug introduced by
It seems like $fieldmap_id can also be of type string; however, parameter $fieldmap_id of Object_Sync_Sf_Pull_Options::set() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

160
			$result = $this->set( $operation, $object_type, /** @scrutinizer ignore-type */ $fieldmap_id, $legacy_value );
Loading history...
161
			if ( true === $result ) {
162
				$this->legacy_delete( $key );
163
			}
164
		}
165
		return $result;
166
	}
167
168
	/**
169
	 * Get individual option records for sync operations
170
	 *
171
	 * @param string $operation what is the option related to? last pull, current pull, merge, delete, etc.
172
	 * @param string $object_type the Salesforce object type.
173
	 * @param int    $fieldmap_id the ID of the specific fieldmap that is running.
174
	 * @param mixed  $default the default value for the option.
175
	 * @return mixed $value the value of the item. False if it's empty.
176
	 */
177
	public function get( $operation, $object_type = '', $fieldmap_id = '', $default = false ) {
178
		// generate the option key parameters.
179
		$params = array(
180
			'operation'   => $operation,
181
			'object_type' => $object_type,
182
			'fieldmap_id' => $fieldmap_id,
183
		);
184
		$key    = $this->generate_option_key( $params );
185
		$value  = get_option( $key, $default );
186
187
		/*
188
		 * examples
189
		 * object_sync_for_salesforce_pull_last_sync_Contact_1
190
		 * object_sync_for_salesforce_currently_pulling_query_Contact_1
191
		 * object_sync_for_salesforce_pull_merge_last_Contact_1
192
		 * object_sync_for_salesforce_pull_delete_last_Contact_1
193
		 */
194
195
		// if the new option value does exist but it has a default value, try to upgrade the old one.
196
		if ( get_option( $key ) && $default === $value ) {
197
			$legacy_key = $this->generate_option_key( $params, true );
198
			// if the keys are not the same, we might need to upgrade.
199
			if ( $key !== $legacy_key ) {
200
				$this->legacy_option_upgrade( $operation, $object_type, $fieldmap_id, $value );
0 ignored issues
show
Bug introduced by
It seems like $fieldmap_id can also be of type string; however, parameter $fieldmap_id of Object_Sync_Sf_Pull_Opti...legacy_option_upgrade() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

200
				$this->legacy_option_upgrade( $operation, $object_type, /** @scrutinizer ignore-type */ $fieldmap_id, $value );
Loading history...
201
			}
202
		}
203
		return $value;
204
	}
205
206
	/**
207
	 * Get legacy named individual option records for sync operations
208
	 *
209
	 * @param string $operation what is the option related to? last pull, current pull, merge, delete, etc.
210
	 * @param string $object_type the Salesforce object type.
211
	 * @param int    $fieldmap_id the ID of the specific fieldmap that is running.
212
	 * @param mixed  $default the default value for the option.
213
	 * @return mixed $value the value of the item. False if it's empty.
214
	 */
215
	public function legacy_get( $operation, $object_type = '', $fieldmap_id, $default = false ) {
216
		// generate the option key parameters.
217
		$params = array(
218
			'operation'   => $operation,
219
			'object_type' => $object_type,
220
			'fieldmap_id' => $fieldmap_id,
221
		);
222
		$key    = $this->generate_option_key( $params, true );
223
		$value  = get_option( $key, $default );
224
		return $value;
225
	}
226
227
	/**
228
	 * Delete the individual option records for sync operation
229
	 *
230
	 * @param string $operation what is the option related to? last pull, current pull, merge, delete, etc.
231
	 * @param string $object_type the Salesforce object type.
232
	 * @param int    $fieldmap_id the ID of the specific fieldmap that is running.
233
	 * @return bool  $result True if successful, false otherwise.
234
	 */
235
	public function delete( $operation, $object_type = '', $fieldmap_id = '' ) {
236
		// generate the option key parameters.
237
		$params = array(
238
			'operation'   => $operation,
239
			'object_type' => $object_type,
240
			'fieldmap_id' => $fieldmap_id,
241
		);
242
		$key    = $this->generate_option_key( $params );
243
		$result = delete_option( $key );
244
		return $result;
245
	}
246
247
	/**
248
	 * Delete the legacy individual option records for sync operation
249
	 *
250
	 * @param string $key the legacy key to delete.
251
	 * @return bool  $result True if successful, false otherwise.
252
	 */
253
	public function legacy_delete( $key ) {
254
		$result = delete_option( $key );
255
		if ( true === $result ) {
256
			$this->remove_upgradeable_key( $key );
257
		}
258
		return $result;
259
	}
260
261
	/**
262
	 * Add an option key to the array of upgradeable keys.
263
	 *
264
	 * @param string $key the key to add to the array.
265
	 * @return array $this->upgradeable_keys the array of keys.
266
	 */
267
	private function add_upgradeable_key( $key ) {
268
		$keys   = $this->get_upgradeable_keys();
269
		$keys[] = $key;
270
		$keys   = array_unique( $keys );
271
		$result = update_option( $this->option_prefix . 'upgradeable_keys', $keys );
272
		if ( true === $result ) {
273
			$this->upgradeable_keys = $keys;
0 ignored issues
show
Documentation Bug introduced by
It seems like $keys of type array is incompatible with the declared type string of property $upgradeable_keys.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
274
			return $this->upgradeable_keys;
275
		}
276
	}
277
278
	/**
279
	 * Remove an option key from the array of upgradeable keys.
280
	 *
281
	 * @param string $key the key to remove from the array.
282
	 * @return array $this->upgradeable_keys the array of keys.
283
	 */
284
	private function remove_upgradeable_key( $key ) {
285
		$keys      = $this->get_upgradeable_keys();
286
		$array_key = array_search( $key, $keys, true );
287
		if ( false !== $array_key ) {
288
			unset( $keys[ $array_key ] );
289
		}
290
		$result = update_option( $this->option_prefix . 'upgradeable_keys', $keys );
291
		if ( true === $result ) {
292
			$this->upgradeable_keys = $keys;
0 ignored issues
show
Documentation Bug introduced by
It seems like $keys of type array is incompatible with the declared type string of property $upgradeable_keys.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
293
			if ( empty( $keys ) ) {
294
				delete_option( $this->option_prefix . 'upgradeable_keys' );
295
			}
296
			return $this->upgradeable_keys;
297
		}
298
	}
299
300
	/**
301
	 * Get the array of upgradeable keys.
302
	 *
303
	 * @return array $this->upgradeable_keys the array of keys.
304
	 */
305
	private function get_upgradeable_keys() {
306
		$keys                   = get_option( $this->option_prefix . 'upgradeable_keys', array() );
307
		$keys                   = array_unique( $keys );
0 ignored issues
show
Bug introduced by
It seems like $keys can also be of type false; however, parameter $array of array_unique() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

307
		$keys                   = array_unique( /** @scrutinizer ignore-type */ $keys );
Loading history...
308
		$this->upgradeable_keys = $keys;
0 ignored issues
show
Documentation Bug introduced by
It seems like $keys of type array is incompatible with the declared type string of property $upgradeable_keys.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
309
		return $this->upgradeable_keys;
310
	}
311
}
312