1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\Adapter; |
4
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\Exception\MessageFormatException; |
6
|
|
|
use IrishDan\NotificationBundle\Message\Message; |
7
|
|
|
use IrishDan\NotificationBundle\Notification\NotificationInterface; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
9
|
|
|
|
10
|
|
|
abstract class BaseMessageAdapter |
11
|
|
|
{ |
12
|
|
|
protected $twig; |
13
|
|
|
protected $eventDispatcher; |
14
|
|
|
|
15
|
|
|
public function setTemplating(\Twig_Environment $twig) |
16
|
|
|
{ |
17
|
|
|
$this->twig = $twig; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function setDispatcher(EventDispatcherInterface $eventDispatcher) |
21
|
|
|
{ |
22
|
|
|
$this->eventDispatcher = $eventDispatcher; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function renderTwigTemplate($data, $user, $template) |
26
|
|
|
{ |
27
|
|
|
return $this->twig->render( |
28
|
|
|
$template, |
29
|
|
|
[ |
30
|
|
|
'data' => $data, |
31
|
|
|
'user' => $user, |
32
|
|
|
] |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function format(NotificationInterface $notification) |
37
|
|
|
{ |
38
|
|
|
$templateArray = $notification->getTemplateArray(); |
39
|
|
|
if (!empty($this->twig) && !empty($templateArray)) { |
40
|
|
|
$data = $notification->getDataArray(); |
41
|
|
|
|
42
|
|
|
if ($notification->getUuid()) { |
43
|
|
|
$data['uuid'] = $notification->getUuid(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
foreach ($templateArray as $key => $template) { |
47
|
|
|
// Find out if the template exists before trying to render it. |
48
|
|
|
$exists = $this->twig->getLoader()->exists($template); |
49
|
|
|
if (!empty($data[$key]) && $exists) { |
50
|
|
|
|
51
|
|
|
$data[$key] = $this->renderTwigTemplate( |
52
|
|
|
$data, |
53
|
|
|
$notification->getNotifiable(), |
54
|
|
|
$template |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$notification->setDataArray($data); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected static function createMessage($dispatchData, $messageData, $channel = 'default') |
64
|
|
|
{ |
65
|
|
|
$message = new Message(); |
66
|
|
|
|
67
|
|
|
$message->setChannel($channel); |
68
|
|
|
$message->setDispatchData($dispatchData); |
69
|
|
|
$message->setMessageData($messageData); |
70
|
|
|
|
71
|
|
|
return $message; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected static function createMessagaData(array $notificationData) |
75
|
|
|
{ |
76
|
|
|
// Build the message data array. |
77
|
|
|
$messageData = []; |
78
|
|
|
$messageData['body'] = empty($notificationData['body']) ? '' : $notificationData['body']; |
79
|
|
|
$messageData['title'] = empty($notificationData['title']) ? '' : $notificationData['title']; |
80
|
|
|
$messageData['uuid'] = empty($notificationData['uuid']) ? '' : $notificationData['uuid']; |
81
|
|
|
|
82
|
|
|
return $messageData; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected static function createFormatterException($shouldImplement, $type) |
86
|
|
|
{ |
87
|
|
|
$message = sprintf('Notifiable must implement %s interface in order to format as a %s message', $shouldImplement, $type); |
88
|
|
|
throw new MessageFormatException( |
89
|
|
|
$message |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |