Completed
Push — update/grey-out-elements-unava... ( 06b29b...1fb283 )
by
unknown
16:50 queued 06:18
created

Jetpack_Sync_Server::receive()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 71
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 7
eloc 20
c 2
b 0
f 1
nc 8
nop 2
dl 0
loc 71
rs 6.7968

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-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_Deflate_Codec();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 12 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
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
		return true;
32
	}
33
34
	private function get_concurrent_request_transient_name( $blog_id ) {
35
		return self::BLOG_LOCK_TRANSIENT_PREFIX.$blog_id;
36
	}
37
38
	function remove_request_lock( $blog_id ) {
39
		delete_site_transient( $this->get_concurrent_request_transient_name( $blog_id ) );
40
	}
41
42
	function receive( $data, $token = null ) {
43
		$start_time = microtime( true );
44
		if ( ! is_array( $data ) ) {
45
			return new WP_Error( 'action_decoder_error', 'Events must be an array' );
46
		}
47
48
		if ( $token && ! $this->attempt_request_lock( $token->blog_id ) ) {
49
			/**
50
			 * Fires when the server receives two concurrent requests from the same blog
51
			 *
52
			 * @since 4.1
53
			 *
54
			 * @param token The token object of the misbehaving site
55
			 */
56
			do_action( "jetpack_sync_multi_request_fail", $token );
57
			return new WP_Error( 'concurrent_request_error', 'There is another request running for the same blog ID' );
58
		}
59
60
		$events = wp_unslash( array_map( array( $this->codec, 'decode' ), $data ) );
61
		$events_processed = array();
62
63
		/**
64
		 * Fires when an array of actions are received from a remote Jetpack site
65
		 *
66
		 * @since 4.1
67
		 *
68
		 * @param array Array of actions received from the remote site
69
		 */
70
		do_action( "jetpack_sync_remote_actions", $events, $token );
71
72
		foreach ( $events as $key => $event ) {
73
			list( $action_name, $args, $user_id, $timestamp ) = $event;
74
75
			/**
76
			 * Fires when an action is received from a remote Jetpack site
77
			 *
78
			 * @since 4.1
79
			 *
80
			 * @param string $action_name The name of the action executed on the remote site
81
			 * @param array $args The arguments passed to the action
82
			 * @param int $user_id The external_user_id who did the action
83
			 * @param double $timestamp Timestamp (in seconds) when the action occurred
84
			 * @param array $token The auth token used to invoke the API
85
			 */
86
			do_action( 'jetpack_sync_remote_action', $action_name, $args, $user_id, $timestamp, $token );
87
88
			/**
89
			 * Fires when an action is received from a remote Jetpack site
90
			 *
91
			 * @since 4.1
92
			 *
93
			 * @param array $args The arguments passed to the action
94
			 * @param int $user_id The external_user_id who did the action
95
			 * @param double $timestamp Timestamp (in seconds) when the action occurred
96
			 * @param array $token The auth token used to invoke the API
97
			 */
98
			do_action( 'jetpack_sync_' . $action_name, $args, $user_id, $timestamp, $token );
99
100
			$events_processed[] = $key;
101
102
			if ( microtime( true ) - $start_time > self::MAX_TIME_PER_REQUEST_IN_SECONDS ) {
103
				break;
104
			}
105
		}
106
107
		if ( $token ) {
108
			$this->remove_request_lock( $token->blog_id );
109
		}
110
111
		return $events_processed;
112
	}
113
}