Completed
Push — add/magic-link-for-mobile ( a67c5b...4ebb52 )
by
unknown
13:24 queued 06:01
created

XMLRPC_Connector   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 16.42 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 11
loc 67
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A xmlrpc_methods() 0 8 1
A verify_registration() 0 3 1
A output() 11 15 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Sets up the Connection XML-RPC methods.
4
 *
5
 * @package jetpack-connection
6
 */
7
8
namespace Automattic\Jetpack\Connection;
9
10
/**
11
 * Registers the XML-RPC methods for Connections.
12
 */
13
class XMLRPC_Connector {
14
	/**
15
	 * The Connection Manager.
16
	 *
17
	 * @var Manager
18
	 */
19
	private $connection;
20
21
	/**
22
	 * Constructor.
23
	 *
24
	 * @param Manager $connection The Connection Manager.
25
	 */
26
	public function __construct( Manager $connection ) {
27
		$this->connection = $connection;
28
29
		add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
30
	}
31
32
	/**
33
	 * Attached to the `xmlrpc_methods` filter.
34
	 *
35
	 * @param array $methods The already registered XML-RPC methods.
36
	 * @return array
37
	 */
38
	public function xmlrpc_methods( $methods ) {
39
		return array_merge(
40
			$methods,
41
			array(
42
				'jetpack.verifyRegistration' => array( $this, 'verify_registration' ),
43
			)
44
		);
45
	}
46
47
	/**
48
	 * Handles verification that a site is registered.
49
	 *
50
	 * @param array $registration_data The data sent by the XML-RPC client:
51
	 *                                 [ $secret_1, $user_id ].
52
	 *
53
	 * @return string|IXR_Error
54
	 */
55
	public function verify_registration( $registration_data ) {
56
		return $this->output( $this->connection->handle_registration( $registration_data ) );
57
	}
58
59
	/**
60
	 * Normalizes output for XML-RPC.
61
	 *
62
	 * @param mixed $data The data to output.
63
	 */
64
	private function output( $data ) {
65 View Code Duplication
		if ( is_wp_error( $data ) ) {
66
			$code = $data->get_error_data();
67
			if ( ! $code ) {
68
				$code = -10520;
69
			}
70
71
			return new \IXR_Error(
72
				$code,
73
				sprintf( 'Jetpack: [%s] %s', $data->get_error_code(), $data->get_error_message() )
74
			);
75
		}
76
77
		return $data;
78
	}
79
}
80