Passed
Pull Request — master (#72)
by Daniel
05:33
created

UserMailer::sendChangeEmailConfirmationEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Helper\User;
15
16
use Psr\Container\ContainerInterface;
17
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
18
use Silverback\ApiComponentsBundle\Exception\MailerTransportException;
19
use Silverback\ApiComponentsBundle\Factory\User\Mailer\ChangeEmailConfirmationEmailFactory;
20
use Silverback\ApiComponentsBundle\Factory\User\Mailer\PasswordChangedEmailFactory;
21
use Silverback\ApiComponentsBundle\Factory\User\Mailer\PasswordResetEmailFactory;
22
use Silverback\ApiComponentsBundle\Factory\User\Mailer\UserEnabledEmailFactory;
23
use Silverback\ApiComponentsBundle\Factory\User\Mailer\UsernameChangedEmailFactory;
24
use Silverback\ApiComponentsBundle\Factory\User\Mailer\VerifyEmailFactory;
25
use Silverback\ApiComponentsBundle\Factory\User\Mailer\WelcomeEmailFactory;
26
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
27
use Symfony\Component\Mailer\MailerInterface;
28
use Symfony\Component\Mime\RawMessage;
29
use Symfony\Contracts\Service\ServiceSubscriberInterface;
30
31
/**
32
 * @author Daniel West <[email protected]>
33
 */
34
class UserMailer implements ServiceSubscriberInterface
35
{
36
    private MailerInterface $mailer;
37
    private ContainerInterface $container;
38
    private array $context;
39
40 10
    public function __construct(MailerInterface $mailer, ContainerInterface $container, array $context = [])
41
    {
42 10
        $this->mailer = $mailer;
43 10
        $this->container = $container;
44 10
        $this->context = $context;
45 10
    }
46
47 2
    public static function getSubscribedServices(): array
48
    {
49
        return [
50 2
            PasswordResetEmailFactory::class,
51
            ChangeEmailConfirmationEmailFactory::class,
52
            WelcomeEmailFactory::class,
53
            UserEnabledEmailFactory::class,
54
            UsernameChangedEmailFactory::class,
55
            PasswordChangedEmailFactory::class,
56
            VerifyEmailFactory::class,
57
        ];
58
    }
59
60 4
    public function sendPasswordResetEmail(AbstractUser $user): void
61
    {
62 4
        $email = $this->container->get(PasswordResetEmailFactory::class)->create($user, $this->context);
63 4
        $this->send($email);
64 3
    }
65
66 1
    public function sendChangeEmailConfirmationEmail(AbstractUser $user): void
67
    {
68 1
        $email = $this->container->get(ChangeEmailConfirmationEmailFactory::class)->create($user, $this->context);
69 1
        $this->send($email);
70 1
    }
71
72
    public function sendEmailVerifyEmail(AbstractUser $user): void
73
    {
74
        $email = $this->container->get(VerifyEmailFactory::class)->create($user, $this->context);
75
        $this->send($email);
76
    }
77
78 1
    public function sendWelcomeEmail(AbstractUser $user): void
79
    {
80 1
        $email = $this->container->get(WelcomeEmailFactory::class)->create($user, $this->context);
81 1
        $this->send($email);
82 1
    }
83
84 1
    public function sendUserEnabledEmail(AbstractUser $user): void
85
    {
86 1
        $email = $this->container->get(UserEnabledEmailFactory::class)->create($user, $this->context);
87 1
        $this->send($email);
88 1
    }
89
90 1
    public function sendUsernameChangedEmail(AbstractUser $user): void
91
    {
92 1
        $email = $this->container->get(UsernameChangedEmailFactory::class)->create($user, $this->context);
93 1
        $this->send($email);
94 1
    }
95
96 1
    public function sendPasswordChangedEmail(AbstractUser $user): void
97
    {
98 1
        $email = $this->container->get(PasswordChangedEmailFactory::class)->create($user, $this->context);
99 1
        $this->send($email);
100 1
    }
101
102 9
    private function send(?RawMessage $message): void
103
    {
104 9
        if (null === $message) {
105 2
            return;
106
        }
107
108
        try {
109 7
            $this->mailer->send($message);
110 1
        } catch (TransportExceptionInterface $exception) {
111 1
            $exception = new MailerTransportException($exception->getMessage());
112 1
            $exception->appendDebug($exception->getDebug());
113 1
            throw $exception;
114
        }
115 6
    }
116
}
117