Skip to content

Commit 1c9f1f9

Browse files
committed
code review
1 parent d686567 commit 1c9f1f9

File tree

7 files changed

+68
-56
lines changed

7 files changed

+68
-56
lines changed

src/Events/MessageWasSent.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class MessageWasSent
2121
public function __construct($notifiable, Notification $notification, $response = null)
2222
{
2323
$this->notifiable = $notifiable;
24+
2425
$this->notification = $notification;
26+
2527
$this->response = $response;
2628
}
2729
}

src/Events/SendingMessage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class SendingMessage
1111
/** @var \Illuminate\Notifications\Notification */
1212
protected $notification;
1313

14+
/** @var mixed|\NotificationChannels\Twilio\CallMessage|\NotificationChannels\Twilio\SmsMessage */
1415
protected $message;
1516

1617
/**

src/Exceptions/CouldNotSendNotification.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ public static function serviceRespondedWithAnException($exception)
1818
}
1919

2020
/**
21-
* @param string $class
21+
* @param mixed $message
2222
*
2323
* @return static
2424
*/
25-
public static function invalidMessageObject($class)
25+
public static function invalidMessageObject($message)
2626
{
27-
return new static("Notification was not sent. Message object class `{$class}` is invalid. It should be either `".SmsMessage::class.'` or `'.CallMessage::class.'`');
27+
$className = get_class($message) ?: 'Unknown';
28+
29+
return new static("Notification was not sent. Message object class `{$className}` is invalid. It should be either `".SmsMessage::class.'` or `'.CallMessage::class.'`');
2830
}
2931

3032
/**

src/TwilioCallMessage.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,30 @@ class TwilioCallMessage
1919
public $from;
2020

2121
/**
22-
* Create a new message instance.
22+
* @param string $url
2323
*
24-
* @param string $url
25-
* @return void
24+
* @return static
2625
*/
27-
public function __construct($url = '')
26+
public static function create($url = '')
2827
{
29-
$this->url = $url;
28+
return new static($url);
3029
}
3130

3231
/**
33-
* @param string $url
32+
* Create a new message instance.
3433
*
35-
* @return static
34+
* @param string $url
3635
*/
37-
public static function create($url = '')
36+
public function __construct($url = '')
3837
{
39-
return new static($url);
38+
$this->url = $url;
4039
}
4140

4241
/**
4342
* Set the message content.
4443
*
4544
* @param string $url
45+
*
4646
* @return $this
4747
*/
4848
public function url($url)
@@ -56,6 +56,7 @@ public function url($url)
5656
* Set the phone number the message should be sent from.
5757
*
5858
* @param string $from
59+
*
5960
* @return $this
6061
*/
6162
public function from($from)

src/TwilioChannel.php

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace NotificationChannels\Twilio;
44

5+
use Exception;
56
use Illuminate\Notifications\Notification;
67
use NotificationChannels\Twilio\Events\MessageWasSent;
78
use NotificationChannels\Twilio\Events\SendingMessage;
@@ -10,9 +11,7 @@
1011

