Completed
Push — add/sync-rest-2 ( 066938...fa68b0 )
by
unknown
09:11
created

Jetpack_Sync_Queue::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
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 $items;
8
	public $id;
9
	public function __construct( $items ) {
10
		$this->id = uniqid();
11
		$this->items = $items;
12
	}
13
}
14
15
/**
16
 * A persistent queue that can be flushed in increments of N items,
17
 * and which blocks reads until checked-out buffers are checked in or
18
 * closed
19
 */
20
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...
21
	public $id;
22
	private $items = array();
23
	private $checkout = null;
24
	private $checkout_size = 10;
25
26
	function __construct( $id ) {
27
		$this->id = $id;
28
	}
29
30
	function add( $item ) {
31
		$this->items[] = $item;
32
	}
33
34
	function add_all( $items ) {
35
		$this->items += $items;
36
	}
37
38
	function reset() {
39
		$this->items = array();
40
	}
41
42
	function size() {
43
		return count( $this->items );
44
	}
45
46
	function checkout() {
47
		if ( $this->checkout ) {
48
			return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' );
49
		}
50
		$buffer = new Jetpack_Sync_Queue_Buffer( array_slice( $this->items, 0, $this->checkout_size ) );
51
		$this->checkout = $buffer;
52
		return $buffer;
53
	}
54
55
	function checkin( $buffer ) {
56
		$is_valid = $this->validate_checkout( $buffer );
57
58
		if ( is_wp_error( $is_valid ) ) {
59
			return $is_valid;
60
		}
61
62
		$this->checkout = null;
63
64
		return true;
65
	}
66
67
	function close( $buffer ) {
68
		$is_valid = $this->validate_checkout( $buffer );
69
70
		if ( is_wp_error( $is_valid ) ) {
71
			return $is_valid;
72
		}
73
74
		$this->checkout = null;
75
76
		$this->items = array_slice( $this->items, count( $buffer->items ) );
77
78
		return true;
79
	}
80
81
	function flush_all() {
82
		$items_reference = $this->items;
83
		$this->items = array();
84
		return $items_reference;
85
	}
86
87
	function set_checkout_size( $new_size ) {
88
		$this->checkout_size = $new_size;
89
	}
90
91
	private function validate_checkout( $buffer ) {
92
		if ( ! $buffer instanceof Jetpack_Sync_Queue_Buffer ) {
93
			return new WP_Error( 'not_a_buffer', 'You must checkin an instance of Jetpack_Sync_Queue_Buffer' );
94
		}
95
96
		if ( !$this->checkout ) {
97
			return new WP_Error( 'buffer_not_checked_out', 'There are no checked out buffers' );
98
		}
99
100
		if ( $this->checkout->id != $buffer->id ) {
101
			return new WP_Error( 'buffer_mismatch', 'The buffer you checked in was not checked out' );
102
		}
103
104
		return true;
105
	}
106
}