Skip to content

Commit

Permalink
Refactor and lookup added
Browse files Browse the repository at this point in the history
  • Loading branch information
markitosgv committed Jun 24, 2016
1 parent 4a2f99a commit 78af72c
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 167 deletions.
21 changes: 21 additions & 0 deletions examples/Account/check-balance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

// Used for composer based installation
require __DIR__ . '/../../vendor/autoload.php';

// Use below for direct download installation
//require_once(__DIR__ . '/../../src/Instasent/Abstracts/InstasentClient.php');
//require_once(__DIR__ . '/../../src/Instasent/AccountClient.php');

$instasentClient = new Instasent\AccountClient("my-token");

$response = $instasentClient->getAccountBalance();

//echo $response["response_code"];
//echo $response["response_body"];

$response_body = json_decode($response["response_body"]);
$available = $response_body->entity->available;
$currency = $response_body->entity->currency;

echo sprintf("You have a balance of %s %s ", $available, $currency);
14 changes: 14 additions & 0 deletions examples/Lookup/do-lookup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

// Used for composer based installation
require __DIR__ . '/../../vendor/autoload.php';

// Use below for direct download installation
//require_once(__DIR__ . '/../../src/Instasent/Abstracts/InstasentClient.php');
//require_once(__DIR__ . '/../../src/Instasent/LookupClient.php');

$instasentClient = new Instasent\LookupClient("my-token");
$response = $instasentClient->doLookup("+34666000000");

echo $response["response_code"];
echo $response["response_body"];
1 change: 1 addition & 0 deletions examples/Sms/send-sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require __DIR__ . '/../../vendor/autoload.php';

// Use below for direct download installation
//require_once(__DIR__ . '/../../src/Instasent/Abstracts/InstasentClient.php');
//require_once(__DIR__ . '/../../src/Instasent/SmsClient.php');

$instasentClient = new Instasent\SmsClient("my-token");
Expand Down
1 change: 1 addition & 0 deletions examples/Sms/sms-info.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require __DIR__ . '/../../vendor/autoload.php';

// Use below for direct download installation
//require_once(__DIR__ . '/../../src/Instasent/Abstracts/InstasentClient.php');
//require_once(__DIR__ . '/../../src/Instasent/SmsClient.php');

$instasentClient = new Instasent\SmsClient("my-token");
Expand Down
1 change: 1 addition & 0 deletions examples/Verify/check-verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require __DIR__ . '/../../vendor/autoload.php';

// Use below for direct download installation
//require_once(__DIR__ . '/../../src/Instasent/Abstracts/InstasentClient.php');
//require_once(__DIR__ . '/../../src/Instasent/VerifyClient.php');

$instasentClient = new Instasent\VerifyClient("my-token");
Expand Down
1 change: 1 addition & 0 deletions examples/Verify/request-verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require __DIR__ . '/../../vendor/autoload.php';

// Use below for direct download installation
//require_once(__DIR__ . '/../../src/Instasent/Abstracts/InstasentClient.php');
//require_once(__DIR__ . '/../../src/Instasent/VerifyClient.php');

$instasentClient = new Instasent\VerifyClient("my-token");
Expand Down
3 changes: 2 additions & 1 deletion examples/Verify/verify-info.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
require __DIR__ . '/../../vendor/autoload.php';

// Use below for direct download installation
//require_once(__DIR__ . '/../../src/Instasent/SmsClient.php');
//require_once(__DIR__ . '/../../src/Instasent/Abstracts/InstasentClient.php');
//require_once(__DIR__ . '/../../src/Instasent/VerifyClient.php');

$instasentClient = new Instasent\VerifyClient("my-token");
$response = $instasentClient->getVerifyById("verifyId");
Expand Down
88 changes: 88 additions & 0 deletions src/Instasent/Abstracts/InstasentClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/*
* This file is part of the Instasent PHP Library.
*
* (c) Instasent <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Instasent\Abstracts;

abstract class InstasentClient
{
/**
* Endpoint URL
*
* @var string
*/
protected $rootEndpoint = 'http://api.instasent.com';

/**
* Secure Channel URL
*
* @var string
*/
protected $secureChannel = 'https://api.instasent.com';

/**
* Api Token
*
* @var string
*/
protected $token;

/**
* Use secure channel flag
*
* @var boolean
*/
protected $useSecureChannel = true;

/**
* InstasentClient constructor.
*
* @param $token
* @param bool $useSecureChannel
*/
public function __construct($token, $useSecureChannel = true)
{
$this->token = $token;
$this->useSecureChannel = $useSecureChannel;
}

/**
* Execute the request using curl
*
* @param string $url
* @param string $httpMethod
* @param string $data
*
* @return array
*/
protected function execRequest($url, $httpMethod, $data)
{
$curl = curl_init();
$headers = array(
'Authorization: Bearer '.$this->token,
);

curl_setopt($curl,CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

if ($httpMethod == 'POST') {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
}
$body = curl_exec($curl);
$info = curl_getinfo($curl);

return array(
"response_code" => $info['http_code'],
"response_body" => $body,
);
}
}
31 changes: 31 additions & 0 deletions src/Instasent/AccountClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Instasent PHP Library.
*
* (c) Instasent <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Instasent;

