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