Completed
Push — develop ( 1d4633...83eb6a )
by David
02:35 queued 10s
created

Default_Api_Service::request()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 7
dl 0
loc 31
rs 9.424
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
	/**
12
	 * @var string
13
	 */
14
	private $wordlift_key;
15
	/**
16
	 * @var int
17
	 */
18
	private $timeout;
19
20
	/**
21
	 * @var string
22
	 */
23
	private $user_agent;
24
25
	/**
26
	 * @var array
27
	 */
28
	private $headers;
29
	/**
30
	 * @var string
31
	 */
32
	private $base_url;
33
34
	/**
35
	 * Default_Api_Service constructor.
36
	 *
37
	 * @param string $base_url
38
	 * @param int $timeout
39
	 * @param string $user_agent
40
	 * @param string $wordlift_key
41
	 */
42
	public function __construct( $base_url, $timeout, $user_agent, $wordlift_key ) {
43
44
		$this->base_url     = $base_url;
45
		$this->timeout      = $timeout;
46
		$this->user_agent   = $user_agent;
47
		$this->wordlift_key = $wordlift_key;
48
49
		$this->headers = array(
50
			'Content-Type'  => 'application/json',
51
			'Authorization' => "Key $wordlift_key",
52
			'Expect'        => '',
53
		);
54
55
	}
56
57
	public function request( $method, $url, $headers = array(), $body = null, $timeout = null, $user_agent = null, $args = array() ) {
58
59
		// Get the timeout for this request.
60
		$request_timeout = isset( $timeout ) ? $timeout : $this->timeout;
61
62
		// Set the time limit if lesser than our request timeout.
63
		$max_execution_time = ini_get( 'max_execution_time' );
64
		if ( $max_execution_time < $request_timeout ) {
65
			@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...
66
		}
67
68
		$request_url = $this->base_url . $url;
69
70
		// Create the request args in the following order:
71
		//  1. use `$args` as base if provided.
72
		//  2. set the custom timeout if provided.
73
		//  3. set the custom user-agent if provided.
74
		//  4. merge the API headers to the provided headers.
75
		//  5. add the body.
76
		//
77
		// In this way the user can fully control the request if wanted (using `$args`) and we can add our defaults.
78
		$request_args = $args + array(
79
				'method'     => $method,
80
				'timeout'    => $request_timeout,
81
				'user-agent' => isset( $user_agent ) ? $user_agent : $this->user_agent,
82
				'headers'    => $headers + $this->headers,
83
				'body'       => $body,
84
			);
85
86
		return new Response( wp_remote_request( $request_url, $request_args ) );
87
	}
88
89
	public function get( $url, $headers = array(), $body = null, $timeout = null, $user_agent = null, $args = array() ) {
90
91
		return $this->request( 'GET', $url, $headers, $body, $timeout, $user_agent, $args );
92
	}
93
94
}
95