Completed
Push — add/sync-rest-2 ( 6651c8...e1b6d9 )
by
unknown
23:50 queued 14:29
created

Jetpack_Sync_Queue::checkout()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 9
rs 9.6666
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
/** 
4
 * A buffer of items from the queue that can be checked out
5
 */
6
class Jetpack_Sync_Queue_Buffer {
7
	public $id;
8
	public $items_with_ids;
9
	
10
	public function __construct( $items_with_ids ) {
11
		$this->id = uniqid();
12
		$this->items_with_ids = $items_with_ids;
13
	}
14
15
	public function get_items() {
16
		return array_map( function( $item ) { return $item->value; }, $this->items_with_ids );
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
17
	}
18
19
	public function get_item_ids() {
20
		return array_map( function( $item ) { return $item->id; }, $this->items_with_ids );
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
21
	}
22
}
23
24
/**
25
 * A persistent queue that can be flushed in increments of N items,
26
 * and which blocks reads until checked-out buffers are checked in or
27
 * closed
28
 */
29
class Jetpack_Sync_Queue {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
30
	public $id;
31
	private $checkout_size;
32
33
	function __construct( $id, $checkout_size = 10 ) {
34
		$this->id = str_replace( '-', '_', $id); // necessary to ensure we don't have ID collisions in the SQL
35
		$this->checkout_size = $checkout_size;
36
	}
37
38
	function add( $item ) {
39
		$added = false;
40
		// this basically tries to add the option until enough time has elapsed that
41
		// it has a unique (microtime-based) option key
42
		while(!$added) {
43
			$added = add_option( $this->get_option_name(), $item, null, false );	
44
		}
45
	}
46
47
	function add_all( $items ) {
48
		// TODO: perhaps there's a more efficient version of this?
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
49
		foreach( $items as $item ) {
50
			$this->add( $item );
51
		}
52
	}
53
54
	function reset() {
55
		$this->items = array();
0 ignored issues
show
Bug introduced by
The property items does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
	}
57
58
	function size() {
59
		global $wpdb;
60
		return $wpdb->get_var( $wpdb->prepare( 
61
			"SELECT count(*) FROM $wpdb->options WHERE option_name LIKE %s", "jetpack_sync_queue_{$this->id}-%" 
62
		) );
63
	}
64
65
	function checkout() {
66
		if ( $this->get_checkout_id() ) {
67
			return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' );
68
		}
69
		$items = $this->fetch_items( $this->checkout_size );
70
		$buffer = new Jetpack_Sync_Queue_Buffer( array_slice( $items, 0, $this->checkout_size ) );
71
		$this->set_checkout_id( $buffer->id );
72
		return $buffer;
73
	}
74
75
	function checkin( $buffer ) {
76
		$is_valid = $this->validate_checkout( $buffer );
77
78
		if ( is_wp_error( $is_valid ) ) {
79
			return $is_valid;
80
		}
81
82
		$this->delete_checkout_id();
83
84
		return true;
85
	}
86
87
	function close( $buffer ) {
88
		$is_valid = $this->validate_checkout( $buffer );
89
90
		if ( is_wp_error( $is_valid ) ) {
91
			return $is_valid;
92
		}
93
94
		$this->delete_checkout_id();
95
96
		global $wpdb;
97
98
		// all this fanciness is basically so we can prepare a statement with an IN(id1, id2, id3) clause
99
		$ids_to_remove = $buffer->get_item_ids();
100
		if ( count( $ids_to_remove ) > 0 ) {
101
			$sql = "DELETE FROM $wpdb->options WHERE option_name IN (".implode(', ', array_fill(0, count($ids_to_remove), '%s')).')';
102
			$query = call_user_func_array( array( $wpdb, 'prepare' ), array_merge( array( $sql ), $ids_to_remove ) );
103
			$wpdb->query( $query );
104
		}
105
106
		return true;
107
	}
108
109
	function flush_all() {
110
		global $wpdb;
111
		$items = array_map( function( $item ) { return $item->value; }, $this->fetch_items() );
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
112
		$wpdb->query( $wpdb->prepare( 
113
			"DELETE FROM $wpdb->options WHERE option_name LIKE %s", "jetpack_sync_queue_{$this->id}-%" 
114
		) );
115
		return $items;
116
	}
117
118
	function get_all() {
119
		return $this->items;
120
	}
121
122
	function set_checkout_size( $new_size ) {
123
		$this->checkout_size = $new_size;
124
	}
125
126
	private function get_checkout_id() {
127
		return get_option( "jetpack_sync_queue_{$this->id}-checkout", false );
128
	}
129
130
	private function set_checkout_id( $checkout_id ) {
131
		return add_option( "jetpack_sync_queue_{$this->id}-checkout", $checkout_id, null, true ); // this one we should autoload
132
	}
133
134
	private function delete_checkout_id() {
135
		delete_option( "jetpack_sync_queue_{$this->id}-checkout" );
136
	}
137
138
	private function get_option_name() {
139
		// this option is specifically chosen to, as much as possible, preserve time order
140
		// and minimise the possibility of collisions between multiple processes working 
141
		// at the same time
142
		// TODO: confirm we only need to support PHP5 (otherwise we'll need to emulate microtime as float)
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
143
		// @see: http://php.net/manual/en/function.microtime.php
144
		$timestamp = sprintf( '%.5f', microtime(true) );
145
		return 'jetpack_sync_queue_'.$this->id.'-'.$timestamp.'-'.getmypid();
146
	}
147
148
	private function fetch_items( $limit = null ) {
149
		global $wpdb;
150
151
		if ( $limit ) {
152
			$query_sql = $wpdb->prepare( "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT %d", "jetpack_sync_queue_{$this->id}-%", $limit );
153
		} else {
154
			$query_sql = $wpdb->prepare( "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC", "jetpack_sync_queue_{$this->id}-%" );
155
		}
156
157
		$items = $wpdb->get_results( $query_sql );
158
		foreach( $items as $item ) {
159
			$item->value = maybe_unserialize( $item->value );
160
		} 
161
162
		return $items;
163
	}
164
165
	private function validate_checkout( $buffer ) {
166
		if ( ! $buffer instanceof Jetpack_Sync_Queue_Buffer ) {
167
			return new WP_Error( 'not_a_buffer', 'You must checkin an instance of Jetpack_Sync_Queue_Buffer' );
168
		}
169
170
		$checkout_id = $this->get_checkout_id();
171
172
		if ( !$checkout_id ) {
173
			return new WP_Error( 'buffer_not_checked_out', 'There are no checked out buffers' );
174
		}
175
176
		if ( $checkout_id != $buffer->id ) {
177
			return new WP_Error( 'buffer_mismatch', 'The buffer you checked in was not checked out' );
178
		}
179
180
		return true;
181
	}
182
}