Completed
Push — master ( cbd26f...25ba26 )
by dan
02:08
created

MailMessageDispatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace IrishDan\NotificationBundle\Dispatcher;
4
5
use IrishDan\NotificationBundle\Message\MessageInterface;
6
7
class MailMessageDispatcher implements MessageDispatcherInterface
8
{
9
    protected $mailer;
10
11
    public function __construct(\Swift_Mailer $mailer)
12
    {
13
        $this->mailer = $mailer;
14
    }
15
16
    public function dispatch(MessageInterface $message)
17
    {
18
        // Get the dispatch and message data from the message.
19
        $dispatchData = $message->getDispatchData();
20
        $messageData = $message->getMessageData();
21
22
        $mail = \Swift_Message::newInstance()
23
            ->setSubject($messageData['title'])
24
            ->setBody($messageData['body']);
25
26
        $mail->setFrom($dispatchData['from']);
27
        $mail->setTo($dispatchData['to']);
28
29
        // Check if its a html email
30
        if (!empty($messageData['html_email'])) {
31
            $mail->setContentType('text/html');
32
        }
33
34
        // Add any attachments
35
        if (!empty($messageData['attachments'])) {
36
            foreach ($messageData['attachments'] as $path => $filename) {
37
                $mail->attach(
38
                    \Swift_Attachment::fromPath($path)->setFilename($filename)
39
                );
40
            }
41
        }
42
43
        $sent = $this->mailer->send($mail);
44
45
        return !empty($sent);
46
    }
47
}