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

SecondFactorRevocationMailService   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 179
Duplicated Lines 64.8 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 14
dl 116
loc 179
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 20 20 1
A sendVettedSecondFactorRevokedByRaEmail() 48 48 1
A sendVettedSecondFactorRevokedByRegistrantEmail() 48 48 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\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\CommandHandlingBundle\Configuration\Service\EmailTemplateService;
28
use Surfnet\StepupMiddleware\CommandHandlingBundle\Value\Sender;
29
use Swift_Mailer as Mailer;
30
use Swift_Message as Message;
31
use Symfony\Component\Templating\EngineInterface;
32
use Symfony\Component\Translation\TranslatorInterface;
33
34
/**
35
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
36
 */
37
final class SecondFactorRevocationMailService
38
{
39
    /**
40
     * @var Mailer
41
     */
42
    private $mailer;
43
44
    /**
45
     * @var Sender
46
     */
47
    private $sender;
48
49
    /**
50
     * @var TranslatorInterface
51
     */
52
    private $translator;
53
54
    /**
55
     * @var EngineInterface
56
     */
57
    private $templateEngine;
58
59
    /**
60
     * @var \Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Service\EmailTemplateService
61
     */
62
    private $emailTemplateService;
63
64
    /**
65
     * @var string
66
     */
67
    private $fallbackLocale;
68
69
    /**
70
     * @var string
71
     */
72
    private $selfServiceUrl;
73
74
    /**
75
     * @param Mailer $mailer
76
     * @param Sender $sender
77
     * @param TranslatorInterface $translator
78
     * @param EngineInterface $templateEngine
79
     * @param EmailTemplateService $emailTemplateService
80
     * @param string $fallbackLocale
81
     * @param string $selfServiceUrl
82
     */
83 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...
84
        Mailer $mailer,
85
        Sender $sender,
86
        TranslatorInterface $translator,
87
        EngineInterface $templateEngine,
88
        EmailTemplateService $emailTemplateService,
89
        $fallbackLocale,
90
        $selfServiceUrl
91
    ) {
92
        Assertion::string($fallbackLocale, 'Fallback locale "%s" expected to be string, type %s given');
93
        Assertion::string($selfServiceUrl, 'Self Service URL "%s" expected to be string, type %s given');
94
95
        $this->mailer = $mailer;
96
        $this->sender = $sender;
97
        $this->translator = $translator;
98
        $this->templateEngine = $templateEngine;
99
        $this->emailTemplateService = $emailTemplateService;
100
        $this->fallbackLocale = $fallbackLocale;
101
        $this->selfServiceUrl = $selfServiceUrl;
102
    }
103
104
    /**
105
     * @param Locale $locale
106
     * @param CommonName $commonName
107
     * @param Email $email
108
     * @param SecondFactorType $secondFactorType
109
     * @param SecondFactorIdentifier $secondFactorIdentifier
110
     */
111 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...
112
        Locale $locale,
113
        CommonName $commonName,
114
        Email $email,
115
        SecondFactorType $secondFactorType,
116
        SecondFactorIdentifier $secondFactorIdentifier
117
    ) {
118
        $subject = $this->translator->trans(
119
            'mw.mail.second_factor_revoked.subject',
120
            [
121
                '%tokenType%' => $secondFactorType
122
            ],
123
            'messages',
124
            $locale->getLocale()
125
        );
126
127
        $emailTemplate = $this->emailTemplateService->findByName(
128
            'second_factor_revoked',
129
            $locale->getLocale(),
130
            $this->fallbackLocale
131
        );
132
        $parameters = [
133
            'isRevokedByRa'   => true,
134
            'templateString'  => $emailTemplate->htmlContent,
135
            'commonName'      => $commonName->getCommonName(),
136
            'tokenType'       => $secondFactorType->getSecondFactorType(),
137
            'tokenIdentifier' => $secondFactorIdentifier->getValue(),
138
            'selfServiceUrl'  => $this->selfServiceUrl,
139
            'locale'          => $locale->getLocale(),
140
        ];
141
142
        // Rendering file template instead of string
143
        // (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248)
144
        $body = $this->templateEngine->render(
145
            'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig',
146
            $parameters
147
        );
148
149
        /** @var Message $message */
150
        $message = $this->mailer->createMessage();
151
        $message
152
            ->setFrom($this->sender->getEmail(), $this->sender->getName())
153
            ->addTo($email->getEmail(), $commonName->getCommonName())
154
            ->setSubject($subject)
155
            ->setBody($body, 'text/html', 'utf-8');
156
157
        $this->mailer->send($message);
158
    }
159
160
    /**
161
     * @param Locale $locale
162
     * @param CommonName $commonName
163
     * @param Email $email
164
     * @param SecondFactorType $secondFactorType
165
     * @param SecondFactorIdentifier $secondFactorIdentifier
166
     */
167 View Code Duplication
    public function sendVettedSecondFactorRevokedByRegistrantEmail(
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...
168
        Locale $locale,
169
        CommonName $commonName,
170
        Email $email,
171
        SecondFactorType $secondFactorType,
172
        SecondFactorIdentifier $secondFactorIdentifier
173
    ) {
174
        $subject = $this->translator->trans(
175
            'mw.mail.second_factor_revoked.subject',
176
            [
177
                '%tokenType%' => $secondFactorType
178
            ],
179
            'messages',
180
            $locale->getLocale()
181
        );
182
183
        $emailTemplate = $this->emailTemplateService->findByName(
184
            'second_factor_revoked',
185
            $locale->getLocale(),
186
            $this->fallbackLocale
187
        );
188
        $parameters = [
189
            'isRevokedByRa'   => false,
190
            'templateString'  => $emailTemplate->htmlContent,
191
            'commonName'      => $commonName->getCommonName(),
192
            'tokenType'       => $secondFactorType->getSecondFactorType(),
193
            'tokenIdentifier' => $secondFactorIdentifier->getValue(),
194
            'selfServiceUrl'  => $this->selfServiceUrl,
195
            'locale'          => $locale->getLocale(),
196
        ];
197
198
        // Rendering file template instead of string
199
        // (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248)
200
        $body = $this->templateEngine->render(
201
            'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig',
202
            $parameters
203
        );
204
205
        /** @var Message $message */
206
        $message = $this->mailer->createMessage();
207
        $message
208
            ->setFrom($this->sender->getEmail(), $this->sender->getName())
209
            ->addTo($email->getEmail(), $commonName->getCommonName())
210
            ->setSubject($subject)
211
            ->setBody($body, 'text/html', 'utf-8');
212
213
        $this->mailer->send($message);
214
    }
215
}
216