RegistrationMailSender   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 48
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sendRegisterVerificationToken() 0 20 1
A __construct() 0 12 1
1
<?php
2
3
namespace App\Module\Authentication\Register\Service;
4
5
use App\Infrastructure\Locale\LocaleConfigurator;
6
use App\Infrastructure\Settings\Settings;
7
use App\Module\Mail\Service\Mailer;
0 ignored issues
show
Bug introduced by
The type App\Module\Mail\Service\Mailer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
9
use Symfony\Component\Mime\Address;
10
use Symfony\Component\Mime\Email;
11
12
/**
13
 * This class manages the emails sent in relation to registration.
14
 *
15
 * This is a centralized Service class containing email content and sending action.
16
 * Advantages (compared to having the email content and sending action in the service class that calls this class):
17
 *  - Prevents other service classes from having the email responsibility (Single-responsibility principle)
18
 *  - Contents are centralized and can be changed easier without having to search them all over the code
19
 */
20
class RegistrationMailSender
21
{
22
    private Email $email;
23
24 9
    public function __construct(
25
        private readonly Mailer $mailer,
26
        private readonly LocaleConfigurator $localeConfigurator,
27
        Settings $settings,
28
    ) {
29 9
        $settings = $settings->get('public')['email'];
30
        // Create email object
31 9
        $this->email = new Email();
32
        // Send auth emails from domain
33 9
        $this->email->from(new Address($settings['main_sender_address'], $settings['main_sender_name']))->replyTo(
34 9
            $settings['main_contact_email']
35 9
        )->priority(Email::PRIORITY_HIGH);
36
    }
37
38
    /**
39
     * Send verification token.
40
     *
41
     * @param string $email
42
     * @param string $fullName
43
     * @param string $language
44
     * @param array $queryParams
45
     *
46
     * @throws TransportExceptionInterface
47
     */
48 2
    public function sendRegisterVerificationToken(string $email, string $fullName, string $language, array $queryParams): void
49
    {
50
        // Change language to one the user is being registered with
51 2
        $originalLocale = setlocale(LC_ALL, 0);
52 2
        $this->localeConfigurator->setLanguage($language);
53
54
        // Send verification mail in the language that was selected for the user
55 2
        $this->email->subject(__('Account created'))
56 2
            ->html(
57 2
                $this->mailer->getContentFromTemplate(
58 2
                    'authentication/email/' . $this->localeConfigurator->getLanguageCodeForPath() .
59 2
                    'new-account.email.php',
60 2
                    ['userFullName' => $fullName, 'queryParams' => $queryParams]
61 2
                )
62 2
            )->to(new Address($email, $fullName));
63
        // Send email
64 2
        $this->mailer->send($this->email);
65
66
        // Reset locale
67 2
        $this->localeConfigurator->setLanguage($originalLocale);
68
    }
69
}
70