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

MailMessageDispatcher   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B dispatch() 0 31 4
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
}