1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\Adapter; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use IrishDan\NotificationBundle\DatabaseNotifiableInterface; |
7
|
|
|
use IrishDan\NotificationBundle\DatabaseNotificationManager; |
8
|
|
|
use IrishDan\NotificationBundle\Message\MessageInterface; |
9
|
|
|
use IrishDan\NotificationBundle\Notification\NotificationInterface; |
10
|
|
|
|
11
|
|
|
class DatabaseMessageAdapter extends BaseMessageAdapter implements MessageAdapterInterface |
12
|
|
|
{ |
13
|
|
|
const CHANNEL = 'database'; |
14
|
|
|
|
15
|
|
|
protected $databaseNotificationManager; |
16
|
|
|
|
17
|
|
|
public function __construct(DatabaseNotificationManager $databaseNotificationManager) |
18
|
|
|
{ |
19
|
|
|
$this->databaseNotificationManager = $databaseNotificationManager; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Generates a message object |
24
|
|
|
* |
25
|
|
|
* @param NotificationInterface $notification |
26
|
|
|
* @return \IrishDan\NotificationBundle\Message\Message |
27
|
|
|
*/ |
28
|
|
|
public function format(NotificationInterface $notification) |
29
|
|
|
{ |
30
|
|
|
$notification->setChannel(self::CHANNEL); |
31
|
|
|
parent::format($notification); |
32
|
|
|
|
33
|
|
|
/** @var DatabaseNotifiableInterface $notifiable */ |
34
|
|
|
$notifiable = $notification->getNotifiable(); |
35
|
|
|
if (!$notifiable instanceof DatabaseNotifiableInterface) { |
36
|
|
|
$this->createFormatterException(DatabaseNotifiableInterface::class, self::CHANNEL); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Build the dispatch data array. |
40
|
|
|
$dispatchData = [ |
41
|
|
|
'id' => $notifiable->getIdentifier(), |
42
|
|
|
'notifiable' => $notifiable, |
43
|
|
|
'uuid' => $notification->getUuid(), |
44
|
|
|
'type' => get_class($notification), |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
$messageData = self::createMessagaData($notification->getDataArray()); |
48
|
|
|
|
49
|
|
|
return self::createMessage($dispatchData, $messageData, self::CHANNEL); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function dispatch(MessageInterface $message) |
53
|
|
|
{ |
54
|
|
|
$dispatchData = $message->getDispatchData(); |
55
|
|
|
$messageData = $message->getMessageData(); |
56
|
|
|
$data = $dispatchData + $messageData; |
57
|
|
|
|
58
|
|
|
$databaseNotification = $this->databaseNotificationManager->createDatabaseNotification($data); |
59
|
|
|
if ($databaseNotification) { |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
} |