Completed
Push — feature/second-factor-revocati... ( a5170e )
by A.
13:46
created

sendVettedSecondFactorRevokedByRaEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 33

Duplication

Lines 48
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 48
loc 48
rs 9.125
cc 1
eloc 33
nc 1
nop 5
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Service;
20
21
use Assert\Assertion;
22
use Surfnet\Stepup\Identity\Value\CommonName;
23
use Surfnet\Stepup\Identity\Value\Email;
24
use Surfnet\Stepup\Identity\Value\Locale;
25
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifier;
26
use Surfnet\StepupBundle\Value\SecondFactorType;
27
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\RaLocation;
28
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\RegistrationAuthorityCredentials;
29
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Service\EmailTemplateService;
30
use Surfnet\StepupMiddleware\CommandHandlingBundle\Value\Sender;
31
use Swift_Mailer as Mailer;
32
use Swift_Message as Message;
33
use Symfony\Component\Templating\EngineInterface;
34
use Symfony\Component\Translation\TranslatorInterface;
35
36
/**
37
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
38
 */
39
final class SecondFactorMailService
40
{
41
    /**
42
     * @var Mailer
43
     */
44
    private $mailer;
45
46
    /**
47
     * @var Sender
48
     */
49
    private $sender;
50
51
    /**
52
     * @var TranslatorInterface
53
     */
54
    private $translator;
55
56
    /**
57
     * @var EngineInterface
58
     */
59
    private $templateEngine;
60
61
    /**
62
     * @var string
63
     */
64
    private $emailVerificationUrlTemplate;
65
66
    /**
67
     * @var \Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Service\EmailTemplateService
68
     */
69
    private $emailTemplateService;
70
71
    /**
72
     * @var string
73
     */
74
    private $fallbackLocale;
75
76
    /**
77
     * @var string
78
     */
79
    private $selfServiceUrl;
80
81
    /**
82
     * @param Mailer $mailer
83
     * @param Sender $sender
84
     * @param TranslatorInterface $translator
85
     * @param EngineInterface $templateEngine
86
     * @param string $emailVerificationUrlTemplate
87
     * @param EmailTemplateService $emailTemplateService
88
     * @param string $fallbackLocale
89
     * @param string $selfServiceUrl
90
     */
91
    public function __construct(
92
        Mailer $mailer,
93
        Sender $sender,
94
        TranslatorInterface $translator,
95
        EngineInterface $templateEngine,
96
        $emailVerificationUrlTemplate,
97
        EmailTemplateService $emailTemplateService,
98
        $fallbackLocale,
99
        $selfServiceUrl
100
    ) {
101
        Assertion::string($fallbackLocale);
102
        Assertion::string($selfServiceUrl);
103
104
        $this->mailer = $mailer;
105
        $this->sender = $sender;
106
        $this->translator = $translator;
107
        $this->templateEngine = $templateEngine;
108
        $this->emailVerificationUrlTemplate = $emailVerificationUrlTemplate;
109
        $this->emailTemplateService = $emailTemplateService;
110
        $this->fallbackLocale = $fallbackLocale;
111
        $this->selfServiceUrl = $selfServiceUrl;
112
    }
113
114
    /**
115
     * @param string $locale
116
     * @param string $commonName
117
     * @param string $email
118
     * @param string $verificationNonce
119
     */
120
    public function sendEmailVerificationEmail(
121
        $locale,
122
        $commonName,
123
        $email,
124
        $verificationNonce
125
    ) {
126
        $subject = $this->translator->trans(
127
            'ss.mail.email_verification_email.subject',
128
            ['%commonName%' => $commonName],
129
            'messages',
130
            $locale
131
        );
132
133
        $verificationUrl = str_replace(
134
            '{nonce}',
135
            urlencode($verificationNonce),
136
            $this->emailVerificationUrlTemplate
137
        );
138
        $emailTemplate = $this->emailTemplateService->findByName('confirm_email', $locale, $this->fallbackLocale);
139
140
        $parameters = [
141
            'templateString'   => $emailTemplate->htmlContent,
142
            'locale'           => $locale,
143
            'commonName'       => $commonName,
144
            'email'            => $email,
145
            'verificationUrl'  => $verificationUrl
146
        ];
147
148
        // Rendering file template instead of string
149
        // (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248)
150
        $body = $this->templateEngine->render(
151
            'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig',
152
            $parameters
153
        );
154
155
        /** @var Message $message */
156
        $message = $this->mailer->createMessage();
157
        $message
158
            ->setFrom($this->sender->getEmail(), $this->sender->getName())
159
            ->addTo($email, $commonName)
160
            ->setSubject($subject)
161
            ->setBody($body, 'text/html', 'utf-8');
162
163
        $this->mailer->send($message);
164
    }
165
166
    /**
167
     * @param string $locale
168
     * @param string $commonName
169
     * @param string $email
170
     * @param string $registrationCode
171
     * @param RegistrationAuthorityCredentials[] $ras
172
     */
173 View Code Duplication
    public function sendRegistrationEmailWithRas(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
        $locale,
175
        $commonName,
176
        $email,
177
        $registrationCode,
178
        array $ras
179
    ) {
180
        $subject = $this->translator->trans(
181
            'ss.mail.registration_email.subject',
182
            ['%commonName%' => $commonName],
183
            'messages',
184
            $locale
185
        );
186
187
        $emailTemplate = $this->emailTemplateService->findByName(
188
            'registration_code_with_ras',
189
            $locale,
190
            $this->fallbackLocale
191
        );
192
193
        $parameters = [
194
            'templateString'   => $emailTemplate->htmlContent,
195
            'locale'           => $locale,
196
            'commonName'       => $commonName,
197
            'email'            => $email,
198
            'registrationCode' => $registrationCode,
199
            'ras'              => $ras,
200
        ];
201
202
        // Rendering file template instead of string
203
        // (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248)
204
        $body = $this->templateEngine->render(
205
            'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig',
206
            $parameters
207
        );
208
209
        /** @var Message $message */
210
        $message = $this->mailer->createMessage();
211
        $message
212
            ->setFrom($this->sender->getEmail(), $this->sender->getName())
213
            ->addTo($email, $commonName)
214
            ->setSubject($subject)
215
            ->setBody($body, 'text/html', 'utf-8');
216
217
        $this->mailer->send($message);
218
    }
219
220
    /**
221
     * @param string $locale
222
     * @param string $commonName
223
     * @param string $email
224
     * @param string $registrationCode
225
     * @param RaLocation[] $raLocations
226
     */
227 View Code Duplication
    public function sendRegistrationEmailWithRaLocations(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
228
        $locale,
229
        $commonName,
230
        $email,
231
        $registrationCode,
232
        array $raLocations
233
    ) {
234
        $subject = $this->translator->trans(
235
            'ss.mail.registration_email.subject',
236
            ['%commonName%' => $commonName],
237
            'messages',
238
            $locale
239
        );
240
241
        $emailTemplate = $this->emailTemplateService->findByName(
242
            'registration_code_with_ra_locations',
243
            $locale,
244
            $this->fallbackLocale
245
        );
246
247
        $parameters = [
248
            'templateString'   => $emailTemplate->htmlContent,
249
            'locale'           => $locale,
250
            'commonName'       => $commonName,
251
            'email'            => $email,
252
            'registrationCode' => $registrationCode,
253
            'raLocations'      => $raLocations,
254
        ];
255
256
        // Rendering file template instead of string
257
        // (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248)
258
        $body = $this->templateEngine->render(
259
            'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig',
260
            $parameters
261
        );
262
263
        /** @var Message $message */
264
        $message = $this->mailer->createMessage();
265
        $message
266
            ->setFrom($this->sender->getEmail(), $this->sender->getName())
267
            ->addTo($email, $commonName)
268
            ->setSubject($subject)
269
            ->setBody($body, 'text/html', 'utf-8');
270
271
        $this->mailer->send($message);
272
    }
273
274
    /**
275
     * @param Locale     $locale
276
     * @param CommonName $commonName
277
     * @param Email      $email
278
     */
279
    public function sendVettedEmail(
280
        Locale $locale,
281
        CommonName $commonName,
282
        Email $email
283
    ) {
284
        $subject = $this->translator->trans(
285
            'ss.mail.vetted_email.subject',
286
            ['%commonName%' => $commonName->getCommonName(), '%email%' => $email->getEmail()],
287
            'messages',
288
            $locale->getLocale()
289
        );
290
291
        $emailTemplate = $this->emailTemplateService->findByName('vetted', $locale->getLocale(), $this->fallbackLocale);
292
        $parameters = [
293
            'templateString'   => $emailTemplate->htmlContent,
294
            'locale'           => $locale->getLocale(),
295
            'commonName'       => $commonName->getCommonName(),
296
            'email'            => $email->getEmail(),
297
        ];
298
299
        // Rendering file template instead of string
300
        // (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248)
301
        $body = $this->templateEngine->render(
302
            'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig',
303
            $parameters
304
        );
305
306
        /** @var Message $message */
307
        $message = $this->mailer->createMessage();
308
        $message
309
            ->setFrom($this->sender->getEmail(), $this->sender->getName())
310
            ->addTo($email->getEmail(), $commonName->getCommonName())
311
            ->setSubject($subject)
312
            ->setBody($body, 'text/html', 'utf-8');
313
314
        $this->mailer->send($message);
315
    }
316
317
    /**
318
     * @param Locale $locale
319
     * @param CommonName $commonName
320
     * @param Email $email
321
     * @param SecondFactorType $secondFactorType
322
     * @param SecondFactorIdentifier $secondFactorIdentifier
323
     * @throws \Exception
324
     */
325 View Code Duplication
    public function sendVettedSecondFactorRevokedByRaEmail(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
326
        Locale $locale,
327
        CommonName $commonName,
328
        Email $email,
329
        SecondFactorType $secondFactorType,
330
        SecondFactorIdentifier $secondFactorIdentifier
331
    ) {
332
        $subject = $this->translator->trans(
333
            'mw.mail.second_factor_revoked.subject',
334
            [
335
                '%tokenType%' => $secondFactorType
336
            ],
337
            'messages',
338
            $locale->getLocale()
339
        );
340
341
        $emailTemplate = $this->emailTemplateService->findByName(
342
            'second_factor_revoked',
343
            $locale->getLocale(),
344
            $this->fallbackLocale
345
        );
346
        $parameters = [
347
            'isRevokedByRa'   => true,
348
            'templateString'  => $emailTemplate->htmlContent,
349
            'commonName'      => $commonName->getCommonName(),
350
            'tokenType'       => $secondFactorType->getSecondFactorType(),
351
            'tokenIdentifier' => $secondFactorIdentifier->getValue(),
352
            'selfServiceUrl'  => $this->selfServiceUrl,
353
            'locale'          => $locale->getLocale(),
354
        ];
355
356
        // Rendering file template instead of string
357
        // (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248)
358
        $body = $this->templateEngine->render(
359
            'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig',
360
            $parameters
361
        );
362
363
        /** @var Message $message */
364
        $message = $this->mailer->createMessage();
365
        $message
366
            ->setFrom($this->sender->getEmail(), $this->sender->getName())
367
            ->addTo($email->getEmail(), $commonName->getCommonName())
368
            ->setSubject($subject)
369
            ->setBody($body, 'text/html', 'utf-8');
370
371
        $this->mailer->send($message);
372
    }
373
374
    /**
375
     * @param Locale $locale
376
     * @param CommonName $commonName
377
     * @param Email $email
378
     * @param SecondFactorType $secondFactorType
379
     * @param SecondFactorIdentifier $secondFactorIdentifier
380
     */
381 View Code Duplication
    public function sendVettedSecondFactorRevokedBySelfEmail(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
382
        Locale $locale,
383
        CommonName $commonName,
384
        Email $email,
385
        SecondFactorType $secondFactorType,
386
        SecondFactorIdentifier $secondFactorIdentifier
387
    ) {
388
        $subject = $this->translator->trans(
389
            'mw.mail.second_factor_revoked.subject',
390
            [
391
                '%tokenType%' => $secondFactorType
392
            ],
393
            'messages',
394
            $locale->getLocale()
395
        );
396
397
        $emailTemplate = $this->emailTemplateService->findByName(
398
            'second_factor_revoked',
399
            $locale->getLocale(),
400
            $this->fallbackLocale
401
        );
402
        $parameters = [
403
            'isRevokedByRa'   => false,
404
            'templateString'  => $emailTemplate->htmlContent,
405
            'commonName'      => $commonName->getCommonName(),
406
            'tokenType'       => $secondFactorType->getSecondFactorType(),
407
            'tokenIdentifier' => $secondFactorIdentifier->getValue(),
408
            'selfServiceUrl'  => $this->selfServiceUrl,
409
            'locale'          => $locale->getLocale(),
410
        ];
411
412
        // Rendering file template instead of string
413
        // (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248)
414
        $body = $this->templateEngine->render(
415
            'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig',
416
            $parameters
417
        );
418
419
        /** @var Message $message */
420
        $message = $this->mailer->createMessage();
421
        $message
422
            ->setFrom($this->sender->getEmail(), $this->sender->getName())
423
            ->addTo($email->getEmail(), $commonName->getCommonName())
424
            ->setSubject($subject)
425
            ->setBody($body, 'text/html', 'utf-8');
426
427
        $this->mailer->send($message);
428
    }
429
}
430