|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\AwsSns; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
|
6
|
|
|
use Illuminate\Notifications\Events\NotificationFailed; |
|
7
|
|
|
use Illuminate\Notifications\Notification; |
|
8
|
|
|
use NotificationChannels\AwsSns\Exceptions\CouldNotSendNotification; |
|
9
|
|
|
|
|
10
|
|
|
class SnsChannel |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Sns |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $sns; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Dispatcher |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $events; |
|
21
|
|
|
|
|
22
|
5 |
|
public function __construct(Sns $sns, Dispatcher $events) |
|
23
|
|
|
{ |
|
24
|
5 |
|
$this->sns = $sns; |
|
25
|
5 |
|
$this->events = $events; |
|
26
|
5 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Send the given notification. |
|
30
|
|
|
* |
|
31
|
|
|
* @return \Aws\Result |
|
32
|
|
|
*/ |
|
33
|
5 |
|
public function send($notifiable, Notification $notification) |
|
34
|
|
|
{ |
|
35
|
|
|
try { |
|
36
|
5 |
|
$destination = $this->getDestination($notifiable, $notification); |
|
37
|
4 |
|
$message = $this->getMessage($notifiable, $notification); |
|
38
|
|
|
|
|
39
|
3 |
|
return $this->sns->send($message, $destination); |
|
40
|
2 |
|
} catch (\Exception $e) { |
|
41
|
2 |
|
$event = new NotificationFailed( |
|
42
|
2 |
|
$notifiable, |
|
43
|
2 |
|
$notification, |
|
44
|
2 |
|
'sns', |
|
45
|
2 |
|
['message' => $e->getMessage(), 'exception' => $e] |
|
46
|
|
|
); |
|
47
|
2 |
|
$this->events->dispatch($event); |
|
48
|
|
|
} |
|
49
|
2 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the phone number to send a notification to. |
|
53
|
|
|
* |
|
54
|
|
|
* @throws CouldNotSendNotification |
|
55
|
|
|
*/ |
|
56
|
5 |
|
protected function getDestination($notifiable, Notification $notification) |
|
57
|
|
|
{ |
|
58
|
5 |
|
if ($to = $notifiable->routeNotificationFor('sns', $notification)) { |
|
59
|
1 |
|
return $to; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
4 |
|
return $this->guessDestination($notifiable); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Try to get the phone number from some commonly used attributes for that. |
|
67
|
|
|
* |
|
68
|
|
|
* @throws CouldNotSendNotification |
|
69
|
|
|
*/ |
|
70
|
4 |
|
protected function guessDestination($notifiable) |
|
71
|
|
|
{ |
|
72
|
4 |
|
$commonAttributes = ['phone', 'phone_number', 'full_phone']; |
|
73
|
4 |
|
foreach ($commonAttributes as $attribute) { |
|
74
|
4 |
|
if (isset($notifiable->{$attribute})) { |
|
75
|
3 |
|
return $notifiable->{$attribute}; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
throw CouldNotSendNotification::invalidReceiver(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the SNS Message object. |
|
84
|
|
|
* |
|
85
|
|
|
* @throws CouldNotSendNotification |
|
86
|
|
|
*/ |
|
87
|
4 |
|
protected function getMessage($notifiable, Notification $notification): SnsMessage |
|
88
|
|
|
{ |
|
89
|
4 |
|
$message = $notification->toSns($notifiable); |
|
|
|
|
|
|
90
|
4 |
|
if (is_string($message)) { |
|
91
|
1 |
|
return new SnsMessage($message); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
3 |
|
if ($message instanceof SnsMessage) { |
|
95
|
2 |
|
return $message; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
throw CouldNotSendNotification::invalidMessageObject($message); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.