Skip to content

Commit a2d350f

Browse files
authored
Support adding to dynamically (#17)
* support setting recipient dynamically * wip
1 parent aabbd75 commit a2d350f

File tree

4 files changed

+75
-3
lines changed

4 files changed

+75
-3
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ You can also modify who the notification(SMS) is sent from, this will overide th
131131
->from("set any sender id/name here");
132132
```
133133
134+
You can also modify who the notification(SMS) is sent to (the recipient)
135+
136+
``` php
137+
return (new AfricasTalkingMessage())
138+
->content('Your SMS message content')
139+
->to("put the recipient phonenumber here"); //eg ->to(1111111111)
140+
```
141+
It's important to know the Order in which the recipient phone number the notification(SMS) will be sent to will be used
142+
143+
1) If you have defined the routeNotificationForAfricasTalking() method on the Notifiable class (User.php in this case) and returned a valid phone number, then that will be used.
144+
145+
2) if you did not define routeNotificationForAfricasTalking() method on the Notifiable class (User.php in this case), then the phone attribute of the User will be used ($user->phone)
146+
147+
3) Lastly if 1 and 2 are not set, then you can set the recipient phone number using ->to(19283281921)
148+
149+
``` php
150+
return (new AfricasTalkingMessage())
151+
->content('Your SMS message content')
152+
->to("put the recipient phonenumber here");
153+
```
134154
135155
## Testing
136156

src/AfricasTalkingChannel.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace NotificationChannels\AfricasTalking;
44

5-
use AfricasTalking\SDK\AfricasTalking as AfricasTalkingSDK;
65
use Exception;
76
use Illuminate\Notifications\Notification;
7+
use AfricasTalking\SDK\AfricasTalking as AfricasTalkingSDK;
8+
use NotificationChannels\AfricasTalking\Exceptions\InvalidPhonenumber;
89
use NotificationChannels\AfricasTalking\Exceptions\CouldNotSendNotification;
910

1011
class AfricasTalkingChannel
@@ -30,7 +31,11 @@ public function send($notifiable, Notification $notification)
3031
$message = $notification->toAfricasTalking($notifiable);
3132

3233
if (!$phoneNumber = $notifiable->routeNotificationFor('africasTalking')) {
33-
$phoneNumber = $notifiable->phone_number;
34+
$phoneNumber = $notifiable->phone_number ?? $message->getTo();
35+
}
36+
37+
if(empty($phoneNumber)) {
38+
throw InvalidPhonenumber::configurationNotSet();
3439
}
3540

3641
if (empty(($message->getSender())) || is_null($message->getSender())) {
@@ -47,7 +52,7 @@ public function send($notifiable, Notification $notification)
4752
}
4853

4954
try {
50-
$this->africasTalking->sms()->send($params);
55+
return $this->africasTalking->sms()->send($params);
5156
} catch (Exception $e) {
5257
throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage());
5358
}

src/AfricasTalkingMessage.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class AfricasTalkingMessage
1010
/** @var string|null */
1111
protected $from;
1212

13+
/** @var string|null */
14+
protected $to;
15+
1316
/**
1417
* Set content for this message.
1518
*
@@ -36,6 +39,19 @@ public function from(string $from): self
3639
return $this;
3740
}
3841

42+
/**
43+
* Set recipient for this message.
44+
*
45+
* @param string $from
46+
* @return self
47+
*/
48+
public function to(string $to): self
49+
{
50+
$this->to = trim($to);
51+
52+
return $this;
53+
}
54+
3955
/**
4056
* Get message content.
4157
*
@@ -55,4 +71,14 @@ public function getSender()
5571
{
5672
return $this->from ?? config('services.africastalking.from');
5773
}
74+
75+
/**
76+
* Get recipient info.
77+
*
78+
* @return string
79+
*/
80+
public function getTo()
81+
{
82+
return $this->to ?? null;
83+
}
5884
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace NotificationChannels\AfricasTalking\Exceptions;
4+
5+
use Exception;
6+
7+
class InvalidPhonenumber extends Exception
8+
{
9+
public static function configurationNotSet(): self
10+
{
11+
return new static(
12+
"please provide a phonenumber to which the notification(SMS) will be sent to, you can do these in three ways:
13+
1) by defining a routeNotificationForAfricasTalking method on your notifiable entity
14+
or
15+
2) by creating a new column called `phone` in your notifiable table
16+
3) by chaining from(phonenumber) method to your AfricasTalkingMessage() eg (new AfricasTalkingMessage())
17+
->content('Your SMS message content')->to(11111111); method in your event class
18+
"
19+
);
20+
}
21+
}

0 commit comments

Comments
 (0)