1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\Formatter; |
4
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\Emailable; |
6
|
|
|
use IrishDan\NotificationBundle\Exception\MessageFormatException; |
7
|
|
|
use IrishDan\NotificationBundle\Message\Message; |
8
|
|
|
use IrishDan\NotificationBundle\Notification\NotificationInterface; |
9
|
|
|
|
10
|
|
|
class MailDataFormatter extends BaseFormatter implements MessageFormatterInterface |
11
|
|
|
{ |
12
|
|
|
const CHANNEL = 'mail'; |
13
|
|
|
private $mailConfiguration; |
14
|
|
|
|
15
|
|
|
public function __construct(array $mailConfiguration) |
16
|
|
|
{ |
17
|
|
|
$this->mailConfiguration = $mailConfiguration; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function format(NotificationInterface $notification) |
21
|
|
|
{ |
22
|
|
|
$notification->setChannel(self::CHANNEL); |
23
|
|
|
|
24
|
|
|
$message = new Message(); |
25
|
|
|
$notificationData = $notification->getDataArray(); |
26
|
|
|
|
27
|
|
|
/** @var Emailable $notifiable */ |
28
|
|
|
$notifiable = $notification->getNotifiable(); |
29
|
|
|
if (!$notifiable instanceof Emailable) { |
30
|
|
|
throw new MessageFormatException( |
31
|
|
|
'Notifiable must implement Emailable interface in order to format email message' |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Set the channel key |
36
|
|
|
$message->setChannel(self::CHANNEL); |
37
|
|
|
|
38
|
|
|
// Build the dispatch data array. |
39
|
|
|
$dispatchData = [ |
40
|
|
|
'to' => $notifiable->getEmail(), |
41
|
|
|
'from' => empty($this->mailConfiguration['default_sender']) ? '' : $this->mailConfiguration['default_sender'], |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
// Build the message data array. |
45
|
|
|
$messageData = []; |
46
|
|
|
$messageData['body'] = empty($notificationData['body']) ? '' : $notificationData['body']; |
47
|
|
|
$messageData['title'] = empty($notificationData['title']) ? '' : $notificationData['title']; |
48
|
|
|
|
49
|
|
View Code Duplication |
if (!empty($this->twig) && $notification->getTemplate()) { |
|
|
|
|
50
|
|
|
$messageData['body'] = $this->renderTwigTemplate( |
51
|
|
|
$notificationData, |
52
|
|
|
$notifiable, |
53
|
|
|
$notification->getTemplate() |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$message->setDispatchData($dispatchData); |
58
|
|
|
$message->setMessageData($messageData); |
59
|
|
|
|
60
|
|
|
return $message; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.