1112
class TwilioChannel
1213
{
13-
/**
14-
* @var \Services_Twilio
15-
*/
14+
/** @var \Services_Twilio */
1615
protected $twilio;
1716

1817
public function __construct(Services_Twilio $twilio)
@@ -25,60 +24,73 @@ public function __construct(Services_Twilio $twilio)
2524
*
2625
* @param mixed $notifiable
2726
* @param \Illuminate\Notifications\Notification $notification
28-
* @return void
27+
*
2928
* @throws CouldNotSendNotification
3029
*/
3130
public function send($notifiable, Notification $notification)
3231
{
33-
if (! $to = $notifiable->routeNotificationFor('twilio')) {
32+
if (!$to = $notifiable->routeNotificationFor('twilio')) {
3433
return;
3534
}
3635

3736
$message = $notification->toTwilio($notifiable);
3837

3938
if (is_string($message)) {
40-
// Default to SMS message if only a string is provided
4139
$message = new TwilioSmsMessage($message);
4240
}
4341

44-
if (! ($message instanceof TwilioSmsMessage) &&
45-
! ($message instanceof TwilioCallMessage)) {
46-
$class = get_class($message) ?: 'Unknown';
47-
48-
throw CouldNotSendNotification::invalidMessageObject($class);
42+
if (!in_array(get_class($message), [TwilioSmsMessage::class, TwilioCallMessage::class])) {
43+
throw CouldNotSendNotification::invalidMessageObject($message);
4944
}
5045

51-
if (! $from = $message->from ?: config('services.twilio.from')) {
46+
if (!$from = $message->from ?: config('services.twilio.from')) {
5247
throw CouldNotSendNotification::missingFrom();
5348
}
5449

5550
$shouldSendMessage = event(new SendingMessage($notifiable, $notification, $message), [], true) !== false;
5651

57-
if (! $shouldSendMessage) {
52+
if (!$shouldSendMessage) {
5853
return;
5954
}
6055

6156
$response = null;
6257

63-
/* @var TwilioSmsMessage|TwilioCallMessage $message */
6458
try {
65-
if ($message instanceof TwilioSmsMessage) {
66-
$response = $this->twilio->account->messages->sendMessage(
67-
$from,
68-
$to,
69-
trim($message->content)
70-
);
71-
} elseif ($message instanceof TwilioCallMessage) {
72-
$response = $this->twilio->account->calls->create(
73-
$from,
74-
$to,
75-
trim($message->url)
76-
);
77-
}
78-
} catch (\Exception $exception) {
59+
$response = $this->sendMessage($message, $from, $to);
60+
} catch (Exception $exception) {
7961
throw CouldNotSendNotification::serviceRespondedWithAnException($exception);
8062
}
8163

8264
event(new MessageWasSent($notifiable, $notification, $response));
8365
}
66+
67+
/**
68+
* @param $message
69+
* @param $from
70+
* @param $to
71+
* @return mixed
72+
*
73+
* @throws \NotificationChannels\Twilio\Exceptions\CouldNotSendNotification
74+
*/
75+
protected function sendMessage($message, $from, $to)
76+
{
77+
if ($message instanceof TwilioSmsMessage) {
78+
return $this->twilio->account->messages->sendMessage(
79+
$from,
80+
$to,
81+
trim($message->content)
82+
);
83+
return $response;
84+
}
85+
86+
if ($message instanceof TwilioCallMessage) {
87+
return $this->twilio->account->calls->create(
88+
$from,
89+
$to,
90+
trim($message->url)
91+
);
92+
}
93+
94+
throw CouldNotSendNotification::invalidMessageObject($message);
95+
}
8496
}

src/TwilioProvider.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,4 @@ public function boot()
2222
);
2323
});
2424
}
25-
26-
/**
27-
* Register the application services.
28-
*/
29-
public function register()
30-
{
31-
}
3225
}

src/TwilioSmsMessage.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,30 @@ class TwilioSmsMessage
1919
public $from;
2020

2121
/**
22-
* Create a new message instance.
22+
* @param string $content
2323
*
24-
* @param string $content
25-
* @return void
24+
* @return static
2625
*/
27-
public function __construct($content = '')
26+
public static function create($content = '')
2827
{
29-
$this->content = $content;
28+
return new static($content);
3029
}
3130

3231
/**
33-
* @param string $content
32+
* Create a new message instance.
3433
*
35-
* @return static
34+
* @param string $content
3635
*/
37-
public static function create($content = '')
36+
public function __construct($content = '')
3837
{
39-
return new static($content);
38+
$this->content = $content;
4039
}
4140

4241
/**
4342
* Set the message content.
4443
*
4544
* @param string $content
45+
*
4646
* @return $this
4747
*/
4848
public function content($content)
@@ -56,6 +56,7 @@ public function content($content)
5656
* Set the phone number the message should be sent from.
5757
*
5858
* @param string $from
59+
*
5960
* @return $this
6061
*/
6162
public function from($from)

0 commit comments

Comments
 (0)