Passed
Pull Request — master (#420)
by Jonathan
07:00 queued 03:47
created

Object_Sync_Sf_Pull_Options::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 2
b 0
f 0
nc 1
nop 3
dl 0
loc 10
rs 10
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
			$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

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

144
	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...
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

144
	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...
145
		$result       = false;
146
		$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

146
		$legacy_value = $this->legacy_get( $operation, $object_type, /** @scrutinizer ignore-type */ $fieldmap_id );
Loading history...
147
		if ( false !== $legacy_value ) {
148
			// generate the option key parameters.
149
			$params = array(
150
				'operation'   => $operation,
151
				'object_type' => $object_type,
152
				'fieldmap_id' => $fieldmap_id,
153
			);
154
			$key    = $this->generate_option_key( $params, true );
155
			$this->add_upgradeable_key( $key );
156
			$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

156
			$result = $this->set( $operation, $object_type, /** @scrutinizer ignore-type */ $fieldmap_id, $legacy_value );
Loading history...
157
			if ( true === $result ) {
158
				$this->legacy_delete( $key );
159
			}
160
		}
161
		return $result;
162
	}
163
164
	/**
165
	 * Get individual option records for sync operations
166
	 *
167
	 * @param string $operation what is the option related to? last pull, current pull, merge, delete, etc.
168
	 * @param string $object_type the Salesforce object type.
169
	 * @param int    $fieldmap_id the ID of the specific fieldmap that is running.
170
	 * @param mixed  $default the default value for the option.
171
	 * @return mixed $value the value of the item. False if it's empty.
172
	 */
173
	public function get( $operation, $object_type = '', $fieldmap_id = '', $default = false ) {
174
		// generate the option key parameters.
175
		$params = array(
176
			'operation'   => $operation,
177
			'object_type' => $object_type,
178
			'fieldmap_id' => $fieldmap_id,
179
		);
180
		$key    = $this->generate_option_key( $params );
181
		$value  = get_option( $key, $default );
182
183
		/*
184
		 * examples
185
		 * object_sync_for_salesforce_pull_last_sync_Contact_1
186
		 * object_sync_for_salesforce_currently_pulling_query_Contact_1
187
		 * object_sync_for_salesforce_pull_merge_last_Contact_1
188
		 * object_sync_for_salesforce_pull_delete_last_Contact_1
189
		 */
190
191
		// if the new option value does not exist, try to upgrade the old one.
192
		if ( $default === $value ) {
193
			$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

193
			$this->legacy_option_upgrade( $operation, $object_type, /** @scrutinizer ignore-type */ $fieldmap_id, $value );
Loading history...
194
		}
195
		return $value;
196
	}
197
198
	/**
199
	 * Get legacy named individual option records for sync operations
200
	 *
201
	 * @param string $operation what is the option related to? last pull, current pull, merge, delete, etc.
202
	 * @param string $object_type the Salesforce object type.
203
	 * @param int    $fieldmap_id the ID of the specific fieldmap that is running.
204
	 * @param mixed  $default the default value for the option.
205
	 * @return mixed $value the value of the item. False if it's empty.
206
	 */
207
	public function legacy_get( $operation, $object_type = '', $fieldmap_id, $default = false ) {
208
		// generate the option key parameters.
209
		$params = array(
210
			'operation'   => $operation,
211
			'object_type' => $object_type,
212
			'fieldmap_id' => $fieldmap_id,
213
		);
214
		$key    = $this->generate_option_key( $params, true );
215
		$value  = get_option( $key, $default );
216
		return $value;
217
	}
218
219
	/**
220
	 * Delete the individual option records for sync operation
221
	 *
222
	 * @param string $operation what is the option related to? last pull, current pull, merge, delete, etc.
223
	 * @param string $object_type the Salesforce object type.
224
	 * @param int    $fieldmap_id the ID of the specific fieldmap that is running.
225
	 * @return bool  $result True if successful, false otherwise.
226
	 */
227
	public function delete( $operation, $object_type = '', $fieldmap_id = '' ) {
228
		// generate the option key parameters.
229
		$params = array(
230
			'operation'   => $operation,
231
			'object_type' => $object_type,
232
			'fieldmap_id' => $fieldmap_id,
233
		);
234
		$key    = $this->generate_option_key( $params );
235
		$result = delete_option( $key );
236
		return $result;
237
	}
238
239
	/**
240
	 * Delete the legacy individual option records for sync operation
241
	 *
242
	 * @param string $key the legacy key to delete.
243
	 * @return bool  $result True if successful, false otherwise.
244
	 */
245
	public function legacy_delete( $key ) {
246
		$result = delete_option( $key );
247
		if ( true === $result ) {
248
			$this->remove_upgradeable_key( $key );
249
		}
250
		return $result;
251
	}
252
253
	/**
254
	 * Add an option key to the array of upgradeable keys.
255
	 *
256
	 * @param string $key the key to add to the array.
257
	 * @return array $this->upgradeable_keys the array of keys.
258
	 */
259
	private function add_upgradeable_key( $key ) {
260
		$keys   = $this->get_upgradeable_keys();
261
		$keys[] = $key;
262
		$result = update_option( $this->option_prefix . 'upgradeable_keys', $keys );
263
		if ( true === $result ) {
264
			$this->upgradeable_keys = $keys;
0 ignored issues
show
Documentation Bug introduced by
It seems like $keys of type string[] 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...
265
			return $this->upgradeable_keys;
266
		}
267
	}
268
269
	/**
270
	 * Remove an option key from the array of upgradeable keys.
271
	 *
272
	 * @param string $key the key to remove from the array.
273
	 * @return array $this->upgradeable_keys the array of keys.
274
	 */
275
	private function remove_upgradeable_key( $key ) {
276
		$keys      = $this->get_upgradeable_keys();
277
		$array_key = array_search( $key, $keys, true );
278
		if ( false !== $array_key ) {
279
			unset( $keys[ $array_key ] );
280
		}
281
		$result = update_option( $this->option_prefix . 'upgradeable_keys', $keys );
282
		if ( true === $result ) {
283
			$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...
284
			if ( empty( $keys ) ) {
285
				delete_option( $this->option_prefix . 'upgradeable_keys' );
286
			}
287
			return $this->upgradeable_keys;
288
		}
289
	}
290
291
	/**
292
	 * Get the array of upgradeable keys.
293
	 *
294
	 * @return array $this->upgradeable_keys the array of keys.
295
	 */
296
	private function get_upgradeable_keys() {
297
		$keys                   = get_option( $this->option_prefix . 'upgradeable_keys', array() );
298
		$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

298
		$keys                   = array_unique( /** @scrutinizer ignore-type */ $keys );
Loading history...
299
		$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...
300
		return $this->upgradeable_keys;
301
	}
302
}
303