Completed
Push — add/send-queue-id-with-sync ( 0824d1 )
by
unknown
106:39 queued 94:48
created

Jetpack_Sync_Server::receive()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 62
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 19
c 1
b 0
f 0
nc 8
nop 4
dl 0
loc 62
rs 7.3333

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
require_once dirname( __FILE__ ) . '/class.jetpack-sync-json-deflate-codec.php';
4
5
/**
6
 * Simple version of a Jetpack Sync Server - just receives arrays of events and
7
 * issues them locally with the 'jetpack_sync_remote_action' action.
8
 */
9
class Jetpack_Sync_Server {
10
	private $codec;
11
	const MAX_TIME_PER_REQUEST_IN_SECONDS = 15;
12
	const BLOG_LOCK_TRANSIENT_PREFIX = 'jp_sync_req_lock_';
13
	const BLOG_LOCK_TRANSIENT_EXPIRY = 60; // seconds
14
15
	// this is necessary because you can't use "new" when you declare instance properties >:(
16
	function __construct() {
17
		$this->codec = new Jetpack_Sync_JSON_Deflate_Codec();
18
	}
19
20
	function set_codec( iJetpack_Sync_Codec $codec ) {
21
		$this->codec = $codec;
22
	}
23
24
	function attempt_request_lock( $blog_id, $expiry = self::BLOG_LOCK_TRANSIENT_EXPIRY ) {
25
		$transient_name = $this->get_concurrent_request_transient_name( $blog_id );
26
		$locked_time    = get_site_transient( $transient_name );
27
		if ( $locked_time ) {
28
			return false;
29
		}
30
		set_site_transient( $transient_name, microtime( true ), $expiry );
31
32
		return true;
33
	}
34
35
	private function get_concurrent_request_transient_name( $blog_id ) {
36
		return self::BLOG_LOCK_TRANSIENT_PREFIX . $blog_id;
37
	}
38
39
	function remove_request_lock( $blog_id ) {
40
		delete_site_transient( $this->get_concurrent_request_transient_name( $blog_id ) );
41
	}
42
43
	function receive( $data, $token = null, $sent_timestamp = null, $queue_id = null ) {
44
		$start_time = microtime( true );
45
		if ( ! is_array( $data ) ) {
46
			return new WP_Error( 'action_decoder_error', 'Events must be an array' );
47
		}
48
49
		if ( $token && ! $this->attempt_request_lock( $token->blog_id ) ) {
50
			/**
51
			 * Fires when the server receives two concurrent requests from the same blog
52
			 *
53
			 * @since 4.2.0
54
			 *
55
			 * @param token The token object of the misbehaving site
56
			 */
57
			do_action( 'jetpack_sync_multi_request_fail', $token );
58
59
			return new WP_Error( 'concurrent_request_error', 'There is another request running for the same blog ID' );
60
		}
61
62
		$events           = wp_unslash( array_map( array( $this->codec, 'decode' ), $data ) );
63
		$events_processed = array();
64
65
		/**
66
		 * Fires when an array of actions are received from a remote Jetpack site
67
		 *
68
		 * @since 4.2.0
69
		 *
70
		 * @param array Array of actions received from the remote site
71
		 */
72
		do_action( 'jetpack_sync_remote_actions', $events, $token );
73
74
		foreach ( $events as $key => $event ) {
75
			list( $action_name, $args, $user_id, $timestamp ) = $event;
76
77
			/**
78
			 * Fires when an action is received from a remote Jetpack site
79
			 *
80
			 * @since 4.2.0
81
			 *
82
			 * @param string $action_name The name of the action executed on the remote site
83
			 * @param array $args The arguments passed to the action
84
			 * @param int $user_id The external_user_id who did the action
85
			 * @param double $timestamp Timestamp (in seconds) when the action occurred
86
			 * @param double $sent_timestamp Timestamp (in seconds) when the action was transmitted
87
			 * @param string $queue_id ID of the queue from which the event was sent (sync or full_sync)
88
			 * @param array $token The auth token used to invoke the API
89
			 */
90
			do_action( 'jetpack_sync_remote_action', $action_name, $args, $user_id, $timestamp, $sent_timestamp, $queue_id, $token );
91
92
			$events_processed[] = $key;
93
94
			if ( microtime( true ) - $start_time > self::MAX_TIME_PER_REQUEST_IN_SECONDS ) {
95
				break;
96
			}
97
		}
98
99
		if ( $token ) {
100
			$this->remove_request_lock( $token->blog_id );
101
		}
102
103
		return $events_processed;
104
	}
105
}
106