SwiftMailer   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A send() 0 17 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;
16
17
use Acme\App\Core\Port\Notification\Client\Email\Email;
18
use Acme\App\Core\Port\Notification\Client\Email\EmailerInterface;
19
use Acme\App\Infrastructure\Notification\Client\Email\SwiftMailer\Mapper\EmailMapper;
20
use Psr\Log\LoggerInterface;
21
use Psr\Log\NullLogger;
22
use Swift_Mailer;
23
24
/**
25
 * @author Marijn Koesen
26
 * @author Kasper Agg
27
 * @author Herberto Graca <[email protected]>
28
 */
29
class SwiftMailer implements EmailerInterface
30
{
31
    /**
32
     * @var Swift_Mailer
33
     */
34
    private $mailer;
35
36
    /**
37
     * @var EmailMapper
38
     */
39
    private $swiftEmailMapper;
40
41
    /**
42
     * @var LoggerInterface
43
     */
44
    private $logger;
45
46
    public function __construct(Swift_Mailer $mailer, EmailMapper $swiftEmailMapper, LoggerInterface $logger = null)
47
    {
48
        $this->mailer = $mailer;
49
        $this->swiftEmailMapper = $swiftEmailMapper;
50
        $this->logger = $logger ?? new NullLogger();
51
    }
52
53
    public function send(Email $email): void
54
    {
55
        // Symfony uses a library called SwiftMailer to send emails. That's why
56
        // we need to map from our Email class to a Swift_Message class.
57
        // See https://symfony.com/doc/current/email.html#sending-emails
58
        $swiftMessage = $this->swiftEmailMapper->map($email);
59
60
        // In config/packages/dev/swiftmailer.yaml the 'disable_delivery' option is set to 'true'.
61
        // That's why in the development environment you won't actually receive any email.
62
        // However, you can inspect the contents of those unsent emails using the debug toolbar.
63
        // See https://symfony.com/doc/current/email/dev_environment.html#viewing-from-the-web-debug-toolbar
64
        $this->mailer->send($swiftMessage);
65
66
        $this->logger->info(
67
            'Sent email with subject "' . $email->getSubject() . '" to ' . implode(', ', $email->getTo())
68
        );
69
    }
70
}
71