Completed
Push — develop ( 1d2391...a94b56 )
by David
02:55 queued 11s
created

Default_Api_Service::request()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 16
nop 7
dl 0
loc 41
rs 8.6417
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 */
6
7
namespace Wordlift\Api;
8
9
class Default_Api_Service implements Api_Service {
10
	/**
11
	 * @var Default_Api_Service
12
	 */
13
	private static $instance;
14
15
	/**
16
	 * @var string
17
	 */
18
	private $wordlift_key;
19
	/**
20
	 * @var int
21
	 */
22
	private $timeout;
23
24
	/**
25
	 * @var string
26
	 */
27
	private $user_agent;
28
29
	/**
30
	 * @var array
31
	 */
32
	private $headers;
33
	/**
34
	 * @var string
35
	 */
36
	private $base_url;
37
38
	/**
39
	 * @var \Wordlift_Log_Service
40
	 */
41
	private $log;
42
43
	/**
44
	 * Default_Api_Service constructor.
45
	 *
46
	 * @param string $base_url
47
	 * @param int $timeout
48
	 * @param string $user_agent
49
	 * @param string $wordlift_key
50
	 */
51
	public function __construct( $base_url, $timeout, $user_agent, $wordlift_key ) {
52
53
		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
54
55
		$this->base_url     = $base_url;
56
		$this->timeout      = $timeout;
57
		$this->user_agent   = $user_agent;
58
		$this->wordlift_key = $wordlift_key;
59
60
		$this->headers = array(
61
			'Content-Type'  => 'application/json',
62
			'Authorization' => "Key $wordlift_key",
63
			'Expect'        => '',
64
		);
65
66
		self::$instance = $this;
67
	}
68
69
	public static function get_instance() {
70
		return self::$instance;
71
	}
72
73
	public function request( $method, $url, $headers = array(), $body = null, $timeout = null, $user_agent = null, $args = array() ) {
74
75
		// Get the timeout for this request.
76
		$request_timeout = isset( $timeout ) ? $timeout : $this->timeout;
77
78
		// Set the time limit if lesser than our request timeout.
79
		$max_execution_time = ini_get( 'max_execution_time' );
80
		if ( $max_execution_time < $request_timeout ) {
81
			@set_time_limit( $request_timeout );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
82
		}
83
84
		$request_url = $this->base_url . $url;
85
86
		// Create the request args in the following order:
87
		//  1. use `$args` as base if provided.
88
		//  2. set the custom timeout if provided.
89
		//  3. set the custom user-agent if provided.
90
		//  4. merge the API headers to the provided headers.
91
		//  5. add the body.
92
		//
93
		// In this way the user can fully control the request if wanted (using `$args`) and we can add our defaults.
94
		$request_args = $args + array(
95
				'method'     => $method,
96
				'timeout'    => $request_timeout,
97
				'user-agent' => isset( $user_agent ) ? $user_agent : $this->user_agent,
98
				'headers'    => $headers + $this->headers,
99
				'body'       => $body,
100
			);
101
102
		$response = wp_remote_request( $request_url, $request_args );
103
104
		if ( defined( 'WL_DEBUG' ) && WL_DEBUG ) {
105
			$this->log->trace(
106
				"=== REQUEST  ===========================\n"
107
				. var_export( $request_args, true )
108
				. "=== RESPONSE ===========================\n"
109
				. var_export( $response, true ) );
110
		}
111
112
		return new Response( $response );
113
	}
114
115
	public function get( $url, $headers = array(), $body = null, $timeout = null, $user_agent = null, $args = array() ) {
116
117
		return $this->request( 'GET', $url, $headers, $body, $timeout, $user_agent, $args );
118
	}
119
120
}
121