Test Failed
Pull Request — master (#67)
by Daniel
10:04 queued 04:16
created

UserMailer::sendPasswordChangedEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
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\Mailer\User\ChangeEmailVerificationEmailFactory;
20
use Silverback\ApiComponentsBundle\Factory\Mailer\User\PasswordChangedEmailFactory;
21
use Silverback\ApiComponentsBundle\Factory\Mailer\User\PasswordResetEmailFactory;
22
use Silverback\ApiComponentsBundle\Factory\Mailer\User\UserEnabledEmailFactory;
23
use Silverback\ApiComponentsBundle\Factory\Mailer\User\UsernameChangedEmailFactory;
24
use Silverback\ApiComponentsBundle\Factory\Mailer\User\WelcomeEmailFactory;
25
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
26
use Symfony\Component\Mailer\MailerInterface;
27
use Symfony\Component\Mime\RawMessage;
28
use Symfony\Contracts\Service\ServiceSubscriberInterface;
29
30
/**
31
 * @author Daniel West <[email protected]>
32
 */
33
class UserMailer implements ServiceSubscriberInterface
34
{
35
    private MailerInterface $mailer;
36
    private ContainerInterface $container;
37
    private array $context;
38
39
    public function __construct(MailerInterface $mailer, ContainerInterface $container, array $context = [])
40
    {
41
        $this->mailer = $mailer;
42
        $this->container = $container;
43
        $this->context = $context;
44
    }
45
46
    public static function getSubscribedServices(): array
47
    {
48
        return [
49
            PasswordResetEmailFactory::class,
50
            ChangeEmailVerificationEmailFactory::class,
51
            WelcomeEmailFactory::class,
52
            UserEnabledEmailFactory::class,
53
            UsernameChangedEmailFactory::class,
54
            PasswordChangedEmailFactory::class,
55
        ];
56
    }
57
58
    public function sendPasswordResetEmail(AbstractUser $user): void
59
    {
60
        $email = $this->container->get(PasswordResetEmailFactory::class)->create($user, $this->context);
61
        $this->send($email);
62
    }
63
64
    public function sendChangeEmailVerificationEmail(AbstractUser $user): void
65
    {
66
        $email = $this->container->get(ChangeEmailVerificationEmailFactory::class)->create($user, $this->context);
67
        $this->send($email);
68
    }
69
70
    public function sendWelcomeEmail(AbstractUser $user): void
71
    {
72
        $email = $this->container->get(WelcomeEmailFactory::class)->create($user, $this->context);
73
        $this->send($email);
74
    }
75
76
    public function sendUserEnabledEmail(AbstractUser $user): void
77
    {
78
        $email = $this->container->get(UserEnabledEmailFactory::class)->create($user, $this->context);
79
        $this->send($email);
80
    }
81
82
    public function sendUsernameChangedEmail(AbstractUser $user): void
83
    {
84
        $email = $this->container->get(UsernameChangedEmailFactory::class)->create($user, $this->context);
85
        $this->send($email);
86
    }
87
88
    public function sendPasswordChangedEmail(AbstractUser $user): void
89
    {
90
        $email = $this->container->get(PasswordChangedEmailFactory::class)->create($user, $this->context);
91
        $this->send($email);
92
    }
93
94
    private function send(?RawMessage $message): void
95
    {
96
        if (null === $message) {
97
            return;
98
        }
99
100
        try {
101
            $this->mailer->send($message);
102
        } catch (TransportExceptionInterface $exception) {
103
            $exception = new MailerTransportException($exception->getMessage());
104
            $exception->appendDebug($exception->getDebug());
105
            throw $exception;
106
        }
107
    }
108
}
109