Completed
Push — add/sso-analytics ( 683a91...e67d7d )
by
unknown
162:06 queued 152:34
created

Jetpack_Sync_Server   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 69
rs 10
wmc 6
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A set_codec() 0 3 1
A receive() 0 54 4
A __construct() 0 4 1
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
13
	// this is necessary because you can't use "new" when you declare instance properties >:(
14
	function __construct() {
15
		$this->codec            = new Jetpack_Sync_Deflate_Codec();
16
		$this->events_processed = array();
0 ignored issues
show
Bug introduced by
The property events_processed does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
	}
18
19
	function set_codec( iJetpack_Sync_Codec $codec ) {
20
		$this->codec = $codec;
21
	}
22
23
	function receive( $data, $token = null ) {
24
		$start_time = microtime( true );
25
		if ( ! is_array( $data ) ) {
26
			return new WP_Error( 'action_decoder_error', 'Events must be an array' );
27
		}
28
29
		$events = wp_unslash( array_map( array( $this->codec, 'decode' ), $data ) );
30
31
		/**
32
		 * Fires when an array of actions are received from a remote Jetpack site
33
		 *
34
		 * @since 4.1
35
		 *
36
		 * @param array Array of actions received from the remote site
37
		 */
38
		do_action( "jetpack_sync_remote_actions", $events, $token );
39
40
		foreach ( $events as $key => $event ) {
41
			list( $action_name, $args, $user_id, $timestamp ) = $event;
42
			/**
43
			 * Fires when an action is received from a remote Jetpack site
44
			 *
45
			 * @since 4.1
46
			 *
47
			 * @param string $action_name The name of the action executed on the remote site
48
			 * @param array $args The arguments passed to the action
49
			 * @param int $user_id The external_user_id who did the action
50
			 * @param double $timestamp Timestamp (in seconds) when the action occurred
51
			 * @param array $token The auth token used to invoke the API
52
			 */
53
			do_action( 'jetpack_sync_remote_action', $action_name, $args, $user_id, $timestamp, $token );
54
55
			/**
56
			 * Fires when an action is received from a remote Jetpack site
57
			 *
58
			 * @since 4.1
59
			 *
60
			 * @param array $args The arguments passed to the action
61
			 * @param int $user_id The external_user_id who did the action
62
			 * @param double $timestamp Timestamp (in seconds) when the action occurred
63
			 * @param array $token The auth token used to invoke the API
64
			 */
65
			do_action( 'jetpack_sync_' . $action_name, $args, $user_id, $timestamp, $token );
66
67
			$this->events_processed[] = $key;
68
69
			// TODO this can be improved to be more intelligent
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
70
			if ( microtime( true ) - $start_time > self::MAX_TIME_PER_REQUEST_IN_SECONDS ) {
71
				break;
72
			}
73
		}
74
75
		return $this->events_processed;
76
	}
77
}