1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\AfricasTalking; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Notifications\Notification; |
7
|
|
|
use AfricasTalking\SDK\AfricasTalking as AfricasTalkingSDK; |
8
|
|
|
use NotificationChannels\AfricasTalking\Exceptions\InvalidPhonenumber; |
9
|
|
|
use NotificationChannels\AfricasTalking\Exceptions\CouldNotSendNotification; |
10
|
|
|
|
11
|
|
|
class AfricasTalkingChannel |
12
|
|
|
{ |
13
|
|
|
/** @var AfricasTalkingSDK */ |
14
|
|
|
protected $africasTalking; |
15
|
|
|
|
16
|
2 |
|
/** @param AfricasTalkingSDK $africasTalking */ |
17
|
|
|
public function __construct(AfricasTalkingSDK $africasTalking) |
18
|
2 |
|
{ |
19
|
2 |
|
$this->africasTalking = $africasTalking; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Send the given notification. |
24
|
|
|
* |
25
|
|
|
* @param mixed $notifiable |
26
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
27
|
|
|
* @throws CouldNotSendNotification |
28
|
1 |
|
*/ |
29
|
|
|
public function send($notifiable, Notification $notification) |
30
|
1 |
|
{ |
31
|
|
|
$message = $notification->toAfricasTalking($notifiable); |
|
|
|
|
32
|
1 |
|
|
33
|
|
|
if (!$phoneNumber = $notifiable->routeNotificationFor('africasTalking')) { |
34
|
|
|
$phoneNumber = $notifiable->phone_number; |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
if(!empty($message->getTo())) { |
38
|
1 |
|
$phoneNumber = $message->getTo(); |
39
|
1 |
|
} |
40
|
1 |
|
|
41
|
|
|
if(empty($phoneNumber)) { |
42
|
|
|
throw InvalidPhonenumber::configurationNotSet(); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
if (empty(($message->getSender())) || is_null($message->getSender())) { |
46
|
|
|
$params = [ |
47
|
|
|
'to' => $phoneNumber, |
48
|
|
|
'message' => $message->getContent(), |
49
|
|
|
]; |
50
|
|
|
} else { |
51
|
|
|
$params = [ |
52
|
|
|
'to' => $phoneNumber, |
53
|
|
|
'message' => $message->getContent(), |
54
|
|
|
'from' => $message->getSender(), |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
return $this->africasTalking->sms()->send($params); |
60
|
|
|
} catch (Exception $e) { |
61
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage()); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|