Completed
Push — add/sync-rest-2 ( 928b66...5c8420 )
by
unknown
09:26
created

Jetpack_Sync_Queue::add_all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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 size() {
39
		return count( $this->items );
40
	}
41
42
	function checkout() {
43
		if ( $this->checkout ) {
44
			return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' );
45
		}
46
		$buffer = new Jetpack_Sync_Queue_Buffer( array_slice( $this->items, 0, $this->checkout_size ) );
47
		$this->checkout = $buffer;
48
		return $buffer;
49
	}
50
51
	function checkin( $buffer ) {
52
		$is_valid = $this->validate_checkout( $buffer );
53
54
		if ( is_wp_error( $is_valid ) ) {
55
			return $is_valid;
56
		}
57
58
		$this->checkout = null;
59
60
		return true;
61
	}
62
63
	function close( $buffer ) {
64
		$is_valid = $this->validate_checkout( $buffer );
65
66
		if ( is_wp_error( $is_valid ) ) {
67
			return $is_valid;
68
		}
69
70
		$this->checkout = null;
71
72
		$this->items = array_slice( $this->items, count( $buffer->items ) );
73
74
		return true;
75
	}
76
77
	function flush_all() {
78
		$items_reference = $this->items;
79
		$this->items = array();
80
		return $items_reference;
81
	}
82
83
	function set_checkout_size( $new_size ) {
84
		$this->checkout_size = $new_size;
85
	}
86
87
	private function validate_checkout( $buffer ) {
88
		if ( ! $buffer instanceof Jetpack_Sync_Queue_Buffer ) {
89
			return new WP_Error( 'not_a_buffer', 'You must checkin an instance of Jetpack_Sync_Queue_Buffer' );
90
		}
91
92
		if ( !$this->checkout ) {
93
			return new WP_Error( 'buffer_not_checked_out', 'There are no checked out buffers' );
94
		}
95
96
		if ( $this->checkout->id != $buffer->id ) {
97
			return new WP_Error( 'buffer_mismatch', 'The buffer you checked in was not checked out' );
98
		}
99
100
		return true;
101
	}
102
}