Skip to content

Commit

Permalink
Merge branch 'lnpbk-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
gregoriohc committed Mar 7, 2017
2 parents f751118 + 7506068 commit 0894f36
Show file tree
Hide file tree
Showing 15 changed files with 462 additions and 93 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"require": {
"php": ">=5.5.9",
"twilio/sdk": "^4.11",
"twilio/sdk": "^5.4.1",
"illuminate/notifications": "5.1.*|5.2.*|5.3.*|5.4.*",
"illuminate/support": "5.1.*|5.2.*|5.3.*|5.4.*",
"illuminate/events": "5.1.*|5.2.*|5.3.*|5.4.*",
Expand Down
7 changes: 7 additions & 0 deletions src/Exceptions/CouldNotSendNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ public static function invalidReceiver()
method or a phone_number attribute to your notifiable.'
);
}

public static function missingAlphaNumericSender()
{
return new static(
'Notification was not sent. Missing `alphanumeric_sender` in config'
);
}
}
88 changes: 65 additions & 23 deletions src/Twilio.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace NotificationChannels\Twilio;

use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification;
use Services_Twilio as TwilioService;
use Twilio\Rest\Client as TwilioService;

class Twilio
{
Expand All @@ -13,34 +13,38 @@ class Twilio
protected $twilioService;

/**
* Default 'from' from config.
* @var string
* @var TwilioConfig
*/
protected $from;
private $config;

/**
* Twilio constructor.
*
* @param TwilioService $twilioService
* @param string $from
* @param TwilioService $twilioService
* @param TwilioConfig $config
*/
public function __construct(TwilioService $twilioService, $from)
public function __construct(TwilioService $twilioService, TwilioConfig $config)
{
$this->twilioService = $twilioService;
$this->from = $from;
$this->config = $config;
}

/**
* Send a TwilioMessage to the a phone number.
*
* @param TwilioMessage $message
* @param $to
* @param TwilioMessage $message
* @param string $to
* @param bool $useAlphanumericSender
* @return mixed
* @throws CouldNotSendNotification
*/
public function sendMessage(TwilioMessage $message, $to)
public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender = false)
{
if ($message instanceof TwilioSmsMessage) {
if ($useAlphanumericSender && $sender = $this->getAlphanumericSender()) {
$message->from($sender);
}

return $this->sendSmsMessage($message, $to);
}

Expand All @@ -51,30 +55,68 @@ public function sendMessage(TwilioMessage $message, $to)
throw CouldNotSendNotification::invalidMessageObject($message);
}

protected function sendSmsMessage($message, $to)
/**
* Send an sms message using the Twilio Service.
*
* @param TwilioSmsMessage $message
* @param string $to
* @return \Twilio\Rest\Api\V2010\Account\MessageInstance
*/
protected function sendSmsMessage(TwilioSmsMessage $message, $to)
{
return $this->twilioService->account->messages->sendMessage(
$this->getFrom($message),
$to,
trim($message->content)
);
$params = [
'from' => $this->getFrom($message),
'body' => trim($message->content),
];

if ($serviceSid = $this->config->getServiceSid()) {
$params['messagingServiceSid'] = $serviceSid;
}

return $this->twilioService->messages->create($to, $params);
}

