Passed
Push — master ( d274c4...2117c8 )
by Hirofumi
137:16 queued 122:16
created

SwiftMailerSendEmail::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 9.9666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shippinno\Email\SwiftMailer;
5
6
use Psr\Log\LoggerInterface;
7
use Shippinno\Email\EmailNotSentException;
8
use Shippinno\Email\SendEmail;
9
use Shippinno\Email\SmtpConfiguration;
10
use Swift_Attachment;
11
use Swift_Mailer;
12
use Swift_Message;
13
use Swift_SmtpTransport;
14
use Tanigami\ValueObjects\Web\Email;
15
use Tanigami\ValueObjects\Web\EmailAddress;
16
use Throwable;
17
18
class SwiftMailerSendEmail extends SendEmail
19
{
20
    /**
21
     * @var Swift_Mailer
22
     */
23
    private $defaultMailer;
24
25
    /**
26
     * @var bool
27
     */
28
    private $ignoresSmtpConfiguration;
29
30
    /**
31
     * @param Swift_Mailer $defaultMailer
32
     * @param bool $ignoresSmtpConfiguration
33
     * @param LoggerInterface|null $logger
34
     */
35 3
    public function __construct(
36
        Swift_Mailer $defaultMailer,
37
        bool $ignoresSmtpConfiguration = false,
38
        LoggerInterface $logger = null
39
    ) {
40 3
        $this->defaultMailer = $defaultMailer;
41 3
        $this->ignoresSmtpConfiguration = $ignoresSmtpConfiguration;
42 3
        parent::__construct($logger);
43 3
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 2
    protected function doExecute(Email $email, SmtpConfiguration $smtpConfiguration = null): int
49
    {
50 2
        $mailer = $this->defaultMailer;
51 2
        if (!$this->ignoresSmtpConfiguration && !is_null($smtpConfiguration)) {
52
            $mailer = $this->smtpConfiguredMailer($smtpConfiguration);
53
        }
54 2
        $message = (new Swift_Message)
55 2
            ->setSubject($email->subject())
56 2
            ->setFrom($email->from()->emailAddress())
57 2
            ->setBody($email->body(), 'text/plain')
58
            ->setTo(array_map(function (EmailAddress $emailAddress) {
59 2
                return $emailAddress->emailAddress();
60 2
            }, $email->tos()))
61
            ->setCc(array_map(function (EmailAddress $emailAddress) {
62 1
                return $emailAddress->emailAddress();
63 2
            }, $email->ccs()))
64
            ->setBcc(array_map(function (EmailAddress $emailAddress) {
65 1
                return $emailAddress->emailAddress();
66 2
            }, $email->bccs()));
67 2
        foreach ($email->attachments() as $attachment) {
68 1
            $message->attach(
69 1
                new Swift_Attachment(
70 1
                    $attachment->content(),
71 1
                    $attachment->fileName(),
72 1
                    $attachment->mimeType()
73
                )
74
            );
75
        }
76 2
        $failedRecipients = [];
77
        try {
78 2
            $sent = $mailer->send($message, $failedRecipients);
79 1
            if ($sent !== $this->countRecipientsOfEmail($email)) {
80
                throw new EmailNotSentException($failedRecipients);
81
            }
82 1
            $this->logger->debug('An email was successfully sent.', [
83 1
                'to' => implode(', ', $message->getTo()),
84 1
                'subject' => $message->getSubject(),
85 1
                'body' => $message->getBody(),
86
            ]);
87 1
        } catch (Throwable $e) {
88 1
            throw new EmailNotSentException($failedRecipients, $e);
89
        }
90
91 1
        return $sent;
92
    }
93
94
    /**
95
     * @param Email $email
96
     * @return int
97
     */
98 1
    protected function countRecipientsOfEmail(Email $email): int
99
    {
100 1
        return count($email->tos()) + count($email->ccs()) + count($email->bccs());
101
    }
102
103
    /**
104
     * @param SmtpConfiguration $smtpConfiguration
105
     * @return Swift_Mailer
106
     */
107 1
    protected function smtpConfiguredMailer(SmtpConfiguration $smtpConfiguration): Swift_Mailer
108
    {
109 1
        return new Swift_Mailer(
110 1
            (new Swift_SmtpTransport($smtpConfiguration->host(), $smtpConfiguration->port()))
111 1
                ->setUsername($smtpConfiguration->username())
112 1
                ->setPassword($smtpConfiguration->password())
113
        );
114
    }
115
}
116