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\PusherableInterface; |
8
|
|
|
use IrishDan\NotificationBundle\PusherManager; |
9
|
|
|
|
10
|
|
|
class PusherMessageAdapter extends BaseMessageAdapter implements MessageAdapterInterface |
11
|
|
|
{ |
12
|
|
|
const CHANNEL = 'pusher'; |
13
|
|
|
protected $pusherManager; |
14
|
|
|
|
15
|
|
|
public function __construct(PusherManager $pusherManager) |
16
|
|
|
{ |
17
|
|
|
$this->pusherManager = $pusherManager; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Generates a message object |
22
|
|
|
* |
23
|
|
|
* @param NotificationInterface $notification |
24
|
|
|
* @return \IrishDan\NotificationBundle\Message\Message |
25
|
|
|
*/ |
26
|
|
|
public function format(NotificationInterface $notification) |
27
|
|
|
{ |
28
|
|
|
$notification->setChannel(self::CHANNEL); |
29
|
|
|
parent::format($notification); |
30
|
|
|
|
31
|
|
|
/** @var PusherableInterface $notifiable */ |
32
|
|
|
$notifiable = $notification->getNotifiable(); |
33
|
|
|
if (!$notifiable instanceof PusherableInterface) { |
34
|
|
|
$this->createFormatterException(PusherableInterface::class, self::CHANNEL); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// Build the dispatch data array. |
38
|
|
|
$dispatchData = [ |
39
|
|
|
'channel' => $this->pusherManager->getUserChannelName($notifiable), |
40
|
|
|
'event' => $this->pusherManager->getEvent(), |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
$messageData = self::createMessagaData($notification->getDataArray()); |
44
|
|
|
|
45
|
|
|
return self::createMessage($dispatchData, $messageData, self::CHANNEL); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function dispatch(MessageInterface $message) |
49
|
|
|
{ |
50
|
|
|
// Get the dispatch and message data from the message. |
51
|
|
|
$dispatchData = $message->getDispatchData(); |
52
|
|
|
$messageData = $message->getMessageData(); |
53
|
|
|
|
54
|
|
|
$pusher = $this->pusherManager->getPusherClient(); |
55
|
|
|
|
56
|
|
|
$channel = [ |
57
|
|
|
$dispatchData['channel'], |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
return !empty($pusher->trigger($channel, $dispatchData['event'], $messageData)); |
61
|
|
|
} |
62
|
|
|
} |