Test Failed
Pull Request — feature/unit-tests (#37)
by Daniel
09:37 queued 03:47
created

UserMailer::sendWelcomeEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component 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\ApiComponentBundle\Mailer;
15
16
use Psr\Container\ContainerInterface;
17
use Silverback\ApiComponentBundle\Entity\User\AbstractUser;
18
use Silverback\ApiComponentBundle\Exception\MailerTransportException;
19
use Silverback\ApiComponentBundle\Factory\Mailer\User\ChangeEmailVerificationEmailFactory;
20
use Silverback\ApiComponentBundle\Factory\Mailer\User\PasswordChangedEmailFactory;
21
use Silverback\ApiComponentBundle\Factory\Mailer\User\PasswordResetEmailFactory;
22
use Silverback\ApiComponentBundle\Factory\Mailer\User\UserEnabledEmailFactory;
23
use Silverback\ApiComponentBundle\Factory\Mailer\User\UsernameChangedEmailFactory;
24
use Silverback\ApiComponentBundle\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(
40
        MailerInterface $mailer,
41
        ContainerInterface $container,
42
        array $context = []
43
    ) {
44 15
        $this->mailer = $mailer;
45
        $this->container = $container;
46
        $this->context = $context;
47
    }
48
49
    public static function getSubscribedServices(): array
50
    {
51
        return [
52
            PasswordResetEmailFactory::class,
53
            ChangeEmailVerificationEmailFactory::class,
54
            WelcomeEmailFactory::class,
55
            UserEnabledEmailFactory::class,
56 15
            UsernameChangedEmailFactory::class,
57 15
            PasswordChangedEmailFactory::class,
58 15
        ];
59 15
    }
60 15
61 15
    public function sendPasswordResetEmail(AbstractUser $user): void
62 15
    {
63 15
        $email = $this->container->get(PasswordResetEmailFactory::class)->create($user, $this->context);
64 15
        $this->send($email);
65 15
    }
66 15
67
    public function sendChangeEmailVerificationEmail(AbstractUser $user): void
68 7
    {
69
        $email = $this->container->get(ChangeEmailVerificationEmailFactory::class)->create($user, $this->context);
70 7
        $this->send($email);
71 7
    }
72 1
73
    public function sendWelcomeEmail(AbstractUser $user): void
74
    {
75 6
        $email = $this->container->get(WelcomeEmailFactory::class)->create($user, $this->context);
76 5
        $this->send($email);
77 5
    }
78
79 5
    public function sendUserEnabledEmail(AbstractUser $user): void
80 5
    {
81
        $email = $this->container->get(UserEnabledEmailFactory::class)->create($user, $this->context);
82 5
        $this->send($email);
83 5
    }
84 5
85
    public function sendUsernameChangedEmail(AbstractUser $user): void
86
    {
87 5
        $email = $this->container->get(UsernameChangedEmailFactory::class)->create($user, $this->context);
88
        $this->send($email);
89
    }
90 3
91 2
    public function sendPasswordChangedEmail(AbstractUser $user): void
92
    {
93 3
        $email = $this->container->get(PasswordChangedEmailFactory::class)->create($user, $this->context);
94
        $this->send($email);
95 3
    }
96 2
97 1
    private function send(?RawMessage $message): void
98 1
    {
99 1
        if (null === $message) {
100
            return;
101
        }
102 1
103
        try {
104
            $this->mailer->send($message);
105 1
        } catch (TransportExceptionInterface $exception) {
106 1
            $exception = new MailerTransportException($exception->getMessage());
107
            $exception->appendDebug($exception->getDebug());
108 5
            throw $exception;
109
        }
110 5
    }
111
}
112