MailMessageAdapter::format()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
cc 4
nc 8
nop 1
1
<?php
2
3
namespace IrishDan\NotificationBundle\Adapter;
4
5
use IrishDan\NotificationBundle\EmailableInterface;
6
use IrishDan\NotificationBundle\Message\MessageInterface;
7
use IrishDan\NotificationBundle\Notification\NotificationInterface;
8
9
class MailMessageAdapter extends BaseMessageAdapter implements MessageAdapterInterface
10
{
11
    protected $mailer;
12
13
    public function __construct(\Swift_Mailer $mailer)
14
    {
15
        $this->mailer = $mailer;
16
    }
17
18
    /**
19
     * Generates a Message object
20
     *
21
     * @param NotificationInterface $notification
22
     * @return \IrishDan\NotificationBundle\Message\Message
23
     */
24
    public function format(NotificationInterface $notification)
25
    {
26
        $notification->setChannel($this->channelName);
27
        parent::format($notification);
28
29
        /** @var EmailableInterface $notifiable */
30
        $notifiable = $notification->getNotifiable();
31
        if (!$notifiable instanceof EmailableInterface) {
32
            $this->createFormatterException(EmailableInterface::class, $this->channelName);
33
        }
34
35
        // Build the dispatch data array.
36
        $dispatchData = [
37
            'to' => $notifiable->getEmail(),
38
            'from' => $this->getSender($notification),
39
            'cc' => '',
40
            'bcc' => '',
41
        ];
42
43
        $messageData = self::createMessagaData($notification->getDataArray());
44
        $data = $notification->getDataArray();
45
46
        if (!empty($data['html_email'])) {
47
            $messageData['html_email'] = true;
48
        }
49
        // Add any email attachments.
50
        $messageData['attachments'] = empty($data['attachments']) ? [] : $data['attachments'];
51
52
        return self::createMessage($dispatchData, $messageData, $this->channelName);
53
    }
54
55
    protected function getSender(NotificationInterface $notification)
56
    {
57
        $data = $notification->getDataArray();
58
59
        if (!empty($data['from'])) {
60
            return $data['from'];
61
        }
62
63
        if (!empty($this->configuration['default_sender'])) {
64
            return $this->configuration['default_sender'];
65
        }
66
67
        throw new \LogicException(
68
            'There is no "from" email address or "default_sender" configured'
69
        );
70
    }
71
72
    public function dispatch(MessageInterface $message)
73
    {
74
        // Get the dispatch and message data from the message.
75
        $dispatchData = $message->getDispatchData();
76
        $messageData = $message->getMessageData();
77
78
        $mail = \Swift_Message::newInstance()
79
            ->setSubject($messageData['title'])
80
            ->setBody($messageData['body']);
81
82
        $mail->setFrom($dispatchData['from']);
83
        $mail->setTo($dispatchData['to']);
84
85
        // Check if its a html email
86
        if (!empty($messageData['html_email'])) {
87
            $mail->setContentType('text/html');
88
        }
89
90
        // Add any attachments
91
        if (!empty($messageData['attachments'])) {
92
            foreach ($messageData['attachments'] as $path => $filename) {
93
                $mail->attach(
94
                    \Swift_Attachment::fromPath($path)->setFilename($filename)
95
                );
96
            }
97
        }
98
99
        $sent = $this->mailer->send($mail);
100
101
        return !empty($sent);
102
    }
103
}