1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\Adapter; |
4
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\Message\MessageInterface; |
6
|
|
|
use IrishDan\NotificationBundle\Notification\NotificationInterface; |
7
|
|
|
use IrishDan\NotificationBundle\TextableInterface; |
8
|
|
|
use Nexmo\Client; |
9
|
|
|
|
10
|
|
|
class NexmoMessageAdapter extends BaseMessageAdapter implements MessageAdapterInterface |
11
|
|
|
{ |
12
|
|
|
protected $client; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Generates a message object |
16
|
|
|
* |
17
|
|
|
* @param NotificationInterface $notification |
18
|
|
|
* @return \IrishDan\NotificationBundle\Message\Message |
19
|
|
|
*/ |
20
|
|
|
public function format(NotificationInterface $notification) |
21
|
|
|
{ |
22
|
|
|
parent::format($notification); |
23
|
|
|
|
24
|
|
|
/** @var TextableInterface $notifiable */ |
25
|
|
|
$notifiable = $notification->getNotifiable(); |
26
|
|
|
if (!$notifiable instanceof TextableInterface) { |
27
|
|
|
$this->createFormatterException(TextableInterface::class, $this->channelName); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// Build the dispatch data array. |
31
|
|
|
$dispatchData = [ |
32
|
|
|
'to' => $notifiable->getNumber(), |
33
|
|
|
'from' => empty($this->configuration['from']) ? '' : $this->configuration['from'], |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
$messageData = self::createMessagaData($notification->getDataArray()); |
37
|
|
|
|
38
|
|
|
return self::createMessage($dispatchData, $messageData, $this->channelName); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function dispatch(MessageInterface $message) |
42
|
|
|
{ |
43
|
|
|
// Get the dispatch and message data from the message. |
44
|
|
|
$dispatchData = $message->getDispatchData(); |
45
|
|
|
$messageData = $message->getMessageData(); |
46
|
|
|
|
47
|
|
|
if (empty($this->client)) { |
48
|
|
|
$credentials = new Client\Credentials\Basic( |
49
|
|
|
$this->configuration['api_key'], |
50
|
|
|
$this->configuration['api_secret'] |
51
|
|
|
); |
52
|
|
|
$this->client = new Client($credentials); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$sent = $this->client->message()->send( |
56
|
|
|
[ |
57
|
|
|
'to' => $dispatchData['to'], |
58
|
|
|
'from' => $dispatchData['from'], |
59
|
|
|
'text' => $messageData['body'], |
60
|
|
|
] |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
return !empty($sent); |
64
|
|
|
} |
65
|
|
|
} |