Completed
Push — add/e2e-pre-connection-tests ( c0b034...386e24 )
by Yaroslav
13:12 queued 02:56
created

Jetpack_IXR_Client::query()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 0
dl 0
loc 42
rs 8.6257
c 0
b 0
f 0
1
<?php
2
/**
3
 * IXR_Client
4
 *
5
 * @package automattic/jetpack-connection
6
 *
7
 * @since 1.5
8
 * @since 7.7 Moved to the jetpack-connection package.
9
 */
10
11
use Automattic\Jetpack\Connection\Client;
12
13
/**
14
 * A Jetpack implementation of the WordPress core IXR client.
15
 */
16
class Jetpack_IXR_Client extends IXR_Client {
17
	/**
18
	 * Jetpack args, used for the remote requests.
19
	 *
20
	 * @var array
21
	 */
22
	public $jetpack_args = null;
23
24
	/**
25
	 * Constructor.
26
	 * Initialize a new Jetpack IXR client instance.
27
	 *
28
	 * @param array       $args    Jetpack args, used for the remote requests.
29
	 * @param string|bool $path    Path to perform the reuqest to.
30
	 * @param int         $port    Port number.
31
	 * @param int         $timeout The connection timeout, in seconds.
32
	 */
33
	public function __construct( $args = array(), $path = false, $port = 80, $timeout = 15 ) {
34
		$defaults = array(
35
			'url'     => Jetpack::xmlrpc_api_url(),
36
			'user_id' => 0,
37
		);
38
39
		$args = wp_parse_args( $args, $defaults );
40
41
		$this->jetpack_args = $args;
42
43
		$this->IXR_Client( $args['url'], $path, $port, $timeout );
44
	}
45
46
	/**
47
	 * Perform the IXR request.
48
	 *
49
	 * @return bool True if request succeeded, false otherwise.
50
	 */
51
	public function query() {
52
		$args    = func_get_args();
53
		$method  = array_shift( $args );
54
		$request = new IXR_Request( $method, $args );
55
		$xml     = trim( $request->getXml() );
56
57
		$response = Client::remote_request( $this->jetpack_args, $xml );
58
59
		if ( is_wp_error( $response ) ) {
60
			$this->error = new IXR_Error( -10520, sprintf( 'Jetpack: [%s] %s', $response->get_error_code(), $response->get_error_message() ) );
0 ignored issues
show
Bug introduced by
The method get_error_code() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method get_error_message() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
			return false;
62
		}
63
64
		if ( ! $response ) {
65
			$this->error = new IXR_Error( -10520, 'Jetpack: Unknown Error' );
66
			return false;
67
		}
68
69
		if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
70
			$this->error = new IXR_Error( -32300, 'transport error - HTTP status code was not 200' );
71
			return false;
72
		}
73
74
		$content = wp_remote_retrieve_body( $response );
75
76
		// Now parse what we've got back.
77
		$this->message = new IXR_Message( $content );
78
		if ( ! $this->message->parse() ) {
79
			// XML error.
80
			$this->error = new IXR_Error( -32700, 'parse error. not well formed' );
81
			return false;
82
		}
83
84
		// Is the message a fault?
85
		if ( 'fault' === $this->message->messageType ) {
86
			$this->error = new IXR_Error( $this->message->faultCode, $this->message->faultString );
87
			return false;
88
		}
89
90
		// Message must be OK.
91
		return true;
92
	}
93
94
	/**
95
	 * Retrieve the Jetpack error from the result of the last request.
96
	 *
97
	 * @param int    $fault_code   Fault code.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $fault_code not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
98
	 * @param string $fault_string Fault string.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $fault_string not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
99
	 * @return Jetpack_Error Error object.
100
	 */
101
	public function get_jetpack_error( $fault_code = null, $fault_string = null ) {
102
		if ( is_null( $fault_code ) ) {
103
			$fault_code = $this->error->code;
104
		}
105
106
		if ( is_null( $fault_string ) ) {
107
			$fault_string = $this->error->message;
108
		}
109
110
		if ( preg_match( '#jetpack:\s+\[(\w+)\]\s*(.*)?$#i', $fault_string, $match ) ) {
111
			$code    = $match[1];
112
			$message = $match[2];
113
			$status  = $fault_code;
114
			return new Jetpack_Error( $code, $message, $status );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with $code.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
115
		}
116
117
		return new Jetpack_Error( "IXR_{$fault_code}", $fault_string );
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Error::__construct() has too many arguments starting with "IXR_{$fault_code}".

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
118
	}
119
}
120