|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\Formatter; |
|
4
|
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\EmailableInterface; |
|
6
|
|
|
use IrishDan\NotificationBundle\Notification\NotificationInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class MailDataFormatter |
|
10
|
|
|
* |
|
11
|
|
|
* @package IrishDan\NotificationBundle\Formatter |
|
12
|
|
|
*/ |
|
13
|
|
|
class MailDataFormatter extends BaseFormatter implements MessageFormatterInterface |
|
14
|
|
|
{ |
|
15
|
|
|
const CHANNEL = 'mail'; |
|
16
|
|
|
private $mailConfiguration; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(array $mailConfiguration) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->mailConfiguration = $mailConfiguration; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Generates a Message object |
|
25
|
|
|
* |
|
26
|
|
|
* @param NotificationInterface $notification |
|
27
|
|
|
* @return \IrishDan\NotificationBundle\Message\Message |
|
28
|
|
|
*/ |
|
29
|
|
|
public function format(NotificationInterface $notification) |
|
30
|
|
|
{ |
|
31
|
|
|
$notification->setChannel(self::CHANNEL); |
|
32
|
|
|
parent::format($notification); |
|
33
|
|
|
|
|
34
|
|
|
/** @var EmailableInterface $notifiable */ |
|
35
|
|
|
$notifiable = $notification->getNotifiable(); |
|
36
|
|
|
if (!$notifiable instanceof EmailableInterface) { |
|
37
|
|
|
$this->createFormatterException(EmailableInterface::class, self::CHANNEL); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// Build the dispatch data array. |
|
41
|
|
|
$dispatchData = [ |
|
42
|
|
|
'to' => $notifiable->getEmail(), |
|
43
|
|
|
'from' => $this->getSender($notification), |
|
44
|
|
|
'cc' => '', |
|
45
|
|
|
'bcc' => '', |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
$messageData = self::createMessagaData($notification->getDataArray()); |
|
49
|
|
|
$data = $notification->getDataArray(); |
|
50
|
|
|
|
|
51
|
|
|
if (!empty($data['html_email'])) { |
|
52
|
|
|
$messageData['html_email'] = true; |
|
53
|
|
|
} |
|
54
|
|
|
// Add any email attachments. |
|
55
|
|
|
$messageData['attachments'] = empty($data['attachments']) ? [] : $data['attachments']; |
|
56
|
|
|
|
|
57
|
|
|
return self::createMessage($dispatchData, $messageData, self::CHANNEL); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function getSender(NotificationInterface $notification) |
|
61
|
|
|
{ |
|
62
|
|
|
$data = $notification->getDataArray(); |
|
63
|
|
|
|
|
64
|
|
|
if (!empty($data['from'])) { |
|
65
|
|
|
return $data['from']; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (!empty($this->mailConfiguration['default_sender'])) { |
|
69
|
|
|
return $this->mailConfiguration['default_sender']; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return ''; |
|
73
|
|
|
} |
|
74
|
|
|
} |