Skip to content
This repository has been archived by the owner on Feb 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from midnite81/develop
Browse files Browse the repository at this point in the history
Update to readme and services
  • Loading branch information
midnite81 committed Apr 18, 2016
2 parents a46736a + 7ac2e38 commit bf66008
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 12 deletions.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,67 @@
# Plivo
A Plivo SMS integration for Laravel

#Installation

This package requires PHP 5.6+, and includes a Laravel 5 Service Provider and Facade.

To install through composer include the package in your `composer.json`.

"midnite81/plivo": "0.0.*"

Run `composer install` or `composer update` to download the dependencies or you can run `composer require midnite81/plivo`.

##Laravel 5 Integration

To use the package with Laravel 5 firstly add the Messaging service provider to the list of service providers
in `app/config/app.php`.

'providers' => [

Midnite81\Plivo\MessengingServiceProvider::class
];

Add the `Messaging` facade to your aliases array.

'aliases' => [

'Messaging' => 'Midnite81\Plivo\Facades\Messaging',
];

Publish the config and migration files using
`php artisan vendor:publish --provider="Midnite81\Plivo\MessagingServiceProvider"`

To access Plivo/Messaging you can either use the Facade or the Messaging instance is bound to the IOC container and you can
then dependency inject it via its contract.


Messaging::get('foo');

public function __construct(Midnite81\LaravelTwoStep\Contracts\Messaging $messaging)
{
$this->messaging = $messaging;
}

#Configuration File

Once you have published the config files, you will find a `Plivo.php` file in the `config` folder. You should
look through these settings and update these where necessary.

# Env

You will need to add the following to your `.env` file and update these with your own settings

PLIVO_AUTH_ID=<auth_id>
PLIVO_AUTH_TOKEN=<auth_token>
PLIVO_SOURCE_NUMBER=<default_sms_number>

# Example Usage

use Midnite81\Plivo\Contracts\Services\Messaging;

public function sendMessage(Messaging $messaging)
{
$msg = $messaging->msg('Hello World!')->to('0123456789')->sendMessage();
}
99 changes: 99 additions & 0 deletions src/Contracts/Services/Messaging.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,107 @@

namespace Midnite81\Plivo\Contracts\Services;

use Midnite81\Plivo\Exceptions\DestinationSMSNumberIsEmptyException;
use Midnite81\Plivo\Exceptions\MessageIsEmptyException;
use Midnite81\Plivo\Exceptions\SourceSMSNumberIsEmptyException;
use Midnite81\Plivo\Services\Plivo;

interface Messaging
{

public function __construct(Plivo $plivo);

/**
* Sends SMS message
*
* @return array
* @throws DestinationSMSNumberIsEmptyException
* @throws MessageIsEmptyException
* @throws SourceSMSNumberIsEmptyException
*/
public function sendMessage();

/**
* Alias for sendMessage
*
* @return array
* @throws DestinationSMSNumberIsEmptyException
* @throws MessageIsEmptyException
* @throws SourceSMSNumberIsEmptyException
*/
public function send();

/**
* Return Formatted Message Data
*
* @return array
*/
public function getMessageData();

/**
* Get Message details
*/
public function getMessages();

/**
* Get Specific Message Details
*
* @param $uuid
* @return array
*/
public function getMessage($uuid);

/**
* Message Setter
*
* @param string $message
* @return Messaging
*/
public function setMessage($message);

/**
* Alias for setMessage
*
* @param $msg
* @return Messaging
*/
public function msg($msg);

/**
* Source Number Setter
*
* @param mixed $sourceNumber
* @return Messaging
*/
public function setSourceNumber($sourceNumber);

/**
* Alias of setSourceNumber
*
* @param $from
* @return Messaging
*/
public function from($from);

/**
* Destination Number(s) Setter
*
* @param array|string $destinationNumber
* @return Messaging
*/
public function setDestinationNumber($destinationNumber);

/**
* Alias for setDestinationNumber
*
* @param $to
* @return Messaging
*/
public function to($to);

/**
* Get destination sms number(s)
*/
public function getDestinationNumber();

}
14 changes: 14 additions & 0 deletions src/Facades/Messaging.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Midnite81\Plivo\Facades;

use Illuminate\Support\Facades\Facade;

class Messaging extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'midnite81.plivo'; }
}
100 changes: 88 additions & 12 deletions src/Services/Messaging.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@ class Messaging implements MessagingContract

/**
* Messaging constructor.
* @param RestAPI $plivo
*
* @param Plivo|RestAPI $plivo
*/
public function __construct(Plivo $plivo)
{
// $this->plivo = new RestAPI(env('PLIVO_AUTH_ID'), env('PLIVO_AUTH_TOKEN'));
$this->plivo = $plivo;
$this->destinationNumber = [];
}

/**
* Send SMS message
* Sends SMS message
*
* @return array
* @throws \Exception
* @throws DestinationSMSNumberIsEmptyException
* @throws MessageIsEmptyException
* @throws SourceSMSNumberIsEmptyException
*/
public function sendMessage()
{
Expand All @@ -74,15 +78,38 @@ public function sendMessage()
throw new MessageIsEmptyException('The Message is empty');
}

$data = [
$data = $this->getMessageData();

return $this->plivo->send_message($data);

}

/**
* Alias for sendMessage
*
* @return array
* @throws DestinationSMSNumberIsEmptyException
* @throws MessageIsEmptyException
* @throws SourceSMSNumberIsEmptyException
*/
public function send()
{
return $this->sendMessage();
}

/**
* Return Formatted Message Data
*
* @return array
*/
public function getMessageData()
{
return [
'src' => $this->sourceNumber,
'dst' => $this->destinationNumber,
'dst' => $this->getDestinationNumber(),
'text' => $this->message,
'type' => $this->type
];

return $this->plivo->send_message($data);

}

/**
Expand All @@ -96,6 +123,7 @@ public function getMessages()

/**
* Get Specific Message Details
*
* @param $uuid
* @return array
*/
Expand All @@ -114,6 +142,7 @@ public function getMessage($uuid)

/**
* Message Setter
*
* @param string $message
* @return Messaging
*/
Expand All @@ -123,8 +152,20 @@ public function setMessage($message)
return $this;
}

/**
* Alias for setMessage
*
* @param $msg
* @return Messaging
*/
public function msg($msg)
{
return $this->setMessage($msg);
}

/**
* Source Number Setter
*
* @param mixed $sourceNumber
* @return Messaging
*/
Expand All @@ -135,17 +176,52 @@ public function setSourceNumber($sourceNumber)
}

/**
* Destination Number Setter
* Alias of setSourceNumber
*
* @param $from
* @return Messaging
*/
public function from($from)
{
return $this->setSourceNumber($from);
}

/**
* Destination Number(s) Setter
*
* @param array|string $destinationNumber
* @return Messaging
*/
public function setDestinationNumber($destinationNumber)
{
if (is_array($destinationNumber)) {
$destinationNumber = implode('<', $destinationNumber);
foreach($destinationNumber as $number) {
array_push($this->destinationNumber, $number);
}
} else {
array_push($this->destinationNumber, $destinationNumber);
}
$this->destinationNumber = $destinationNumber;

return $this;
}

/**
* Alias for setDestinationNumber
*
* @param $to
* @return Messaging
*/
public function to($to)
{
return $this->setDestinationNumber($to);
}

/**
* Get destination sms number(s)
*/
public function getDestinationNumber()
{
return implode('<', $this->destinationNumber);
}

}

0 comments on commit bf66008

Please sign in to comment.