Skip to content

Commit

Permalink
PSR-1, PSR-2 compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent CHAUVEAU committed Feb 8, 2015
1 parent 3c65b30 commit 1067553
Show file tree
Hide file tree
Showing 12 changed files with 618 additions and 440 deletions.
1 change: 1 addition & 0 deletions .gitattributes
@@ -0,0 +1 @@
* text=auto
91 changes: 64 additions & 27 deletions examples/realtime.php
Expand Up @@ -4,8 +4,6 @@
* @see http://thecallr.com/docs/real-time/
*/

// Rt.Command object?
/* Composer */
// require 'vendor/autoload.php';

Expand All @@ -14,15 +12,17 @@
require '../src/THECALLR/Realtime/Request.php';
require '../src/THECALLR/Realtime/Response.php';
require '../src/THECALLR/Realtime/CallFlow.php';
require '../src/THECALLR/Realtime/Command.php';

/* Classes used here */
use \THECALLR\Realtime\Server;
use \THECALLR\Realtime\Request;
use \THECALLR\Realtime\Command;
use \THECALLR\Realtime\CallFlow;

/* Recommended */
date_default_timezone_set('UTC');
set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
set_error_handler(function ($errno, $errstr, $errfile, $errline, array $errcontext) {
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
});

Expand All @@ -31,7 +31,7 @@

