Completed
Pull Request — develop (#202)
by
unknown
03:25
created

sendRegistrationEmailWithRas()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 34

Duplication

Lines 48
Ratio 100 %

Importance

Changes 0
Metric Value
dl 48
loc 48
rs 9.125
c 0
b 0
f 0
cc 1
eloc 34
nc 1
nop 6
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\Stepup\DateTime\DateTime;
23
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\RaLocation;
24
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\RegistrationAuthorityCredentials;
25
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Service\EmailTemplateService;
26
use Surfnet\StepupMiddleware\CommandHandlingBundle\Value\Sender;
27
use Swift_Mailer as Mailer;
28
use Swift_Message as Message;
29
use Symfony\Component\Templating\EngineInterface;
30
use Symfony\Component\Translation\TranslatorInterface;
31
32
final class RegistrationMailService
33
{
34
    /**
35
     * @var Mailer
36
     */
37
    private $mailer;
38
39
    /**
40
     * @var Sender
41
     */
42
    private $sender;
43
44
    /**
45
     * @var TranslatorInterface
46
     */
47
    private $translator;
48
49
    /**
50
     * @var EngineInterface
51
     */
52
    private $templateEngine;
53
54
    /**
55
     * @var \Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Service\EmailTemplateService
56
     */
57
    private $emailTemplateService;
58
59
    /**
60
     * @var string
61
     */
62
    private $fallbackLocale;
63
64
    /**
65
     * @param Mailer $mailer
66
     * @param Sender $sender
67
     * @param TranslatorInterface $translator
68
     * @param EngineInterface $templateEngine
69
     * @param EmailTemplateService $emailTemplateService
70
     * @param string $fallbackLocale
71
     */
72 View Code Duplication
    public function __construct(
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...
73
        Mailer $mailer,
74
        Sender $sender,
75
        TranslatorInterface $translator,
76
        EngineInterface $templateEngine,
77
        EmailTemplateService $emailTemplateService,
78
        $fallbackLocale
79
    ) {
80
        Assertion::string($fallbackLocale, 'Fallback locale "%s" expected to be string, type %s given');
81
82
        $this->mailer = $mailer;
83
        $this->sender = $sender;
84
        $this->translator = $translator;
85
        $this->templateEngine = $templateEngine;
86
        $this->emailTemplateService = $emailTemplateService;
87
        $this->fallbackLocale = $fallbackLocale;
88
    }
89
90
    /**
91
     * @param string $locale
92
     * @param string $commonName
93
     * @param string $email
94
     * @param string $registrationCode
95
     * @param DateTime $expirationDate
96
     * @param RegistrationAuthorityCredentials[] $ras
97
     */
98 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...
99
        $locale,
100
        $commonName,
101
        $email,
102
        $registrationCode,
103
        DateTime $expirationDate,
104
        array $ras
105
    ) {
106
        $subject = $this->translator->trans(
107
            'ss.mail.registration_email.subject',
108
            ['%commonName%' => $commonName],
109
            'messages',
110
            $locale
111
        );
112
113
        $emailTemplate = $this->emailTemplateService->findByName(
114
            'registration_code_with_ras',
115
            $locale,
116
            $this->fallbackLocale
117
        );
118
119
        $parameters = [
120
            'templateString'   => $emailTemplate->htmlContent,
121
            'locale'           => $locale,
122
            'commonName'       => $commonName,
123
            'email'            => $email,
124
            'registrationCode' => $registrationCode,
125
            'expirationDate'   => $expirationDate,
126
            'ras'              => $ras,
127
        ];
128
129
        // Rendering file template instead of string
130
        // (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248)
131
        $body = $this->templateEngine->render(
132
            'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig',
133
            $parameters
134
        );
135
136
        /** @var Message $message */
137
        $message = $this->mailer->createMessage();
138
        $message
139
            ->setFrom($this->sender->getEmail(), $this->sender->getName())
140
            ->addTo($email, $commonName)
141
            ->setSubject($subject)
142
            ->setBody($body, 'text/html', 'utf-8');
143
144
        $this->mailer->send($message);
145
    }
146
147
    /**
148
     * @param string $locale
149
     * @param string $commonName
150
     * @param string $email
151
     * @param string $registrationCode
152
     * @param DateTime $expirationDate
153
     * @param RaLocation[] $raLocations
154
     */
155 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...
156
        $locale,
157
        $commonName,
158
        $email,
159
        $registrationCode,
160
        DateTime $expirationDate,
161
        array $raLocations
162
    ) {
163
        $subject = $this->translator->trans(
164
            'ss.mail.registration_email.subject',
165
            ['%commonName%' => $commonName],
166
            'messages',
167
            $locale
168
        );
169
170
        $emailTemplate = $this->emailTemplateService->findByName(
171
            'registration_code_with_ra_locations',
172
            $locale,
173
            $this->fallbackLocale
174
        );
175
176
        $parameters = [
177
            'templateString'   => $emailTemplate->htmlContent,
178
            'locale'           => $locale,
179
            'commonName'       => $commonName,
180
            'email'            => $email,
181
            'registrationCode' => $registrationCode,
182
            'expirationDate'   => $expirationDate,
183
            'raLocations'      => $raLocations,
184
        ];
185
186
        // Rendering file template instead of string
187
        // (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248)
188
        $body = $this->templateEngine->render(
189
            'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig',
190
            $parameters
191
        );
192
193
        /** @var Message $message */
194
        $message = $this->mailer->createMessage();
195
        $message
196
            ->setFrom($this->sender->getEmail(), $this->sender->getName())
197
            ->addTo($email, $commonName)
198
            ->setSubject($subject)
199
            ->setBody($body, 'text/html', 'utf-8');
200
201
        $this->mailer->send($message);
202
    }
203
}
204