Completed
Pull Request — master (#2165)
by Justin
05:28
created

WPSC_Query_Base   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 332
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 332
rs 10
c 0
b 0
f 0
wmc 24
lcom 2
cbo 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
fetch() 0 1 ?
A exists() 0 4 1
B get() 0 17 5
prepare_get() 0 1 ?
A get_data() 0 7 2
prepare_get_data() 0 1 ?
A get_meta() 0 9 3
A prepare_get_meta() 0 3 1
set() 0 1 ?
save() 0 1 ?
B set_meta() 0 17 5
A save_meta() 0 3 1
A cache_get() 0 3 1
A cache_set() 0 3 1
A cache_delete() 0 3 1
A get_group_id() 0 9 3
1
<?php
2
/**
3
 * The WP eCommerce Base Query Class
4
 *
5
 * @package wp-e-commerce
6
 * @since 4.0
7
 */
8
9
abstract class WPSC_Query_Base {
10
11
	/**
12
	 * An array of arrays of cache keys. Allows versioning the cached values,
13
	 * and busting cache for a group if needed (by incrementing the version).
14
	 *
15
	 * Should be structured like:
16
	 *
17
	 * protected $group_ids = array(
18
	 * 	'group_id' => array(
19
	 * 		'group'   => 'full_group_id_key',
20
	 * 		'version' => 1,
21
	 * 	),
22
	 * 	'group2_id' => array(
23
	 * 		'group'   => 'full_group2_id_key',
24
	 * 		'version' => 1,
25
	 * 	),
26
	 * );
27
	 *
28
	 * And fetched like:
29
	 *
30
	 * $value = $this->cache_get( $cache_key, 'group_id' );
31
	 *
32
	 * And set like:
33
	 *
34
	 * $this->cache_set( $cache_key, $value, 'group_id' );
35
	 *
36
	 * @var array
37
	 */
38
	protected $group_ids = array();
39
40
	/**
41
	 * Contains the values fetched from the DB
42
	 *
43
	 * @access protected
44
	 * @since 4.0
45
	 *
46
	 * @var array
47
	 */
48
	protected $data = array();
49
50
	/**
51
	 * Data that is not directly stored inside the DB but is inferred. Optional.
52
	 *
53
	 * @access protected
54
	 * @since 4.0
55
	 */
56
	protected $meta_data = array();
57
58
	/**
59
	 * True if the DB row is fetched into the $data array.
60
	 *
61
	 * @access protected
62
	 * @since 4.0
63
	 *
64
	 * @var string
65
	 */
66
	protected $fetched = false;
67
68
	/**
69
	 * True if the row exists in DB
70
	 *
71
	 * @access protected
72
	 * @since 4.0
73
	 *
74
	 * @var string
75
	 */
76
	protected $exists = false;
77
78
	/**
79
	 * Fetches the actual $data array.
80
	 *
81
	 * @access protected
82
	 * @since 4.0
83
	 *
84
	 * @return void
85
	 */
86
	abstract protected function fetch();
87
88
	/**
89
	 * Whether the DB row for this purchase log exists
90
	 *
91
	 * @access public
92
	 * @since 4.0
93
	 *
94
	 * @return bool True if it exists. Otherwise false.
95
	 */
96
	public function exists() {
97
		$this->fetch();
98
		return $this->exists;
99
	}
100
101
	/**
102
	 * Returns the value of the specified property of the $data array if it exists.
103
	 *
104
	 * @access public
105
	 * @since  4.0
106
	 *
107
	 * @param  string $key Name of the property (column)
108
	 * @return mixed
109
	 */
110
	public function get( $key ) {
111
112
		// lazy load the row if it's not fetched from the database yet
113
		if ( empty( $this->data ) || ! array_key_exists( $key, $this->data ) ) {
114
			$this->fetch();
115
		}
116
117
		if ( isset( $this->data[ $key ] ) ) {
118
			$value = $this->data[ $key ];
119
		} else if ( isset( $this->meta_data[ $key ] ) ) {
120
			$value = $this->meta_data[ $key ];
121
		} else {
122
			$value = null;
123
		}
124
125
		return $this->prepare_get( $value, $key );
126
	}
127
128
	/**
129
	 * Prepares the return value for get() (apply_filters, etc).
130
	 *
131
	 * @access protected
132
	 * @since  4.0
133
	 *
134
	 * @param  mixed  $value Value fetched
135
	 * @param  string $key   Key for $data.
136
	 *
137
	 * @return mixed
138
	 */
139
	abstract protected function prepare_get( $value, $key );
140
141
	/**
142
	 * Returns the entire $data array.
143
	 *
144
	 * @access public
145
	 * @since  4.0
146
	 *
147
	 * @return array
148
	 */
149
	public function get_data() {
150
		if ( empty( $this->data ) ) {
151
			$this->fetch();
152
		}
153
154
		return $this->prepare_get_data();
155
	}
156
157
	/**
158
	 * Prepares the return value for get_data() (apply_filters, etc).
159
	 *
160
	 * @access protected
161
	 * @since  4.0
162
	 *
163
	 * @return mixed
164
	 */
165
	abstract protected function prepare_get_data();
166
167
	/**
168
	 * Returns the entire $meta_data array.
169
	 *
170
	 * @access public
171
	 * @since  4.0
172
	 *
173
	 * @return array
174
	 */
175
	public function get_meta() {
176
177
		// lazy load the row if it's not fetched from the database yet
178
		if ( empty( $this->data ) && empty( $this->meta_data ) ) {
179
			$this->fetch();
180
		}
181
182
		return $this->prepare_get_meta();
183
	}
184
185
	/**
186
	 * Prepares the return value for get_meta() (apply_filters, etc).
187
	 *
188
	 * @access protected
189
	 * @since  4.0
190
	 *
191
	 * @return mixed
192
	 */
193
	protected function prepare_get_meta() {
194
		return $this->meta_data;
195
	}
196
197
	/**
198
	 * Sets a property to a certain value. This function accepts a key and a value
199
	 * as arguments, or an associative array containing key value pairs.
200
	 *
201
	 * @access public
202
	 * @since  4.0
203
	 *
204
	 * @param mixed $key        Name of the property (column), or an array containing
205
	 *                          key value pairs
206
	 * @param string|int $value Optional. Defaults to null. In case $key is a string,
0 ignored issues
show
Documentation introduced by
Should the type for parameter $value not be string|integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
207
	 *                          this should be specified.
208
	 * @return WPSC_Query_Base  The current object (for method chaining)
209
	 */
210
	abstract public function set( $key, $value = null );
211
212
	/**
213
	 * Saves the object back to the database.
214
	 *
215
	 * @access public
216
	 * @since  4.0
217
	 *
218
	 * @return mixed
219
	 */
220
	abstract public function save();
221
222
	/**
223
	 * Sets a meta property to a certain value. This function should accept a key
224
	 * and a value as arguments, or an associative array containing key value pairs.
225
	 *
226
	 * @access public
227
	 * @since  4.0
228
	 *
229
	 * @param mixed $key        Name of the property (column), or an array containing
230
	 *                          key value pairs
231
	 * @param string|int $value Optional. Defaults to null. In case $key is a string,
0 ignored issues
show
Documentation introduced by
Should the type for parameter $value not be string|integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
232
	 *                          this should be specified.
233
	 * @return WPSC_Query_Base  The current object (for method chaining)
234
	 */
235
	public function set_meta( $key, $value = null ) {
236
		if ( is_array( $key ) ) {
237
			foreach ( $key as $index => $value) {
238
				$this->set_meta( $index, $value );
239
			}
240
			return;
241
		}
242
243
		// lazy load the row if it's not fetched from the database yet
244
		if ( empty( $this->data ) && empty( $this->meta_data ) ) {
245
			$this->fetch();
246
		}
247
248
		$this->meta_data[ $key ] = $value;
249
250
		return $this;
251
	}
252
253
	/**
254
	 * Saves the meta data back to the database.
255
	 *
256
	 * @access public
257
	 * @since  4.0
258
	 *
259
	 * @return WPSC_Query_Base  The current object (for method chaining)
260
	 */
261
	public function save_meta() {
262
		return $this;
263
	}
264
265
	/**
266
	 * Wrapper for wp_cache_get.
267
	 *
268
	 * @access public
269
	 * @since 4.0
270
	 *
271
	 * @see wp_cache_get()
272
	 *
273
	 * @param int|string $key      The key under which the cache contents are stored.
274
	 * @param string     $group_id The key for the group_ids array to compile the group
275
	 *                             from version/key.
276
	 *                             Default 0 (no expiration).
277
	 * @return bool False on failure, true on success
278
	 */
279
	public function cache_get( $key, $group_id ) {
280
		return wp_cache_get( $key, $this->get_group_id( $group_id ) );
281
	}
282
283
	/**
284
	 * Wrapper for wp_cache_set.
285
	 *
286
	 * @access public
287
	 * @since 4.0
288
	 *
289
	 * @see wp_cache_set()
290
	 *
291
	 * @param int|string $key      The cache key to use for retrieval later.
292
	 * @param mixed      $data     The contents to store in the cache.
293
	 * @param string     $group_id The key for the group_ids array to compile the group
294
	 *                             from version/key.
295
	 * @param int        $expire   Optional. When to expire the cache contents, in seconds.
296
	 *                             Default 0 (no expiration).
297
	 * @return bool False on failure, true on success
298
	 */
299
	public function cache_set( $key, $data, $group_id, $expire = 0 ) {
300
		return wp_cache_set( $key, $data, $this->get_group_id( $group_id ), $expire );
301
	}
302
303
	/**
304
	 * Wrapper for wp_cache_delete.
305
	 *
306
	 * @access public
307
	 * @since 4.0
308
	 *
309
	 * @see wp_cache_delete()
310
	 *
311
	 * @param int|string $key      What the contents in the cache are called.
312
	 * @param string     $group_id The key for the group_ids array to compile the group
313
	 *                             from version/key.
314
	 * @return bool True on successful removal, false on failure.
315
	 */
316
	public function cache_delete( $key, $group_id ) {
317
		return wp_cache_delete( $key, $this->get_group_id( $group_id ) );
318
	}
319
320
	/**
321
	 * Get the versioned group id from the $group_ids array.
322
	 *
323
	 * @since  4.0
324
	 *
325
	 * @param  string $group_id The key for the group_ids array to compile the group
326
	 *                          from version/key.
327
	 *
328
	 * @return string           The full group key.
329
	 */
330
	public function get_group_id( $group_id ) {
331
		$group = $this->group_ids[ $group_id ]['group'];
332
333
		if ( isset( $this->group_ids[ $group_id ]['version'] ) && $this->group_ids[ $group_id ]['version'] ) {
334
			$group .= '_' . $this->group_ids[ $group_id ]['version'];
335
		}
336
337
		return $group;
338
	}
339
340
}
341