1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281:
<?php
namespace Angelfon\SDK\Rest;
use Angelfon\SDK\Exceptions\ConfigurationException;
use Angelfon\SDK\Exceptions\RestException;
use Angelfon\SDK\Exceptions\AngelfonException;
use Angelfon\SDK\Http\Client as HttpClient;
use Angelfon\SDK\Http\GuzzleClient;
use Angelfon\SDK\VersionInfo;
class Client
{
const ENV_USERNAME = "ANGELFON_USERNAME";
const ENV_PASSWORD = "ANGELFON_PASSWORD";
const ENV_CLIENT_ID = "ANGELFON_CLIENT_ID";
const ENV_CLIENT_SECRET = "ANGELFON_CLIENT_SECRET";
protected $username = '';
protected $password = '';
protected $clientId;
protected $clientSecret = '';
protected $httpClient;
protected $accessToken = '';
protected $_api = null;
public function __construct($username = null, $password = null, $clientId = null, $clientSecret = null, HttpClient $httpClient = null, $environment = null)
{
if (is_null($environment)) {
$environment = $_ENV;
}
if ($username) {
$this->username = $username;
} elseif (array_key_exists(self::ENV_USERNAME, $environment)) {
$this->username = $environment[self::ENV_USERNAME];
}
if ($password) {
$this->password = $password;
} elseif (array_key_exists(self::ENV_PASSWORD, $environment)) {
$this->password = $environment[self::ENV_PASSWORD];
}
if ($clientId) {
$this->clientId = $clientId;
} elseif (array_key_exists(self::ENV_CLIENT_ID, $environment)) {
$this->clientId = $environment[self::ENV_CLIENT_ID];
}
if ($clientSecret) {
$this->clientSecret = $clientSecret;
} elseif (array_key_exists(self::ENV_CLIENT_SECRET, $environment)) {
$this->clientSecret = $environment[self::ENV_CLIENT_SECRET];
}
if (!$this->username || !$this->password || !$this->clientId || !$this->clientSecret)
throw new ConfigurationException("Missing credentials to create a Client");
if ($httpClient) {
$this->httpClient = $httpClient;
} else {
$this->httpClient = new GuzzleClient();
}
$this->obtainAccessToken();
}
public function request($method, $uri, $params = [], $data = [], $headers = [], $timeout = null)
{
$headers['User-Agent'] = 'angelfon-php/' . VersionInfo::string() .
' (PHP ' . phpversion() . ')';
$headers['Accept-Charset'] = 'utf-8';
if ($this->accessToken) $headers['Authorization'] = 'Bearer ' . $this->accessToken;
if ($method == 'POST' && !array_key_exists('Content-Type', $headers)) {
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
if (!array_key_exists('Accept', $headers)) {
$headers['Accept'] = 'application/json';
}
return $this->getHttpClient()->request(
$method,
$uri,
$params,
$data,
$headers,
$timeout
);
}
public function obtainAccessToken()
{
$data = [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'grant_type' => 'password',
'username' => $this->username,
'password' => $this->password
];
$response = $this->request('POST', 'https://api.angelfon.com/0.99/oauth/token', null, $data);
$content = $response->getContent();
$code = $response->getStatusCode();
$validResponse = array_key_exists('access_token', $content);
if ($validResponse) $this->accessToken = $content['access_token'];
else {
if (array_key_exists('message', $content))
throw new RestException($content['message'], $code, $code);
else if (array_key_exists('data', $content))
throw new RestException($content['data'], $content['error'], $code);
else throw new RestException();
}
}
public function getUsername(){
return $this->username;
}
public function getPassword(){
return $this->password;
}
public function getClientId(){
return $this->clientId;
}
public function getClientSecret(){
return $this->clientSecret;
}
public function getHttpClient()
{
return $this->httpClient;
}
public function getAccessToken()
{
return $this->accessToken;
}
public function getApi()
{
if (!$this->_api) $this->_api = new Api($this);
return $this->_api;
}
protected function getCalls() {
return $this->api->v099->user->calls;
}
protected function contextSms($id) {
return $this->api->v099->user->sms($id);
}
protected function getSms() {
return $this->api->v099->user->sms;
}
protected function contextCalls($id) {
return $this->api->v099->user->calls($id);
}
public function __get($name) {
$method = 'get' . ucfirst($name);
if (method_exists($this, $method)) {
return $this->$method();
}
throw new AngelfonException('Unknown domain ' . $name);
}
public function __call($name, $arguments) {
$method = 'context' . ucfirst($name);
if (method_exists($this, $method)) {
return call_user_func_array(array($this, $method), $arguments);
}
throw new AngelfonException('Unknown context ' . $name);
}
}