SwiftMailerUserMailer::__construct()   A
last analyzed

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
/*
4
 * This file is part of the BenGorUser package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorUser\SwiftMailerBridge\Infrastructure\Mailing;
14
15
use BenGorUser\User\Domain\Model\UserEmail;
16
use BenGorUser\User\Domain\Model\UserMailable;
17
use BenGorUser\User\Domain\Model\UserMailer;
18
19
/**
20
 * SwiftMailer user mailer class.
21
 *
22
 * @author Beñat Espiña <[email protected]>
23
 * @author Gorka Laucirica <[email protected]>
24
 */
25
final class SwiftMailerUserMailer implements UserMailer
26
{
27
    /**
28
     * The swift mailer instance.
29
     *
30
     * @var \Swift_Mailer
31
     */
32
    private $swiftMailer;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param \Swift_Mailer $swiftMailer The swift mailer instance
38
     */
39
    public function __construct(\Swift_Mailer $swiftMailer)
40
    {
41
        $this->swiftMailer = $swiftMailer;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function mail(UserMailable $mail)
48
    {
49
        if (is_array($mail->to())) {
50
            $to = array_map(function (UserEmail $receiver) {
51
                return $receiver->email();
52
            }, $mail->to());
53
        } else {
54
            $to = $mail->to()->email();
55
        }
56
57
        $message = \Swift_Message::newInstance()
58
            ->setSubject($mail->subject())
59
            ->setFrom($mail->from()->email())
60
            ->setTo($to)
61
            ->setBody($mail->bodyText(), 'text/plain')
62
            ->addPart($mail->bodyHtml(), 'text/html');
63
64
        $this->swiftMailer->send($message);
65
    }
66
}
67