Passed
Push — 414-update-transients-for-mult... ( 154897...e694b4 )
by Jonathan
03:12
created

Object_Sync_Sf_Sync_Transients::set()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 6
nop 5
dl 0
loc 28
rs 9.5555
c 0
b 0
f 0
1
<?php
2
/**
3
 * Handles getting and setting the transients.
4
 *
5
 * @class   Object_Sync_Sf_Sync_Transients
6
 * @package Object_Sync_Salesforce
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Object_Sync_Sf_Sync_Transients class.
13
 */
14
class Object_Sync_Sf_Sync_Transients {
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
	 * Transient keys that can be upgraded
32
	 *
33
	 * @var string
34
	 */
35
	private $upgradeable_keys;
36
37
	/**
38
	 * Constructor for transient records class
39
	 */
40
	public function __construct() {
41
		$this->version          = object_sync_for_salesforce()->version;
42
		$this->option_prefix    = object_sync_for_salesforce()->option_prefix;
43
		$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...
44
45
	}
46
47
	/**
48
	 * Generate a transient key
49
	 *
50
	 * @param array $params the pieces to put together.
51
	 * @param bool  $legacy whether this is a legacy key.
52
	 * @return string $key the full transient key.
53
	 */
54
	private function generate_transient_key( $params, $legacy = false ) {
55
		array_unshift( $params, substr( $this->option_prefix, 0, -1 ) ); // add the prefixes.
56
		$params = array_filter( $params, fn( $value ) => ! is_null( $value ) && '' !== $value ); // remove null and empty values.
57
58
		// legacy keys don't have a fieldmap.
59
		if ( true === $legacy && isset( $params['fieldmap_id'] ) ) {
60
			unset( $params['fieldmap_id'] );
61
		}
62
63
		// make the key a string.
64
		$key = implode( '_', $params );
65
66
		// allow developers to filter the key.
67
		$key = apply_filters( $this->option_prefix . 'transient_key', $key, $params );
68
69
		/* // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
70
		add_filter( 'object_sync_for_salesforce_transient_key', 'change_transient_key', 10, 2 );
71
		function change_transient_key( $key, $params ) {
72
			$key = 'my_key_name';
73
			return $key;
74
		}
75
		*/
76
77
		if ( true === $legacy ) {
78
			// allow developers to filter the legacy key.
79
			$key = apply_filters( $this->option_prefix . 'transient_legacy_key', $key, $params );
80
		}
81
82
		// note: the WordPress codex indicates that option names do not need to be escaped.
83
		// see: https://developer.wordpress.org/reference/functions/update_option/.
84
85
		return $key;
86
	}
87
88
	/**
89
	 * Set individual transient records for sync operations
90
	 *
91
	 * @param string $operation what is the transient related to? last pull, current pull, merge, delete, etc.
92
	 * @param string $object_type the Salesforce object type.
93
	 * @param int    $fieldmap_id the ID of the specific fieldmap that is running.
94
	 * @param mixed  $value the value to be saved in the option.
95
	 * @param int    $expiration whether to expire the transient.
96
	 * @return bool  $result value of the save operation.
97
	 */
98
	public function set( $operation, $object_type = '', $fieldmap_id = '', $value = '', $expiration = 0 ) {
99
		// generate the option key parameters.
100
		$params = array(
101
			'operation'   => $operation,
102
			'object_type' => $object_type,
103
			'fieldmap_id' => $fieldmap_id,
104
		);
105
		$key    = $this->generate_transient_key( $params );
106
		$value  = isset( $value ) ? $value : '';
107
108
		/*
109
		 * examples
110
		 * object_sync_for_salesforce_pull_last_sync_Contact_1
111
		 * object_sync_for_salesforce_currently_pulling_query_Contact_1
112
		 * object_sync_for_salesforce_pull_merge_last_Contact_1
113
		 * object_sync_for_salesforce_pull_delete_last_Contact_1
114
		 */
115
116
		$result = set_transient( $key, $value, $expiration );
117
118
		if ( true === $result ) {
119
			$legacy_key = $this->generate_transient_key( $params, true );
120
			// if the legacy key exists and the keys are not the same, we might need to upgrade.
121
			if ( get_transient( $legacy_key ) && $key !== $legacy_key ) {
122
				$this->legacy_transient_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_Sync_Tran...acy_transient_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

122
				$this->legacy_transient_upgrade( $operation, $object_type, /** @scrutinizer ignore-type */ $fieldmap_id );
Loading history...
123
			}
124
		}
125
		return $result;
126
	}
127
128
	/**
129
	 * Set individual transient records for sync operations
130
	 *
131
	 * @param string $operation what is the option related to? last pull, current pull, merge, delete, etc.
132
	 * @param string $object_type the Salesforce object type.
133
	 * @param int    $fieldmap_id the ID of the specific fieldmap that is running.
134
	 * @param mixed  $value the value to be saved in the option.
135
	 * @param int    $expiration whether to expire the transient
136
	 * @return bool  $result value of the save operation.
137
	 */
138
	private function legacy_transient_upgrade( $operation, $object_type = '', $fieldmap_id = '', $value = '', $expiration = 0 ) {
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

138
	private function legacy_transient_upgrade( $operation, $object_type = '', $fieldmap_id = '', /** @scrutinizer ignore-unused */ $value = '', $expiration = 0 ) {

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 $expiration 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

138
	private function legacy_transient_upgrade( $operation, $object_type = '', $fieldmap_id = '', $value = '', /** @scrutinizer ignore-unused */ $expiration = 0 ) {

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...
139
		$result       = false;
140
		$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_Sync_Transients::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

140
		$legacy_value = $this->legacy_get( $operation, $object_type, /** @scrutinizer ignore-type */ $fieldmap_id );
Loading history...
141
		if ( false !== $legacy_value ) {
142
			// generate the option key parameters.
143
			$params = array(
144
				'operation'   => $operation,
145
				'object_type' => $object_type,
146
				'fieldmap_id' => $fieldmap_id,
147
			);
148
			$key    = $this->generate_transient_key( $params, true );
149
			$this->add_upgradeable_key( $key );
150
			$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_Sync_Transients::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

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

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

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