SwiftEmailMapper::map()   B
last analyzed

Complexity

Conditions 7
Paths 64

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.4266
c 0
b 0
f 0
cc 7
nc 64
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Infrastructure\Notification\Client\Email\SwiftMailer\Mapper;
16
17
use Acme\App\Core\Port\Notification\Client\Email\Email;
18
use Swift_Attachment;
19
use Swift_Message;
20
use Swift_Mime_SimpleMessage;
21
22
/**
23
 * @author Marijn Koesen
24
 * @author Herberto Graca <[email protected]>
25
 */
26
class SwiftEmailMapper implements EmailMapper
27
{
28
    public function map(Email $message): Swift_Mime_SimpleMessage
29
    {
30
        // Symfony uses a library called SwiftMailer to send emails. That's why
31
        // email messages are created instantiating a Swift_Message class.
32
        // See https://symfony.com/doc/current/email.html#sending-emails
33
        $swiftMessage = new Swift_Message($message->getSubject());
34
35
        $swiftMessage->setFrom($message->getFrom()->getEmail(), $message->getFrom()->getName());
36
37
        foreach ($message->getTo() as $toReceiver) {
38
            $swiftMessage->addTo($toReceiver->getEmail(), $toReceiver->getName());
39
        }
40
        foreach ($message->getCc() as $ccReceiver) {
41
            $swiftMessage->addCc($ccReceiver->getEmail(), $ccReceiver->getName());
42
        }
43
        foreach ($message->getBcc() as $bccReceiver) {
44
            $swiftMessage->addBcc($bccReceiver->getEmail(), $bccReceiver->getName());
45
        }
46
47
        foreach ($message->getParts() as $part) {
48
            $swiftMessage->addPart($part->getContent(), $part->getContentType(), $part->getCharset());
49
        }
50
51
        foreach ($message->getHeaders() as $header) {
52
            $swiftMessage->getHeaders()->addTextHeader($header->getName(), $header->getValue());
53
        }
54
55
        foreach ($message->getAttachments() as $attachment) {
56
            $swiftMessage->attach(new Swift_Attachment(
57
                $attachment->getContent(), $attachment->getFileName(), $attachment->getContentType()
58
            ));
59
        }
60
61
        return $swiftMessage;
62
    }
63
}
64