use Instasent\Abstracts\InstasentClient;

class AccountClient extends InstasentClient
{

/**
* Get Account Balance
*
* @return array
*/
public function getAccountBalance()
{
$url = ($this->useSecureChannel) ? $this->secureChannel.'/organization/account/' : $this->rootEndpoint.'/organization/account/';
$httpMethod = 'GET';
return $this->execRequest($url, $httpMethod, array());
}

}
61 changes: 61 additions & 0 deletions src/Instasent/LookupClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the Instasent PHP Library.
*
* (c) Instasent <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Instasent;

use Instasent\Abstracts\InstasentClient;

class LookupClient extends InstasentClient
{

/**
* Do a lookup to get number info
* @param string $to Recipient where lookup is requested
*
* @return array
*/
public function doLookup($to)
{
$url = ($this->useSecureChannel) ? $this->secureChannel.'/lookup/' : $this->rootEndpoint.'/lookup/';
$httpMethod = 'POST';
$data = array('to' => $to);

return $this->execRequest($url, $httpMethod, $data);
}

/**
* Get a lookup by id
* @param string $id
*
* @return array
*/
public function getLookupById($id)
{
$url = ($this->useSecureChannel) ? $this->secureChannel.'/lookup/'.$id : $this->rootEndpoint.'/lookup/'.$id;
$httpMethod = 'GET';
return $this->execRequest($url, $httpMethod, array());
}

/**
* Get all lookups. Filter by page and resultes per page.
* @param integer $page
* @param integer $perPage
*
* @return array
*/
public function getLookups($page = 1, $perPage = 10)
{
$query = http_build_query(array('page' => $page, 'per_page' => $perPage));
$url = ($this->useSecureChannel) ? $this->secureChannel.'/lookup/?'.$query : $this->rootEndpoint.'/lookup/?'.$query;
$httpMethod = 'GET';
return $this->execRequest($url, $httpMethod, array());
}
}
97 changes: 17 additions & 80 deletions src/Instasent/SmsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,19 @@

namespace Instasent;

class SmsClient
{
/**
* [$rootEndpoint description]
* @var string
*/
private $rootEndpoint = 'http://api.instasent.com';
use Instasent\Abstracts\InstasentClient;

/**
* [$secureCahnnel description]
* @var string
*/
private $secureChannel = 'https://api.instasent.com';

/**
* [$token description]
* @var string
*/
private $token;

/**
* [$useSecureChannel description]
* @var boolean
*/
private $useSecureChannel = true;

/**
* [__construct description]
* @param [type] $environment [description]
*/
public function __construct($token, $useSecureChannel = true)
{
$this->token = $token;
$this->useSecureChannel = $useSecureChannel;
}
class SmsClient extends InstasentClient
{

/**
* [sendSms Send a sms.]
* @param [string] $from [Remittent, 11chars max]
* @param [string] $to [Recipient where SMS is delivered, Include the country phone prefix format E164]
* @param [string] $text [Message text content, 160 chars per SMS]
* @param [string] $clientId [An user reference for your internal use, Optional - 40chars max, Unique per SMS]
* Send a sms
* @param string $from Remittent, 11chars max
* @param string $to Recipient where SMS is delivered, Include the country phone prefix format E164
* @param string $text Message text content, 160 chars per SMS
* @param string $clientId An user reference for your internal use, Optional - 40chars max, Unique per SMS
*
* @return array [description]
* @return array
*/
public function sendSms($from, $to, $text, $clientId = null)
{
Expand All @@ -68,10 +37,10 @@ public function sendSms($from, $to, $text, $clientId = null)
}

/**
* [getSms Get Sms by entity Id.]
* @param [type] $id [description]
* Get Sms by entity Id
* @param string $id
*
* @return array [description]
* @return array
*/
public function getSmsById($id)
{
Expand All @@ -81,11 +50,11 @@ public function getSmsById($id)
}

/**
* [getSms Get all sms. Filter by page and resultes per page.]
* @param integer $page [description]
* @param integer $perPage [description]
* @return array [description]
* Get all sms. Filter by page and resultes per page.
* @param integer $page
* @param integer $perPage
*
* @return array
*/
public function getSms($page = 1, $perPage = 10)
{
Expand All @@ -94,36 +63,4 @@ public function getSms($page = 1, $perPage = 10)
$httpMethod = 'GET';
return $this->execRequest($url, $httpMethod, array());
}

/**
* [execRequest Execute the request using curl]
* @param [type] $url [description]
* @param [type] $httpMethod [description]
* @param [type] $data [description]
*
* @return array [description]
*/
private function execRequest($url, $httpMethod, $data)
{
$curl = curl_init();
$headers = array(
'Authorization: Bearer '.$this->token,
);

curl_setopt($curl,CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

if ($httpMethod == 'POST') {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
}
$body = curl_exec($curl);
$info = curl_getinfo($curl);

return array(
"response_code" => $info['http_code'],
"response_body" => $body,
);
}
}
Loading

0 comments on commit 78af72c

Please sign in to comment.