Completed
Pull Request — develop (#218)
by
unknown
09:07 queued 04:17
created

EmailVerificationMailService   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 132
Duplicated Lines 18.18 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 8
dl 24
loc 132
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 24 24 1
B sendEmailVerificationEmail() 0 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\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
    /**
67
     * @var string
68
     */
69
    private $selfServiceUrl;
70
71
    /**
72
     * @param Mailer $mailer
73
     * @param Sender $sender
74
     * @param TranslatorInterface $translator
75
     * @param EngineInterface $templateEngine
76
     * @param string $emailVerificationUrlTemplate
77
     * @param EmailTemplateService $emailTemplateService
78
     * @param string $fallbackLocale
79
     * @param string $selfServiceUrl
80
     *
81
     * @throws \Assert\AssertionFailedException
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
        $emailVerificationUrlTemplate,
89
        EmailTemplateService $emailTemplateService,
90
        $fallbackLocale,
91
        $selfServiceUrl
92
    ) {
93
        Assertion::string(
94
            $emailVerificationUrlTemplate,
95
            'Email verification URL template "%s" expected to be string, type %s given'
96
        );
97
98
        $this->mailer = $mailer;
99
        $this->sender = $sender;
100
        $this->translator = $translator;
101
        $this->templateEngine = $templateEngine;
102
        $this->emailVerificationUrlTemplate = $emailVerificationUrlTemplate;
103
        $this->emailTemplateService = $emailTemplateService;
104
        $this->fallbackLocale = $fallbackLocale;
105
        $this->selfServiceUrl = $selfServiceUrl;
106
    }
107
108
    /**
109
     * @param string $locale
110
     * @param string $commonName
111
     * @param string $email
112
     * @param string $verificationNonce
113
     */
114
    public function sendEmailVerificationEmail(
115
        $locale,
116
        $commonName,
117
        $email,
118
        $verificationNonce
119
    ) {
120
        $subject = $this->translator->trans(
121
            'ss.mail.email_verification_email.subject',
122
            ['%commonName%' => $commonName],
123
            'messages',
124
            $locale
125
        );
126
127
        $verificationUrl = str_replace(
128
            '{nonce}',
129
            urlencode($verificationNonce),
130
            $this->emailVerificationUrlTemplate
131
        );
132
        $emailTemplate = $this->emailTemplateService->findByName('confirm_email', $locale, $this->fallbackLocale);
133
134
        $parameters = [
135
            'templateString'   => $emailTemplate->htmlContent,
136
            'locale'           => $locale,
137
            'commonName'       => $commonName,
138
            'email'            => $email,
139
            'verificationUrl'  => $verificationUrl,
140
            'selfServiceUrl'   => $this->selfServiceUrl,
141
        ];
142
143
        // Rendering file template instead of string
144
        // (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248)
145
        $body = $this->templateEngine->render(
146
            'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig',
147
            $parameters
148
        );
149
150
        /** @var Message $message */
151
        $message = $this->mailer->createMessage();
152
        $message
153
            ->setFrom($this->sender->getEmail(), $this->sender->getName())
154
            ->addTo($email, $commonName)
155
            ->setSubject($subject)
156
            ->setBody($body, 'text/html', 'utf-8');
157
158
        $this->mailer->send($message);
159
    }
160
}
161