Skip to content

Commit dc088cf

Browse files
committed
Added sms service, and alphanumeric senders. Tests back green again.
1 parent 14c24e5 commit dc088cf

12 files changed

+275
-54
lines changed

src/Exceptions/CouldNotSendNotification.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,11 @@ public static function invalidReceiver()
3939
method or a phone_number attribute to your notifiable.'
4040
);
4141
}
42+
43+
public static function missingAlphaNumericSender()
44+
{
45+
return new static(
46+
'Notification was not sent. Missing `alphanumeric_sender` in config'
47+
);
48+
}
4249
}

src/Twilio.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,38 @@ class Twilio
1313
protected $twilioService;
1414

1515
/**
16-
* Default 'from' from config.
17-
* @var string
16+
* @var TwilioConfig
1817
*/
19-
protected $from;
18+
private $config;
2019

2120
/**
2221
* Twilio constructor.
2322
*
24-
* @param TwilioService $twilioService
25-
* @param string $from
23+
* @param TwilioService $twilioService
24+
* @param TwilioConfig $config
2625
*/
27-
public function __construct(TwilioService $twilioService, $from)
26+
public function __construct(TwilioService $twilioService, TwilioConfig $config)
2827
{
2928
$this->twilioService = $twilioService;
30-
$this->from = $from;
29+
$this->config = $config;
3130
}
3231

3332
/**
3433
* Send a TwilioMessage to the a phone number.
3534
*
36-
* @param TwilioMessage $message
37-
* @param $to
35+
* @param TwilioMessage $message
36+
* @param $to
37+
* @param bool $useAlphanumericSender
3838
* @return mixed
3939
* @throws CouldNotSendNotification
4040
*/
41-
public function sendMessage(TwilioMessage $message, $to)
41+
public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender = false)
4242
{
4343
if ($message instanceof TwilioSmsMessage) {
44+
if ($useAlphanumericSender && $sender = $this->getAlphanumericSender()) {
45+
$message->from($sender);
46+
}
47+
4448
return $this->sendSmsMessage($message, $to);
4549
}
4650

@@ -56,7 +60,9 @@ protected function sendSmsMessage($message, $to)
5660
return $this->twilioService->account->messages->sendMessage(
5761
$this->getFrom($message),
5862
$to,
59-
trim($message->content)
63+
trim($message->content),
64+
null,
65+
$this->config->getSmsParams()
6066
);
6167
}
6268

@@ -71,10 +77,19 @@ protected function makeCall($message, $to)
7177

7278
protected function getFrom($message)
7379
{
74-
if (! $from = $message->from ?: $this->from) {
80+
if (! $from = $message->getFrom() ?: $this->config->getFrom()) {
7581
throw CouldNotSendNotification::missingFrom();
7682
}
7783

7884
return $from;
7985
}
86+
87+
protected function getAlphanumericSender()
88+
{
89+
if ($sender = $this->config->getAlphanumericSender()) {
90+
return $sender;
91+
}
92+
93+
return null;
94+
}
8095
}

src/TwilioChannel.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function send($notifiable, Notification $notification)
4545
try {
4646
$to = $this->getTo($notifiable);
4747
$message = $notification->toTwilio($notifiable);
48+
$useSender = $this->canReceiveAlphanumericSender($notifiable);
4849

4950
if (is_string($message)) {
5051
$message = new TwilioSmsMessage($message);
@@ -54,7 +55,7 @@ public function send($notifiable, Notification $notification)
5455
throw CouldNotSendNotification::invalidMessageObject($message);
5556
}
5657

57-
return $this->twilio->sendMessage($message, $to);
58+
return $this->twilio->sendMessage($message, $to, $useSender);
5859
} catch (Exception $exception) {
5960
$this->events->fire(
6061
new NotificationFailed($notifiable, $notification, 'twilio', ['message' => $exception->getMessage()])
@@ -73,4 +74,16 @@ protected function getTo($notifiable)
7374

7475
throw CouldNotSendNotification::invalidReceiver();
7576
}
77+
78+
/**
79+
* Get the alphanumeric sender
80+
* @param $notifiable
81+
* @return mixed|null
82+
* @throws CouldNotSendNotification
83+
*/
84+
protected function canReceiveAlphanumericSender($notifiable)
85+
{
86+
return (method_exists($notifiable, 'canReceiveAlphanumericSender') &&
87+
$notifiable->canReceiveAlphanumericSender());
88+
}
7689
}

