Skip to content

Commit abfeaf5

Browse files
committed
Some abstraction, code cleaning and improvement
1 parent 0c32a6f commit abfeaf5

File tree

6 files changed

+82
-127
lines changed

6 files changed

+82
-127
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class AccountApproved extends Notification
101101
}
102102
```
103103

104-
In order to let your Notification know which phone are you sending/calling to, add the `routeNotificationForTwilio` method to your Notifiable model.
104+
In order to let your Notification know which phone are you sending/calling to, the channel will look for the `phone_number` attribute of the Notifiable model. If you want to override this behaviour, add the `routeNotificationForTwilio` method to your Notifiable model.
105105

106106
```php
107107
public function routeNotificationForTwilio()

src/TwilioAbstractMessage.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace NotificationChannels\Twilio;
4+
5+
abstract class TwilioAbstractMessage
6+
{
7+
/**
8+
* The message content.
9+
*
10+
* @var string
11+
*/
12+
public $content;
13+
14+
/**
15+
* The phone number the message should be sent from.
16+
*
17+
* @var string
18+
*/
19+
public $from;
20+
21+
/**
22+
* @param string $content
23+
*
24+
* @return static
25+
*/
26+
public static function create($content = '')
27+
{
28+
return new static($content);
29+
}
30+
31+
/**
32+
* Create a new message instance.
33+
*
34+
* @param string $content
35+
*/
36+
public function __construct($content = '')
37+
{
38+
$this->content = $content;
39+
}
40+
41+
/**
42+
* Set the message content.
43+
*
44+
* @param string $content
45+
*
46+
* @return $this
47+
*/
48+
public function content($content)
49+
{
50+
$this->content = $content;
51+
52+
return $this;
53+
}
54+
55+
/**
56+
* Set the phone number the message should be sent from.
57+
*
58+
* @param string $from
59+
*
60+
* @return $this
61+
*/
62+
public function from($from)
63+
{
64+
$this->from = $from;
65+
66+
return $this;
67+
}
68+
}

src/TwilioCallMessage.php

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,18 @@
22

33
namespace NotificationChannels\Twilio;
44

5-
class TwilioCallMessage
5+
class TwilioCallMessage extends TwilioAbstractMessage
66
{
77
/**
8-
* The message TwiML url.
9-
*
10-
* @var string
11-
*/
12-
public $url;
13-
14-
/**
15-
* The phone number the message should be sent from.
16-
*
17-
* @var string
18-
*/
19-
public $from;
20-
21-
/**
22-
* @param string $url
23-
*
24-
* @return static
25-
*/
26-
public static function create($url = '')
27-
{
28-
return new static($url);
29-
}
30-
31-
/**
32-
* Create a new message instance.
33-
*
34-
* @param string $url
35-
*/
36-
public function __construct($url = '')
37-
{
38-
$this->url = $url;
39-
}
40-
41-
/**
42-
* Set the message content.
8+
* Set the message url.
439
*
4410
* @param string $url
4511
*
4612
* @return $this
4713
*/
4814
public function url($url)
4915
{
50-
$this->url = $url;
51-
52-
return $this;
53-
}
54-
55-
/**
56-
* Set the phone number the message should be sent from.
57-
*
58-
* @param string $from
59-
*
60-
* @return $this
61-
*/
62-
public function from($from)
63-
{
64-
$this->from = $from;
16+
$this->content = $url;
6517

6618
return $this;
6719
}

src/TwilioChannel.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use Exception;
66
use Illuminate\Notifications\Notification;
7-
use NotificationChannels\Twilio\Events\MessageWasSent;
8-
use NotificationChannels\Twilio\Events\SendingMessage;
97
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification;
108
use Services_Twilio;
119

@@ -30,7 +28,9 @@ public function __construct(Services_Twilio $twilio)
3028
public function send($notifiable, Notification $notification)
3129
{
3230
if (! $to = $notifiable->routeNotificationFor('twilio')) {
33-
return;
31+
if (! $to = $notifiable->phone_number) {
32+
return;
33+
}
3434
}
3535

3636
$message = $notification->toTwilio($notifiable);
@@ -39,18 +39,16 @@ public function send($notifiable, Notification $notification)
3939
$message = new TwilioSmsMessage($message);
4040
}
4141

42-
if (! in_array(get_class($message), [TwilioSmsMessage::class, TwilioCallMessage::class])) {
42+
if (! $message instanceof TwilioAbstractMessage) {
4343
throw CouldNotSendNotification::invalidMessageObject($message);
4444
}
4545

4646
if (! $from = $message->from ?: config('services.twilio.from')) {
4747
throw CouldNotSendNotification::missingFrom();
4848
}
4949

50-
$response = null;
51-
5250
try {
53-
$response = $this->sendMessage($message, $from, $to);
51+
$this->sendMessage($message, $from, $to);
5452
} catch (Exception $exception) {
5553
throw CouldNotSendNotification::serviceRespondedWithAnException($exception);
5654
}
@@ -72,15 +70,13 @@ protected function sendMessage($message, $from, $to)
7270
$to,
7371
trim($message->content)
7472
);
75-
76-
return $response;
7773
}
7874

7975
if ($message instanceof TwilioCallMessage) {
8076
return $this->twilio->account->calls->create(
8177
$from,
8278
$to,
83-
trim($message->url)
79+
trim($message->content)
8480
);
8581
}
8682

src/TwilioSmsMessage.php

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,6 @@
22

33
namespace NotificationChannels\Twilio;
44

5-
class TwilioSmsMessage
5+
class TwilioSmsMessage extends TwilioAbstractMessage
66
{
7-
/**
8-
* The message content.
9-
*
10-
* @var string
11-
*/
12-
public $content;
13-
14-
/**
15-
* The phone number the message should be sent from.
16-
*
17-
* @var string
18-
*/
19-
public $from;
20-
21-
/**
22-
* @param string $content
23-
*
24-
* @return static
25-
*/
26-
public static function create($content = '')
27-
{
28-
return new static($content);
29-
}
30-
31-
/**
32-
* Create a new message instance.
33-
*
34-
* @param string $content
35-
*/
36-
public function __construct($content = '')
37-
{
38-
$this->content = $content;
39-
}
40-
41-
/**
42-
* Set the message content.
43-
*
44-
* @param string $content
45-
*
46-
* @return $this
47-
*/
48-
public function content($content)
49-
{
50-
$this->content = $content;
51-
52-
return $this;
53-
}
54-
55-
/**
56-
* Set the phone number the message should be sent from.
57-
*
58-
* @param string $from
59-
*
60-
* @return $this
61-
*/
62-
public function from($from)
63-
{
64-
$this->from = $from;
65-
66-
return $this;
67-
}
687
}

tests/TwilioCallMessageTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ public function it_can_accept_an_url_when_constructing_a_message()
2222
{
2323
$message = new TwilioCallMessage('http://example.com');
2424

25-
$this->assertEquals('http://example.com', $message->url);
25+
$this->assertEquals('http://example.com', $message->content);
2626
}
2727

2828
/** @test */
2929
public function it_provides_a_create_method()
3030
{
3131
$message = TwilioCallMessage::create('http://example.com');
3232

33-
$this->assertEquals('http://example.com', $message->url);
33+
$this->assertEquals('http://example.com', $message->content);
3434
}
3535

3636
/** @test */
3737
public function it_can_set_the_url()
3838
{
3939
$this->message->url('http://example.com');
4040

41-
$this->assertEquals('http://example.com', $this->message->url);
41+
$this->assertEquals('http://example.com', $this->message->content);
4242
}
4343

4444
/** @test */

0 commit comments

Comments
 (0)