Skip to content

Commit cd8d7e1

Browse files
committed
Some refactoring and improvements
1 parent 23ccfec commit cd8d7e1

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed

src/Exceptions/CouldNotSendNotification.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ public static function serviceRespondedWithAnException($exception)
2424
*/
2525
public static function invalidMessageObject($class)
2626
{
27-
return new static("Message object class `{$class}` is invalid. It should be either `".SmsMessage::class.'` or `'.CallMessage::class.'`');
27+
return new static("Notification was not sent. Message object class `{$class}` is invalid. It should be either `".SmsMessage::class.'` or `'.CallMessage::class.'`');
28+
}
29+
30+
/**
31+
* @return static
32+
*/
33+
public static function missingFrom()
34+
{
35+
return new static("Notification was not sent. Missing `from` number.");
2836
}
2937
}

src/CallMessage.php renamed to src/TwilioCallMessage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace NotificationChannels\Twilio;
44

5-
class CallMessage
5+
class TwilioCallMessage
66
{
77
/**
88
* The message TwiML url.

src/Channel.php renamed to src/TwilioChannel.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification;
99
use Services_Twilio;
1010

11-
class Channel
11+
class TwilioChannel
1212
{
1313
/**
1414
* @var \Services_Twilio
@@ -34,22 +34,25 @@ public function send($notifiable, Notification $notification)
3434
return;
3535
}
3636

37-
$config = config('services.twilio');
38-
3937
$message = $notification->toTwilio($notifiable);
4038

4139
if (is_string($message)) {
42-
$message = new SmsMessage($message);
40+
// Default to SMS message if only a string is provided
41+
$message = new TwilioSmsMessage($message);
4342
}
4443

45-
if (!$message instanceof SmsMessage::class &&
46-
!$message instanceof CallMessage::class
44+
if (!$message instanceof TwilioSmsMessage::class &&
45+
!$message instanceof TwilioCallMessage::class
4746
) {
4847
$class = get_class($message) ?: 'Unknown';
4948

5049
throw CouldNotSendNotification::invalidMessageObject($class);
5150
}
5251

52+
if (!$from = $message->from ?: config('services.twilio.from')) {
53+
throw CouldNotSendNotification::missingFrom();
54+
}
55+
5356
$shouldSendMessage = event(new SendingMessage($notifiable, $notification, $message), [], true) !== false;
5457

5558
if (!$shouldSendMessage) {
@@ -58,17 +61,17 @@ public function send($notifiable, Notification $notification)
5861

5962
$response = null;
6063

61-
/** @var SmsMessage|CallMessage $message */
64+
/** @var TwilioSmsMessage|TwilioCallMessage $message */
6265
try {
63-
if ($message instanceof SmsMessage::class) {
66+
if ($message instanceof TwilioSmsMessage::class) {
6467
$response = $this->twilio->account->messages->sendMessage(
65-
$message->from ?: $config['from'],
68+
$from,
6669
$to,
6770
trim($message->content)
6871
);
69-
} elseif ($message instanceof CallMessage::class) {
72+
} elseif ($message instanceof TwilioCallMessage::class) {
7073
$response = $this->twilio->account->calls->create(
71-
$message->from ?: $config['from'],
74+
$from,
7275
$to,
7376
trim($message->url)
7477
);

src/Provider.php renamed to src/TwilioProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
use Illuminate\Support\ServiceProvider;
66

7-
class Provider extends ServiceProvider
7+
class TwilioProvider extends ServiceProvider
88
{
99
/**
1010
* Bootstrap the application services.
1111
*/
1212
public function boot()
1313
{
14-
$this->app->when(Channel::class)
14+
$this->app->when(TwilioChannel::class)
1515
->needs(\Services_Twilio::class)
1616
->give(function () {
1717
$config = config('services.twilio');

src/SmsMessage.php renamed to src/TwilioSmsMessage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace NotificationChannels\Twilio;
44

5-
class SmsMessage
5+
class TwilioSmsMessage
66
{
77
/**
88
* The message content.

0 commit comments

Comments
 (0)