Test Failed
Push — master ( 61801d...f3c992 )
by Daniel
11:05
created

UserMailer::sendPasswordResetEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 1
cts 1
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
30
/**
31
 * @author Daniel West <[email protected]>
32
 */
33
class UserMailer
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 10
    {
41
        $this->mailer = $mailer;
42 10
        $this->container = $container;
43 10
        $this->context = $context;
44 10
    }
45 10
46
    public function sendPasswordResetEmail(AbstractUser $user): void
47 2
    {
48
        $email = $this->container->get(PasswordResetEmailFactory::class)->create($user, $this->context);
49
        $this->send($email);
50 2
    }
51
52
    public function sendChangeEmailConfirmationEmail(AbstractUser $user): void
53
    {
54
        $email = $this->container->get(ChangeEmailConfirmationEmailFactory::class)->create($user, $this->context);
55
        $this->send($email);
56
    }
57
58
    public function sendEmailVerifyEmail(AbstractUser $user): void
59
    {
60 4
        $email = $this->container->get(VerifyEmailFactory::class)->create($user, $this->context);
61
        $this->send($email);
62 4
    }
63 4
64 3
    public function sendWelcomeEmail(AbstractUser $user): void
65
    {
66 1
        $email = $this->container->get(WelcomeEmailFactory::class)->create($user, $this->context);
67
        $this->send($email);
68 1
    }
69 1
70 1
    public function sendUserEnabledEmail(AbstractUser $user): void
71
    {
72
        $email = $this->container->get(UserEnabledEmailFactory::class)->create($user, $this->context);
73
        $this->send($email);
74
    }
75
76
    public function sendUsernameChangedEmail(AbstractUser $user): void
77
    {
78 1
        $email = $this->container->get(UsernameChangedEmailFactory::class)->create($user, $this->context);
79
        $this->send($email);
80 1
    }
81 1
82 1
    public function sendPasswordChangedEmail(AbstractUser $user): void
83
    {
84 1
        $email = $this->container->get(PasswordChangedEmailFactory::class)->create($user, $this->context);
85
        $this->send($email);
86 1
    }
87 1
88 1
    private function send(?RawMessage $message): void
89
    {
90 1
        if (null === $message) {
91
            return;
92 1
        }
93 1
94 1
        try {
95
            $this->mailer->send($message);
96 1
        } catch (TransportExceptionInterface $exception) {
97
            $exception = new MailerTransportException($exception->getMessage());
98 1
            $exception->appendDebug($exception->getDebug());
99 1
            throw $exception;
100 1
        }
101
    }
102
}
103