Completed
Push — renovate/octokit-rest-18.x ( fdf192...f7756d )
by
unknown
42:56 queued 32:41
created

REST_Sender::queue_pull()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 42

Duplication

Lines 42
Ratio 100 %

Importance

Changes 0
Metric Value
cc 7
nc 13
nop 3
dl 42
loc 42
rs 8.3146
c 0
b 0
f 0
1
<?php
2
/**
3
 * Sync package.
4
 *
5
 * @package  automattic/jetpack-sync
6
 */
7
8
namespace Automattic\Jetpack\Sync;
9
10
/**
11
 * This class will handle checkout of Sync queues for REST Endpoints.
12
 *
13
 * @since 9.9.0
14
 */
15
class REST_Sender {
16
17
	/**
18
	 * Items pending send.
19
	 *
20
	 * @var array
21
	 */
22
	public $items = array();
23
24
	/**
25
	 * Checkout objects from the queue
26
	 *
27
	 * @param string $queue_name   Name of Queue.
28
	 * @param int    $number_of_items Number of Items.
29
	 * @param array  $args          arguments.
30
	 *
31
	 * @return array|WP_Error
32
	 */
33 View Code Duplication
	public function queue_pull( $queue_name, $number_of_items, $args ) {
34
		$queue = new Queue( $queue_name );
35
36
		if ( 0 === $queue->size() ) {
37
			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 );
38
		}
39
40
		$sender = Sender::get_instance();
41
42
		// try to give ourselves as much time as possible.
43
		set_time_limit( 0 );
44
45
		if ( $args['pop'] ) {
46
			$buffer = new Queue_Buffer( 'pop', $queue->pop( $number_of_items ) );
0 ignored issues
show
Bug introduced by
It seems like $queue->pop($number_of_items) targeting Automattic\Jetpack\Sync\Queue::pop() can also be of type null or object; however, Automattic\Jetpack\Sync\...e_Buffer::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
47
		} else {
48
			// let's delete the checkin state.
49
			if ( $args['force'] ) {
50
				$queue->unlock();
51
			}
52
			$buffer = $this->get_buffer( $queue, $number_of_items );
53
		}
54
		// Check that the $buffer is not checkout out already.
55
		if ( is_wp_error( $buffer ) ) {
56
			return new WP_Error( 'buffer_open', "We couldn't get the buffer it is currently checked out", 400 );
57
		}
58
59
		if ( ! is_object( $buffer ) ) {
60
			return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 );
61
		}
62
63
		Settings::set_is_syncing( true );
64
		list( $items_to_send, $skipped_items_ids ) = $sender->get_items_to_send( $buffer, $args['encode'] );
0 ignored issues
show
Bug introduced by
It seems like $buffer can also be of type object<Automattic\Jetpack\Sync\Queue_Buffer>; however, Automattic\Jetpack\Sync\...er::get_items_to_send() does only seem to accept array|object<Automattic\...pack\Sync\Queue_Buffer>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
65
		Settings::set_is_syncing( false );
66
67
		return array(
68
			'buffer_id'      => $buffer->id,
69
			'items'          => $items_to_send,
70
			'skipped_items'  => $skipped_items_ids,
71
			'codec'          => $args['encode'] ? $sender->get_codec()->name() : null,
72
			'sent_timestamp' => time(),
73
		);
74
	}
75
76
	/**
77
	 * Adds Sync items to local property.
78
	 */
79
	public function jetpack_sync_send_data_listener() {
80
		foreach ( func_get_args()[0] as $key => $item ) {
81
			$this->items[ $key ] = $item;
82
		}
83
	}
84
85
	/**
86
	 * Check out a buffer of full sync actions.
87
	 *
88
	 * @return array Sync Actions to be returned to requestor
89
	 */
90 View Code Duplication
	public function immediate_full_sync_pull() {
91
		// try to give ourselves as much time as possible.
92
		set_time_limit( 0 );
93
94
		$original_send_data_cb = array( 'Automattic\Jetpack\Sync\Actions', 'send_data' );
95
		$temp_send_data_cb     = array( $this, 'jetpack_sync_send_data_listener' );
96
97
		Sender::get_instance()->set_enqueue_wait_time( 0 );
98
		remove_filter( 'jetpack_sync_send_data', $original_send_data_cb );
99
		add_filter( 'jetpack_sync_send_data', $temp_send_data_cb, 10, 6 );
100
		Sender::get_instance()->do_full_sync();
101
		remove_filter( 'jetpack_sync_send_data', $temp_send_data_cb );
102
		add_filter( 'jetpack_sync_send_data', $original_send_data_cb, 10, 6 );
103
104
		return array(
105
			'items'          => $this->items,
106
			'codec'          => Sender::get_instance()->get_codec()->name(),
107
			'sent_timestamp' => time(),
108
			'status'         => Actions::get_sync_status(),
109
		);
110
	}
111
112
	/**
113
	 * Checkout items out of the sync queue.
114
	 *
115
	 * @param Queue $queue         Sync Queue.
116
	 * @param int   $number_of_items Number of items to checkout.
117
	 *
118
	 * @return WP_Error
119
	 */
120 View Code Duplication
	protected function get_buffer( $queue, $number_of_items ) {
121
		$start        = time();
122
		$max_duration = 5; // this will try to get the buffer.
123
124
		$buffer   = $queue->checkout( $number_of_items );
125
		$duration = time() - $start;
126
127
		while ( is_wp_error( $buffer ) && $duration < $max_duration ) {
128
			sleep( 2 );
129
			$duration = time() - $start;
130
			$buffer   = $queue->checkout( $number_of_items );
131
		}
132
133
		if ( false === $buffer ) {
134
			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 );
135
		}
136
137
		return $buffer;
138
	}
139
140
}
141