src/TwilioConfig.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
namespace NotificationChannels\Twilio;
3+
4+
class TwilioConfig
5+
{
6+
/**
7+
* @var array
8+
*/
9+
private $config;
10+
11+
/**
12+
* TwilioConfig constructor.
13+
*
14+
* @param array $config
15+
*/
16+
public function __construct(array $config)
17+
{
18+
$this->config = $config;
19+
}
20+
21+
public function getAccountSid()
22+
{
23+
return $this->config['account_sid'];
24+
}
25+
26+
public function getAuthToken()
27+
{
28+
return $this->config['auth_token'];
29+
}
30+
31+
/**
32+
* Get the from entity from config
33+
*/
34+
public function getFrom()
35+
{
36+
return $this->config['from'];
37+
}
38+
39+
public function getAlphanumericSender()
40+
{
41+
if (isset($this->config['alphanumeric_sender'])) {
42+
return $this->config['alphanumeric_sender'];
43+
}
44+
45+
return null;
46+
}
47+
48+
public function getSmsParams()
49+
{
50+
$params = [];
51+
52+
if (isset($this->config['sms_service_sid'])) {
53+
$params['MessagingServiceSid'] = $this->config['sms_service_sid'];
54+
}
55+
56+
return $params;
57+
}
58+
}

src/TwilioMessage.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,13 @@ public function from($from)
6565

6666
return $this;
6767
}
68+
69+
/**
70+
* Get the from address
71+
* @return string
72+
*/
73+
public function getFrom()
74+
{
75+
return $this->from;
76+
}
6877
}

