Skip to content

Commit

Permalink
First upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianGiupponi committed Nov 1, 2014
0 parents commit 0864333
Show file tree
Hide file tree
Showing 15 changed files with 291 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
composer.phar
composer.lock
.DS_Store
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- hhvm

before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction --dev

script: phpunit
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "christian-giupponi/nexmo",
"description": "This is a Laravel4 package that helps you integrate Nexmo sms services into your site.",
"License": "MIT",
"authors": [
{
"name": "Christian Giupponi",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "4.2.*",
"guzzlehttp/guzzle": "~5.0"
},
"autoload": {
"classmap": [
"src/migrations"
],
"psr-0": {
"ChristianGiupponi\\Nexmo\\": "src/"
}
},
"minimum-stability": "stable"
}
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
Empty file added public/.gitkeep
Empty file.
14 changes: 14 additions & 0 deletions src/ChristianGiupponi/Nexmo/Facades/Nexmo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php namespace ChristianGiupponi\Nexmo\Facades;

use Illuminate\Support\Facades\Facade;

class Nexmo extends Facade {

/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'nexmo'; }

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

namespace ChristianGiupponi\Nexmo;

use GuzzleHttp\Client;

class Nexmo {

protected $apiKey;
protected $apiSecret;
public $nexmoUrl = "https://rest.nexmo.com/account/";
public $nexmoPostUrl = "https://rest.nexmo.com/sms/json";

/**
* getBalance
* this function will return the balance of you Nexmo account
*
* @return json
*/
public function getBalance()
{
//Set the base api url
$base_url = "get-balance/";

//Get result from api
return $this->get_request( $base_url );

}

/**
* pricing
* Get the price of a given country.
* It returns also the available carrier name with price.
*
* @param string $countryOrPrefix
* @return json
*/
public function pricing( $countryOrPrefix = '' )
{
//check if the request is fo a specific country or prefix
if( trim( $countryOrPrefix ) == '' )
{
return json_encode( [
'code' => '400',
'reason' => 'Code or Prefix required',
'body' => ''
] );
}

$base_url = 'get-pricing/outbound/';
$params = '/' . $countryOrPrefix;

return $this->get_request( $base_url, $params );

}

/**
* Send SMS
*
* This function will send a post request to Nexmo website to send a new sms
* To see all the options available please visite nexmo website:
* https://docs.nexmo.com/index.php/sms-api/send-message
* The option must be formed like: array( 'foo' => 'bar' )
*
* @param string $from
* @param string $to
* @param string $text
* @param array $options
* @return json
*/
public function sendSMS( $from = '', $to = '', $text = '', $options = [ ] )
{
if( trim( $from ) == '' || trim( $to ) == '' || trim( $text ) == '' )
{
return json_encode( [
'code' => '400',
'reason' => 'Missing parameter, Form - To and Text are required.',
'body' => ''
] );
}

$required = [
'from' => $from,
'to' => $to,
'text' => urlencode( $text )
];

return $this->post_request( $required, $options );

}

/**
* api_request
* This function will make the api call using GuzzleHttp
*
* @param $method
* @param $base_url
* @param string $params
* @return json
*/
public function get_request( $base_url, $params = '' )
{
$this->apiKey = \Config::get( 'nexmo::nexmo_api_key' );
$this->apiSecret = \Config::get( 'nexmo::nexmo_api_secret' );

//Create the url
$url = $this->nexmoUrl . $base_url . $this->apiKey . '/' . $this->apiSecret . $params;

//Initialize a new client for api request
$client = new Client();

//Send the request
$response = $client->get( $url, [
'headers' => [ 'Accept' => 'application/json' ]
] );

//Get the result
$code = $response->getStatusCode();
$reason = $response->getReasonPhrase();
$body = $response->json();

//Format the result
return json_encode( [
'code' => $code,
'reason' => $reason,
'body' => $body
] );
}

public function post_request( $requried = [ ], $optional = [ ] )
{
//Get api from config file
$this->apiKey = \Config::get( 'nexmo::nexmo_api_key' );
$this->apiSecret = \Config::get( 'nexmo::nexmo_api_secret' );

//Create uri params with the required file
$required_url = http_build_query( $requried );

//If there are any optional fields let's add them
$optional_url = ( count( $optional ) > 0 ) ? '&' . http_build_query( $optional ) : '';

//This is the final url
$url = $this->nexmoPostUrl . "?api_key=" . $this->apiKey . '&api_secret=' . $this->apiSecret . '&' . $required_url . $optional_url;

//Initialize a new client for api request
$client = new Client();

//Make POST request
$response = $client->post( $url );

//Get the result
$code = $response->getStatusCode();
$reason = $response->getReasonPhrase();
$body = $response->json();

//Format the result
return json_encode( [
'code' => $code,
'reason' => $reason,
'body' => $body
] );

}
}
47 changes: 47 additions & 0 deletions src/ChristianGiupponi/Nexmo/NexmoServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php namespace ChristianGiupponi\Nexmo;

use Illuminate\Support\ServiceProvider;

class NexmoServiceProvider extends ServiceProvider {

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->package('christian-giupponi/nexmo');
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app['nexmo'] = $this->app->share(function($app)
{
return new Nexmo;
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array();
}

}
Empty file added src/config/.gitkeep
Empty file.
6 changes: 6 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'nexmo_api_key' => '',
'nexmo_api_secret' => ''
];
Empty file added src/controllers/.gitkeep
Empty file.
Empty file added src/lang/.gitkeep
Empty file.
Empty file added src/migrations/.gitkeep
Empty file.
Empty file added src/views/.gitkeep
Empty file.
Empty file added tests/.gitkeep
Empty file.

0 comments on commit 0864333

Please sign in to comment.