Completed
Push — master ( 81ca8e...f62b70 )
by Sand
01:53
created

SandCage::destroyFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
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
	 * The "schedule-tasks" service
35
	 * @param array $payload values to send
36
	 * @param string $callback_endpoint to send the callback to
37
	 */ 
38 View Code Duplication
	public function scheduleFiles($payload, $callback_endpoint = '') {
39
40
		$this->post_fields = array('key'=>$this->sandcage_api_key) + $payload;
41
42
		if ($callback_endpoint != '') {
43
			$this->post_fields['callback_url'] = $callback_endpoint;
44
		}
45
46
		$this->call($this->sandcage_api_endpoint_base . 'schedule-tasks');
47
48
	}
49
50
	/** 
51
	 * The "destroy-files" service
52
	 * @param array $payload values to send
53
	 * @param string $callback_endpoint to send the callback to
54
	 */ 
55 View Code Duplication
	public function destroyFiles($payload, $callback_endpoint = '') {
56
57
		$this->post_fields = array('key'=>$this->sandcage_api_key) + $payload;
58
59
		if ($callback_endpoint != '') {
60
			$this->post_fields['callback_url'] = $callback_endpoint;
61
		}
62
63
		$this->call($this->sandcage_api_endpoint_base . 'destroy-files');
64
65
	}
66
67
	/** 
68
	 * The "list-files" service
69
	 * @param array $payload values to send
70
	 */ 
71 View Code Duplication
	public function listFiles($payload) {
72
73
		$this->post_fields = array('key'=>$this->sandcage_api_key) + $payload;
74
75
		$this->call($this->sandcage_api_endpoint_base . 'list-files');
76
77
	}
78
79
	/** 
80
	 * The "get-info" service
81
	 * @param array $payload values to send
82
	 */ 
83 View Code Duplication
	public function getInfo($payload) {
84
85
		$this->post_fields = array('key'=>$this->sandcage_api_key) + $payload;
86
87
		$this->call($this->sandcage_api_endpoint_base . 'get-info');
88
89
	}
90
91
	/** 
92
	 * Send a requst using cURL 
93
	 * @param string $service_endpoint to request
94
	 */ 
95
	public function call($service_endpoint) {
96
97
		// Initialize the cURL session
98
		$ch = curl_init($service_endpoint);
99
100
		curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
101
102
		// Handle open_basedir & safe mode
103
		if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
104
			$this->follow_location = true;
105
		}
106
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow_location);
107
		curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); 
108
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
109
		curl_setopt($ch, CURLOPT_POST, TRUE);
110
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->post_fields));
111
112
		// Execute the cURL session
113
		$this->response = curl_exec($ch);
114
115
		// Retry if certificates are missing.
116
		if (curl_errno($ch) == CURLE_SSL_CACERT) {
117
118
			// Set the pem file holding the CA Root Certificates to verify the peer with.
119
			curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
120
			
121
			// Retry execution after setting CURLOPT_CAINFO
122
			$this->response = curl_exec($ch);
123
			
124
		}
125
126
		// Get information regarding the transfer
127
		$this->status = curl_getinfo($ch);
128
129
		// Close the cURL session
130
		curl_close($ch);
131
132
	}
133
134
	/** 
135
	 * Return the HTTP status of the call
136
	 * @return array or FALSE on failure 
137
	 */ 
138
	public function getHttpStatus() {
139
140
		return $this->status;
141
142
	}
143
144
	/** 
145
	 * Return the HTTP status of the call
146
	 * @return array or FALSE on failure  
147
	 */ 
148
	public function getResponse() {
149
150
		return $this->response;
151
152
	}
153
}
154