protected function makeCall($message, $to)
/**
* Make a call using the Twilio Service.
*
* @param TwilioCallMessage $message
* @param string $to
* @return \Twilio\Rest\Api\V2010\Account\CallInstance
*/
protected function makeCall(TwilioCallMessage $message, $to)
{
return $this->twilioService->account->calls->create(
$this->getFrom($message),
return $this->twilioService->calls->create(
$to,
trim($message->content)
$this->getFrom($message),
['url' => trim($message->content)]
);
}

protected function getFrom($message)
/**
* Get the from address from message, or config.
*
* @param TwilioMessage $message
* @return string
* @throws CouldNotSendNotification
*/
protected function getFrom(TwilioMessage $message)
{
if (! $from = $message->from ?: $this->from) {
if (! $from = $message->getFrom() ?: $this->config->getFrom()) {
throw CouldNotSendNotification::missingFrom();
}

return $from;
}

/**
* Get the alphanumeric sender from config, if one exists.
*
* @return string|null
*/
protected function getAlphanumericSender()
{
if ($sender = $this->config->getAlphanumericSender()) {
return $sender;
}
}
}
3 changes: 1 addition & 2 deletions src/TwilioCallMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ class TwilioCallMessage extends TwilioMessage
/**
* Set the message url.
*
* @param string $url
*
* @param string $url
* @return $this
*/
public function url($url)
Expand Down
31 changes: 26 additions & 5 deletions src/TwilioChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class TwilioChannel
/**
* TwilioChannel constructor.
*
* @param Twilio $twilio
* @param Dispatcher $events
* @param Twilio $twilio
* @param Dispatcher $events
*/
public function __construct(Twilio $twilio, Dispatcher $events)
{
Expand All @@ -35,8 +35,8 @@ public function __construct(Twilio $twilio, Dispatcher $events)
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return mixed
* @throws CouldNotSendNotification
*/
Expand All @@ -45,6 +45,7 @@ public function send($notifiable, Notification $notification)
try {
$to = $this->getTo($notifiable);
$message = $notification->toTwilio($notifiable);
$useSender = $this->canReceiveAlphanumericSender($notifiable);

if (is_string($message)) {
$message = new TwilioSmsMessage($message);
Expand All @@ -54,14 +55,21 @@ public function send($notifiable, Notification $notification)
throw CouldNotSendNotification::invalidMessageObject($message);
}

return $this->twilio->sendMessage($message, $to);
return $this->twilio->sendMessage($message, $to, $useSender);
} catch (Exception $exception) {
$this->events->fire(
new NotificationFailed($notifiable, $notification, 'twilio', ['message' => $exception->getMessage()])
);
}
}

/**
* Get the address to send a notification to.
*
* @param mixed $notifiable
* @return mixed
* @throws CouldNotSendNotification
*/
protected function getTo($notifiable)
{
if ($notifiable->routeNotificationFor('twilio')) {
Expand All @@ -73,4 +81,17 @@ protected function getTo($notifiable)

throw CouldNotSendNotification::invalidReceiver();
}

/**
* Get the alphanumeric sender.
*
* @param $notifiable
* @return mixed|null
* @throws CouldNotSendNotification
*/
protected function canReceiveAlphanumericSender($notifiable)
{
return method_exists($notifiable, 'canReceiveAlphanumericSender') &&
$notifiable->canReceiveAlphanumericSender();
}
}
75 changes: 75 additions & 0 deletions src/TwilioConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace NotificationChannels\Twilio;

class TwilioConfig
{
/**
* @var array
*/
private $config;

/**
* TwilioConfig constructor.
*
* @param array $config
*/
public function __construct(array $config)
{
$this->config = $config;
}

/**
* Get the account sid.
*
* @return string
*/
public function getAccountSid()
{
return $this->config['account_sid'];
}

/**
* Get the auth token.
*
* @return string
*/
public function getAuthToken()
{
return $this->config['auth_token'];
}

/**
* Get the default from address.
*
* @return string
*/
public function getFrom()
{
return $this->config['from'];
}

/**
* Get the alphanumeric sender.
*
* @return string
*/
public function getAlphanumericSender()
{
if (isset($this->config['alphanumeric_sender'])) {
return $this->config['alphanumeric_sender'];
}
}

/**
* Get the service sid.
*
* @return string
*/
public function getServiceSid()
{
if (isset($this->config['sms_service_sid'])) {
return $this->config['sms_service_sid'];
}
}
}
20 changes: 14 additions & 6 deletions src/TwilioMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ abstract class TwilioMessage
public $from;

/**
* Create a message object.
* @param string $content
*
* @return static
*/
public static function create($content = '')
Expand All @@ -31,7 +31,7 @@ public static function create($content = '')
/**
* Create a new message instance.
*
* @param string $content
* @param string $content
*/
public function __construct($content = '')
{
Expand All @@ -41,8 +41,7 @@ public function __construct($content = '')
/**
* Set the message content.
*
* @param string $content
*
* @param string $content
* @return $this
*/
public function content($content)
Expand All @@ -55,8 +54,7 @@ public function content($content)
/**
* Set the phone number the message should be sent from.
*
* @param string $from
*
* @param string $from
* @return $this
*/
public function from($from)
Expand All @@ -65,4 +63,14 @@ public function from($from)

return $this;
}

/**
* Get the from address.
*
* @return string
*/
public function getFrom()
{
return $this->from;
}
}
Loading

0 comments on commit 0894f36

Please sign in to comment.