Completed
Push — master ( c7dd50...3d9a80 )
by Sand
01:49
created

SandCage::call()   C

Complexity

Conditions 11
Paths 40

Size

Total Lines 56
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 6.5481
c 0
b 0
f 0
cc 11
eloc 28
nc 40
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php 
2
3
namespace SandCage;
4
5
class SandCage {
6
	// Your SandCage API Key
7
	// This can be retrieved from https://www.sandcage.com/panel/api_key
8
	protected $sandcage_api_key = '[YOUR SANDCAGE API KEY]';
9
	
10
	// SandCage API version
11
	protected $sandcage_api_version = '0.2';
12
13
	// SandCage API endpoint base
14
	protected $sandcage_api_endpoint_base;
15
16
	protected $user_agent;
17
	protected $follow_location = false;
18
	protected $timeout = 30;
19
	protected $post_fields;
20
	protected $status;
21
	protected $response;
22
23
	public function __construct($sandcage_api_key = null) {
24
25
		if (!is_null($sandcage_api_key)) {
26
			$this->sandcage_api_key = $sandcage_api_key;
27
		}
28
		$this->sandcage_api_endpoint_base = 'https://api.sandcage.com/' . $this->sandcage_api_version . '/';
29
		$this->user_agent = 'SandCage - ' . $this->sandcage_api_version;
30
31
	}
32
33
	/** 
34
	 * Send a requst using cURL 
35
	 * @param string $service being requested
36
	 * @param array $payload values to send
37
	 * @param string $callback_endpoint to send the callback to. Optional
38
	 */ 
39
	public function call($service, $payload, $callback_endpoint = '') {
40
41
		$service_endpoint = $this->sandcage_api_endpoint_base;
42
43
		if ($service == 'scheduleTasks') {
44
			$service_endpoint .= 'schedule-tasks';
45
		} else if ($service == 'destroyFiles') {
46
			$service_endpoint .= 'destroy-files';
47
		} else if ($service == 'getInfo') {
48
			$service_endpoint .= 'get-info';
49
		} else if ($service == 'listFiles') {
50
			$service_endpoint .= 'list-files';
51
		}
52
53
		$this->post_fields = array('key'=>$this->sandcage_api_key) + $payload;
54
55
		if ((($service == 'scheduleTasks') || ($service == 'destroyFiles')) && ($callback_endpoint != '')) {
56
			$this->post_fields['callback_url'] = $callback_endpoint;
57
		}
58
59
		// Initialize the cURL session
60
		$ch = curl_init($service_endpoint);
61
62
		curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
63
64
		// Handle open_basedir & safe mode
65
		if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
66
			$this->follow_location = true;
67
		}
68
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow_location);
69
		curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); 
70
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
71
		curl_setopt($ch, CURLOPT_POST, TRUE);
72
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->post_fields));
73
74
		// Execute the cURL session
75
		$this->response = curl_exec($ch);
76
77
		// Retry if certificates are missing.
78
		if (curl_errno($ch) == CURLE_SSL_CACERT) {
79
80
			// Set the pem file holding the CA Root Certificates to verify the peer with.
81
			curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
82
			
83
			// Retry execution after setting CURLOPT_CAINFO
84
			$this->response = curl_exec($ch);
85
			
86
		}
87
88
		// Get information regarding the transfer
89
		$this->status = curl_getinfo($ch);
90
91
		// Close the cURL session
92
		curl_close($ch);
93
94
	}
95
96
	/** 
97
	 * Return the HTTP status of the call
98
	 * @return array or FALSE on failure 
99
	 */ 
100
	public function getHttpStatus() {
101
102
		return $this->status;
103
104
	}
105
106
	/** 
107
	 * Return the HTTP status of the call
108
	 * @return array or FALSE on failure  
109
	 */ 
110
	public function getResponse() {
111
112
		return $this->response;
113
114
	}
115
}
116