Completed
Push — feature/second-factor-revocati... ( 715c87...f06494 )
by A.
03:32
created

RegistrationMailService   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 166
Duplicated Lines 65.66 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 9
dl 109
loc 166
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 17 17 1
B sendRegistrationEmailWithRas() 46 46 1
B sendRegistrationEmailWithRaLocations() 46 46 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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