1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2016 SURFnet B.V. |
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\StepupMiddleware\CommandHandlingBundle\Configuration\Service\EmailTemplateService; |
23
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Value\Sender; |
24
|
|
|
use Swift_Mailer as Mailer; |
25
|
|
|
use Swift_Message as Message; |
26
|
|
|
use Symfony\Component\Templating\EngineInterface; |
27
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
28
|
|
|
|
29
|
|
|
final class EmailVerificationMailService |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var TranslatorInterface |
33
|
|
|
*/ |
34
|
|
|
private $translator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var EmailTemplateService |
38
|
|
|
*/ |
39
|
|
|
private $emailTemplateService; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $emailVerificationUrlTemplate; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $fallbackLocale; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Mailer |
53
|
|
|
*/ |
54
|
|
|
private $mailer; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Sender |
58
|
|
|
*/ |
59
|
|
|
private $sender; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var EngineInterface |
63
|
|
|
*/ |
64
|
|
|
private $templateEngine; |
65
|
|
|
|
66
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
67
|
|
|
Mailer $mailer, |
68
|
|
|
Sender $sender, |
69
|
|
|
TranslatorInterface $translator, |
70
|
|
|
EngineInterface $templateEngine, |
71
|
|
|
$emailVerificationUrlTemplate, |
72
|
|
|
EmailTemplateService $emailTemplateService, |
73
|
|
|
$fallbackLocale |
74
|
|
|
) { |
75
|
|
|
Assertion::string( |
76
|
|
|
$emailVerificationUrlTemplate, |
77
|
|
|
'Email verification URL template "%s" expected to be string, type %s given' |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$this->mailer = $mailer; |
81
|
|
|
$this->sender = $sender; |
82
|
|
|
$this->translator = $translator; |
83
|
|
|
$this->templateEngine = $templateEngine; |
84
|
|
|
$this->emailVerificationUrlTemplate = $emailVerificationUrlTemplate; |
85
|
|
|
$this->emailTemplateService = $emailTemplateService; |
86
|
|
|
$this->fallbackLocale = $fallbackLocale; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $locale |
91
|
|
|
* @param string $commonName |
92
|
|
|
* @param string $email |
93
|
|
|
* @param string $verificationNonce |
94
|
|
|
*/ |
95
|
|
|
public function sendEmailVerificationEmail( |
96
|
|
|
$locale, |
97
|
|
|
$commonName, |
98
|
|
|
$email, |
99
|
|
|
$verificationNonce |
100
|
|
|
) { |
101
|
|
|
$subject = $this->translator->trans( |
102
|
|
|
'ss.mail.email_verification_email.subject', |
103
|
|
|
['%commonName%' => $commonName], |
104
|
|
|
'messages', |
105
|
|
|
$locale |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$verificationUrl = str_replace( |
109
|
|
|
'{nonce}', |
110
|
|
|
urlencode($verificationNonce), |
111
|
|
|
$this->emailVerificationUrlTemplate |
112
|
|
|
); |
113
|
|
|
$emailTemplate = $this->emailTemplateService->findByName('confirm_email', $locale, $this->fallbackLocale); |
114
|
|
|
|
115
|
|
|
$parameters = [ |
116
|
|
|
'templateString' => $emailTemplate->htmlContent, |
117
|
|
|
'locale' => $locale, |
118
|
|
|
'commonName' => $commonName, |
119
|
|
|
'email' => $email, |
120
|
|
|
'verificationUrl' => $verificationUrl |
121
|
|
|
]; |
122
|
|
|
|
123
|
|
|
// Rendering file template instead of string |
124
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
125
|
|
|
$body = $this->templateEngine->render( |
126
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
127
|
|
|
$parameters |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
/** @var Message $message */ |
131
|
|
|
$message = $this->mailer->createMessage(); |
132
|
|
|
$message |
133
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
134
|
|
|
->addTo($email, $commonName) |
135
|
|
|
->setSubject($subject) |
136
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
137
|
|
|
|
138
|
|
|
$this->mailer->send($message); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
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.