src/TwilioProvider.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@ class TwilioProvider extends ServiceProvider
1212
*/
1313
public function boot()
1414
{
15+
$config = $this->app->make(TwilioConfig::class, $this->app['config']['services.twilio']);
16+
$twilio = $this->app->make(TwilioService::class, [
17+
$config->getAccountSid(),
18+
$config->getAuthToken(),
19+
]);
20+
1521
$this->app->when(TwilioChannel::class)
1622
->needs(Twilio::class)
17-
->give(function () {
18-
$config = $this->app['config']['services.twilio'];
19-
20-
return new Twilio(
21-
$this->app->make(TwilioService::class, [
22-
$config['account_sid'],
23-
$config['auth_token'],
24-
]),
25-
$config['from']
26-
);
23+
->give(function () use ($twilio, $config) {
24+
return new Twilio($twilio, $config);
2725
});
2826
}
2927

src/TwilioSmsMessage.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,34 @@
44

55
class TwilioSmsMessage extends TwilioMessage
66
{
7+
/**
8+
* @var null|string
9+
*/
10+
public $alphaNumSender = null;
11+
12+
/**
13+
* Get the from address of this message
14+
* @return null|string
15+
*/
16+
public function getFrom()
17+
{
18+
if ($this->from) {
19+
return $this->from;
20+
}
21+
22+
if ($this->alphaNumSender && strlen($this->alphaNumSender) > 0) {
23+
return $this->alphaNumSender;
24+
}
25+
26+
return null;
27+
}
28+
29+
/**
30+
* Set the alphanumeric sender
31+
* @param $sender
32+
*/
33+
public function sender($sender)
34+
{
35+
$this->alphaNumSender = $sender;
36+
}
737
}

tests/IntegrationTest.php

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Mockery;
88
use NotificationChannels\Twilio\TwilioCallMessage;
99
use NotificationChannels\Twilio\TwilioChannel;
10+
use NotificationChannels\Twilio\TwilioConfig;
1011
use NotificationChannels\Twilio\TwilioSmsMessage;
1112
use PHPUnit_Framework_TestCase;
1213
use NotificationChannels\Twilio\Twilio;
@@ -44,43 +45,85 @@ public function it_can_send_a_sms_message()
4445
$message = TwilioSmsMessage::create('Message text');
4546
$this->notification->shouldReceive('toTwilio')->andReturn($message);
4647

47-
$twilio = new Twilio($this->twilioService, '+31612345678');
48+
$config = new TwilioConfig([
49+
'from' => '+31612345678'
50+
]);
51+
$twilio = new Twilio($this->twilioService, $config);
52+
$channel = new TwilioChannel($twilio, $this->events);
53+
54+
$this->smsMessageWillBeSentToTwilioWith('+31612345678', '+22222222222', 'Message text', null, []);
55+
56+
$channel->send(new NotifiableWithAttribute(), $this->notification);
57+
}
4858

59+
/** @test */
60+
public function it_can_send_a_sms_message_using_service()
61+
{
62+
$message = TwilioSmsMessage::create('Message text');
63+
$this->notification->shouldReceive('toTwilio')->andReturn($message);
64+
65+
$config = new TwilioConfig([
66+
'from' => '+31612345678',
67+
'sms_service_sid' => '0123456789'
68+
]);
69+
$twilio = new Twilio($this->twilioService, $config);
4970
$channel = new TwilioChannel($twilio, $this->events);
5071

51-
$this->smsMessageWillBeSentToTwilioWith('+31612345678', '+22222222222', 'Message text');
72+
$this->smsMessageWillBeSentToTwilioWith('+31612345678', '+22222222222', 'Message text', null, [
73+
'MessagingServiceSid' => '0123456789'
74+
]);
5275

5376
$channel->send(new NotifiableWithAttribute(), $this->notification);
5477
}
5578

79+
/** @test */
80+
public function it_can_send_a_sms_message_using_alphanumeric_sender()
81+
{
82+
$message = TwilioSmsMessage::create('Message text');
83+
$this->notification->shouldReceive('toTwilio')->andReturn($message);
84+
85+
$config = new TwilioConfig([
86+
'from' => '+31612345678',
87+
'alphanumeric_sender' => 'TwilioTest'
88+
]);
89+
$twilio = new Twilio($this->twilioService, $config);
90+
$channel = new TwilioChannel($twilio, $this->events);
91+
92+
$this->smsMessageWillBeSentToTwilioWith('TwilioTest', '+33333333333', 'Message text', null, []);
93+
94+
$channel->send(new NotifiableWithAlphanumericSender(), $this->notification);
95+
}
96+
5697
/** @test */
5798
public function it_can_make_a_call()
5899
{
59100
$message = TwilioCallMessage::create('http://example.com');
60101
$this->notification->shouldReceive('toTwilio')->andReturn($message);
61102

62-
$twilio = new Twilio($this->twilioService, '+31612345678');
63-
103+
$config = new TwilioConfig([
104+
'from' => '+31612345678'
105+
]);
106+
$twilio = new Twilio($this->twilioService, $config);
64107
$channel = new TwilioChannel($twilio, $this->events);
65108

66109
$this->callWillBeSentToTwilioWith('+31612345678', '+22222222222', 'http://example.com');
67110

68111
$channel->send(new NotifiableWithAttribute(), $this->notification);
69112
}
70113

71-
protected function smsMessageWillBeSentToTwilioWith($from, $to, $message)
114+
protected function smsMessageWillBeSentToTwilioWith(...$args)
72115
{
73116
$this->twilioService->account->messages->shouldReceive('sendMessage')
74117
->atLeast()->once()
75-
->with($from, $to, $message)
118+
->with(...$args)
76119
->andReturn(true);
77120
}
78121

79-
protected function callWillBeSentToTwilioWith($from, $to, $url)
122+
protected function callWillBeSentToTwilioWith(...$args)
80123
{
81124
$this->twilioService->account->calls->shouldReceive('create')
82125
->atLeast()->once()
83-
->with($from, $to, $url)
126+
->with(...$args)
84127
->andReturn(true);
85128
}
86129
}

0 commit comments

Comments
 (0)