/* When a call is inbound (a DID is being called),
this callback will be called */
$flow->onInboundCall(function(Request $request) {
$flow->onInboundCall(function (Request $request) {
// your code

/* your callback **MUST** return a label to execute */
Expand All @@ -40,7 +40,7 @@

/* When an outbound call is answered,
this callback will be called */
$flow->onOutboundCall(function(Request $request) {
$flow->onOutboundCall(function (Request $request) {
// your code

/* label to execute */
Expand All @@ -49,41 +49,78 @@

/* Define a label with a command and its parameters,
along with the **async** result callback */
$flow->define('ask_age', 'read', ['media_id' => 'TTS|TTS_EN-GB_SERENA|Hello there. How old are you?',
'max_digits' => 3,
'attempts' => 3,
'timeout_ms' => 5000], function($result, $error, Request $request) use ($flow) {
// your code
$flow->define(
'ask_age',
'read',
['media_id' => 'TTS|TTS_EN-GB_SERENA|Hello there. How old are you?',
'max_digits' => 3,
'attempts' => 3,
'timeout_ms' => 5000],
function ($result, $error, Request $request) use ($flow) {
// your code

/* if the 'read' command succeeds, the result will be in $result, and $error will be null.
if it fails, the error will be in $error, and result will be null */

/* here we store some variables in the call
they can be used in subsequent labels */
$flow->variables->result = $result;
$flow->variables->error = $error;
$flow->variables->age = $result;
/* label to execute */
return 'say_age';
}
);

/* if the 'read' command succeeds, the result will be in $result, and $error will be null.
if it fails, the error will be in $error, and result will be null */

/* here we store some variables in the call
they can be used in subsequent labels */
$flow->variables->result = $result;
$flow->variables->error = $error;
$flow->variables->age = $result;
/* label to execute */
return 'say_age';
});
/* This label is using the $age variable store above */
$flow->define(
'say_age',
'play',
['media_id' => 'TTS|TTS_EN-GB_SERENA|You are {age} years old.'],
function ($result, $error, Request $request) {
/* special label to hangup */
return '_hangup';
}
);

// /* This label is using the $age variable store above */
// $flow->define('say_age2',
// new Command('play',
// ['media_id' => 'TTS|TTS_EN-GB_SERENA|You are {age} years old.']),
// function ($result, $error, Request $request) {
// /* special label to hangup */
// return '_hangup';
// });

// /* This label is using the $age variable store above */
// $flow->define('say_age3',
// new Command\Play('TTS|TTS_EN-GB_SERENA|You are {age} years old.'),
// function ($result, $error, Request $request) {
// /* special label to hangup */
// return '_hangup';
// });

/* This label is using the $age variable store above */
$flow->define('say_age', 'play', ['media_id' => 'TTS|TTS_EN-GB_SERENA|You are {age} years old.'], function($result, $error, Request $request) {
/* special label to hangup */
return '_hangup';
});
$flow->define(
'say_age4',
Command::Play('TTS|TTS_EN-GB_SERENA|You are {age} years old.'),
function ($result, $error, Request $request) {
/* special label to hangup */
return '_hangup';
}
);

/* Real-time Server */
$server = new Server;

/* Register a callback to receive raw input. Useful for debugging. */
$server->setRawInputHandler(function($data) {
$server->setRawInputHandler(function ($data) {
$data = date('c').' <<<< '.$data."\n";
file_put_contents('/tmp/RT_DEBUG', $data, FILE_APPEND);
});

/* Register a callback to receive raw output. Useful for debugging. */
$server->setRawOutputHandler(function($data) {
$server->setRawOutputHandler(function ($data) {
$data = date('c').' >>>> '.$data."\n";
file_put_contents('/tmp/RT_DEBUG', $data, FILE_APPEND);
});
Expand Down
152 changes: 79 additions & 73 deletions src/THECALLR/API/Client.php
Expand Up @@ -6,81 +6,87 @@
* JSON-RPC 2.0 Client
* @author Florent CHAUVEAU <fc@thecallr.com>
*/
class Client {
private $_auth;
private $_url = "https://api.thecallr.com";
private $_headers = [];
class Client
{
private $auth;
private $url = "https://api.thecallr.com";
private $headers = [];

/**
* Change API endpoint.
* @param string $url API endpoint
* @return void
*/
public function setURL($url) {
$this->_url = $url;
}
/**
* Change API endpoint.
* @param string $url API endpoint
* @return void
*/
public function setURL($url)
{
$this->url = $url;
}

/**
* Set API credentials (username, password).
* @param string $username Username
* @param string $password Password
* @return void
*/
public function setAuthCredentials($username, $password) {
$this->_auth = "{$username}:{$password}";
}
/**
* Set API credentials (username, password).
* @param string $username Username
* @param string $password Password
* @return void
*/
public function setAuthCredentials($username, $password)
{
$this->auth = "{$username}:{$password}";
}

/**
* Set API token.
* @param string $token API token
* @return void
*/
public function setAuthToken($token) {
$this->_auth = "_token:{$token}";
}
/**
* Set API token.
* @param string $token API token
* @return void
*/
public function setAuthToken($token)
{
$this->auth = "_token:{$token}";
}

/**
* Set customer HTTP headers.
* @param array $headers HTTP headers (key/value)
* @return void
*/
public function setCustomHeaders(array $headers) {
foreach ($headers as $k => &$v) {
if (strpos($v, ':') === false) {
$v = $k.': '.$v;
}
}
$this->_headers = $headers;
}
/**
* Set customer HTTP headers.
* @param array $headers HTTP headers (key/value)
* @return void
*/
public function setCustomHeaders(array $headers)
{
foreach ($headers as $k => &$v) {
if (strpos($v, ':') === false) {
$v = $k.': '.$v;
}
}
$this->headers = $headers;
}

/**
* @param string $method JSON-RPC method
* @param array $params JSON-RPC parameters
* @return mixed API response
* @throws \THECALLR\API\Exception\LocalException
* @throws \THECALLR\API\Exception\RemoteException
*/
public function call($method, array $params = [], $id = null) {
if (!is_string($method)) {
throw new Exception\LocalException('METHOD_TYPE_ERROR');
}
if (!is_array($params)) {
throw new Exception\LocalException('PARAMS_TYPE_ERROR');
}
if ($id === null) {
$id = (int) mt_rand(1, 1024);
}
if (!is_int($id)) {
throw new Exception\LocalException('ID_TYPE_ERROR');
}
$request = new Request;
$request->id = $id;
$request->method = $method;
$request->params = $params;
$response = $request->send($this->_url, $this->_auth, $this->_headers);
if ($response->isError()) {
throw $response->error;
}
return $response->result;
}
}
/**
* @param string $method JSON-RPC method
* @param array $params JSON-RPC parameters
* @return mixed API response
* @throws \THECALLR\API\Exception\LocalException
* @throws \THECALLR\API\Exception\RemoteException
*/
public function call($method, array $params = [], $id = null)
{
if (!is_string($method)) {
throw new Exception\LocalException('METHOD_TYPE_ERROR');
}
if (!is_array($params)) {
throw new Exception\LocalException('PARAMS_TYPE_ERROR');
}
if ($id === null) {
$id = (int) mt_rand(1, 1024);
}
if (!is_int($id)) {
throw new Exception\LocalException('ID_TYPE_ERROR');
}
$request = new Request;
$request->id = $id;
$request->method = $method;
$request->params = $params;
$response = $request->send($this->url, $this->auth, $this->headers);
if ($response->isError()) {
throw $response->error;
}
return $response->result;
}
}
4 changes: 3 additions & 1 deletion src/THECALLR/API/Exception/LocalException.php
Expand Up @@ -2,4 +2,6 @@

namespace THECALLR\API\Exception;

class LocalException extends \Exception {}
class LocalException extends \Exception
{
}
12 changes: 7 additions & 5 deletions src/THECALLR/API/Exception/RemoteException.php
Expand Up @@ -2,8 +2,10 @@

namespace THECALLR\API\Exception;

class RemoteException extends \Exception {
function __construct($data) {
parent::__construct($data->message, $data->code);
}
}
class RemoteException extends \Exception
{
public function __construct($data)
{
parent::__construct($data->message, $data->code);
}
}

0 comments on commit 1067553

Please sign in to comment.