Completed
Push — master ( 3d9a80...2c597e )
by Sand
01:53
created

SandCage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 100
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
B call() 0 45 5
A getHttpStatus() 0 5 1
A getResponse() 0 5 1
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 endpoint to request
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 . $service;
42
		$this->post_fields = array('key'=>$this->sandcage_api_key) + $payload;
43
44
		if ($callback_endpoint != '') {
45
			$this->post_fields['callback_url'] = $callback_endpoint;
46
		}
47
48
		// Initialize the cURL session
49
		$ch = curl_init($service_endpoint);
50
51
		curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
52
53
		// Handle open_basedir & safe mode
54
		if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
55
			$this->follow_location = true;
56
		}
57
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow_location);
58
		curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); 
59
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
60
		curl_setopt($ch, CURLOPT_POST, TRUE);
61
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->post_fields));
62
63
		// Execute the cURL session
64
		$this->response = curl_exec($ch);
65
66
		// Retry if certificates are missing.
67
		if (curl_errno($ch) == CURLE_SSL_CACERT) {
68
69
			// Set the pem file holding the CA Root Certificates to verify the peer with.
70
			curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
71
			
72
			// Retry execution after setting CURLOPT_CAINFO
73
			$this->response = curl_exec($ch);
74
			
75
		}
76
77
		// Get information regarding the transfer
78
		$this->status = curl_getinfo($ch);
79
80
		// Close the cURL session
81
		curl_close($ch);
82
83
	}
84
85
	/** 
86
	 * Return the HTTP status of the call
87
	 * @return array or FALSE on failure 
88
	 */ 
89
	public function getHttpStatus() {
90
91
		return $this->status;
92
93
	}
94
95
	/** 
96
	 * Return the HTTP status of the call
97
	 * @return array or FALSE on failure  
98
	 */ 
99
	public function getResponse() {
100
101
		return $this->response;
102
103
	}
104
}
105