Overview

Namespaces

  • Angelfon
    • SDK
      • Exceptions
      • Http
      • Rest
        • Api
          • V099
            • User

Classes

  • Angelfon\SDK\Domain
  • Angelfon\SDK\Http\GuzzleClient
  • Angelfon\SDK\Http\Response
  • Angelfon\SDK\InstanceContext
  • Angelfon\SDK\InstanceResource
  • Angelfon\SDK\ListResource
  • Angelfon\SDK\Options
  • Angelfon\SDK\Page
  • Angelfon\SDK\Rest\Api
  • Angelfon\SDK\Rest\Api\V099
  • Angelfon\SDK\Rest\Api\V099\User\CallContext
  • Angelfon\SDK\Rest\Api\V099\User\CallInstance
  • Angelfon\SDK\Rest\Api\V099\User\CallList
  • Angelfon\SDK\Rest\Api\V099\User\CallOptions
  • Angelfon\SDK\Rest\Api\V099\User\CallPage
  • Angelfon\SDK\Rest\Api\V099\User\CreateCallOptions
  • Angelfon\SDK\Rest\Api\V099\User\CreateSmsOptions
  • Angelfon\SDK\Rest\Api\V099\User\ReadCallOptions
  • Angelfon\SDK\Rest\Api\V099\User\ReadSmsOptions
  • Angelfon\SDK\Rest\Api\V099\User\SmsContext
  • Angelfon\SDK\Rest\Api\V099\User\SmsInstance
  • Angelfon\SDK\Rest\Api\V099\User\SmsList
  • Angelfon\SDK\Rest\Api\V099\User\SmsOptions
  • Angelfon\SDK\Rest\Api\V099\User\SmsPage
  • Angelfon\SDK\Rest\Api\V099\UserContext
  • Angelfon\SDK\Rest\Api\V099\UserInstance
  • Angelfon\SDK\Rest\Client
  • Angelfon\SDK\Serialize
  • Angelfon\SDK\Values
  • Angelfon\SDK\Version
  • Angelfon\SDK\VersionInfo

Interfaces

  • Angelfon\SDK\Http\Client

Exceptions

  • Angelfon\SDK\Exceptions\AngelfonException
  • Angelfon\SDK\Exceptions\ConfigurationException
  • Angelfon\SDK\Exceptions\RestException
  • Overview
  • Namespace
  • Class
  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;


/**
* A client for accessing the Angelfon API
* @author Fernando Mora G.
*/
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";

    /** @var string Account Username */
    protected $username = '';   

    /** @var string Account Password */
    protected $password = '';

    /** @var int App Client ID */
    protected $clientId;

    /** @var string App Client Secret */
    protected $clientSecret = '';

    /** @var \Angelfon\SDK\Http\Client HTTP Client */
    protected $httpClient;

    /** @var string Angelfon API Access Token */
    protected $accessToken = '';

    /** @var \Angelfon\SDK\Rest\Api Api domain */
    protected $_api = null;

    /**
     * @param string $username Account Username
     * @param string $password Account Password
     * @param string $clientId Application Client ID
     * @param string $clientSecret Application Client Secret
     * @param \Angelfon\SDK\Http\Client|null $httpClient HTTP Client for requests
     * @param mixed[] $environment Evironment to look for credentials, defaults to $_ENV
     * @throws \Angelfon\SDK\Exceptions\ConfigurationException When not valid authentication
     */
    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();
    }

    /**
     * 
     * Perform a Request using the HTTP Client
     * 
     * @param  string $method Http Method
     * @param  string $uri Fully qualified URL
     * @param  string[] $params Query string parameters
     * @param  string[] $data POST body data
     * @param  string[] $headers HTTP Headers
     * @param  string $username Account Username
     * @param  string $password Account Password
     * @param  string $clientId Application Client ID
     * @param  string $clientSecret Application Client Secret
     * @param  int $timeout HTTP Request timeout
     * @return \Angelfon\SDK\Http\Response Response from Angelfon API
     */
    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();
    }
    
  }

    /**
  * Get current username
  * @return string
  */
    public function getUsername(){
        return $this->username;
    }

    /**
  * Get current password
  * @return string
  */
    public function getPassword(){
        return $this->password;
    }

    /**
  * Get current clientId
  * @return int
  */
    public function getClientId(){
        return $this->clientId;
    }

    /**
  * Get current clientSecret
  * @return string
  */
    public function getClientSecret(){
        return $this->clientSecret;
    }

    /**
     * Get current HTTP Client
     * @return \Angelfon\SDK\Http\Client
     */
    public function getHttpClient()
    {
        return $this->httpClient;
    }

    /**
     * Get the Angelfon API Access Token
     * @return string
     */
    public function getAccessToken()
    {
        return $this->accessToken;
    }

    /**
     * Access the Api Angelfon Domain
     * @return \Angelfon\SDK\Rest\Api The Api Domain
     */
    public function getApi()
    {
        if (!$this->_api) $this->_api = new Api($this);
        return $this->_api;
    }

    /**
   * @return \Angelfon\SDK\Rest\Api\V099\User\CallList 
   */
  protected function getCalls() {
    return $this->api->v099->user->calls;
  }

  /**
   * @param string $id Sms id that uniquely identifies the Sms to fetch
   * @return \Angelfon\SDK\Rest\Api\V099\User\SmsContext 
   */
  protected function contextSms($id) {
    return $this->api->v099->user->sms($id);
  }

    /**
   * @return \Angelfon\SDK\Rest\Api\V099\User\SmsList 
   */
  protected function getSms() {
    return $this->api->v099->user->sms;
  }

  /**
   * @param string $id Call id that uniquely identifies the Call to fetch
   * @return \Angelfon\SDK\Rest\Api\V099\User\CallContext 
   */
  protected function contextCalls($id) {
    return $this->api->v099->user->calls($id);
  }
    

    /**
   * Magic getter to lazy load domains
   * 
   * @param string $name Domain to return
   * @return \Angelfon\SDK\Domain The requested domain
   * @throws \Angelfon\SDK\Exceptions\AngelfonException For unknown domains
   */
  public function __get($name) {
    $method = 'get' . ucfirst($name);
    if (method_exists($this, $method)) {
        return $this->$method();
    }

    throw new AngelfonException('Unknown domain ' . $name);
  }

  /**
   * Magic call to lazy load contexts
   * 
   * @param string $name Context to return
   * @param mixed[] $arguments Context to return
   * @return \Angelfon\SDK\InstanceContext The requested context
   * @throws \Angelfon\SDK\Exceptions\AngelfonException For unknown contexts
   */
  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);
  }
}
API documentation generated by ApiGen