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

SecondFactorVettedMailService::sendVettedEmail()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 25
nc 1
nop 3
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\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 SecondFactorVettedMailService
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
    /**
66
     * @param Mailer $mailer
67
     * @param Sender $sender
68
     * @param TranslatorInterface $translator
69
     * @param EngineInterface $templateEngine
70
     * @param EmailTemplateService $emailTemplateService
71
     * @param string $fallbackLocale
72
     */
73 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...
74
        Mailer $mailer,
75
        Sender $sender,
76
        TranslatorInterface $translator,
77
        EngineInterface $templateEngine,
78
        EmailTemplateService $emailTemplateService,
79
        $fallbackLocale
80
    ) {
81
        Assertion::string($fallbackLocale, 'Fallback locale "%s" expected to be string, type %s given');
82
83
        $this->mailer = $mailer;
84
        $this->sender = $sender;
85
        $this->translator = $translator;
86
        $this->templateEngine = $templateEngine;
87
        $this->emailTemplateService = $emailTemplateService;
88
        $this->fallbackLocale = $fallbackLocale;
89
    }
90
91
92
93
    /**
94
     * @param Locale     $locale
95
     * @param CommonName $commonName
96
     * @param Email      $email
97
     */
98
    public function sendVettedEmail(
99
        Locale $locale,
100
        CommonName $commonName,
101
        Email $email
102
    ) {
103
        $subject = $this->translator->trans(
104
            'ss.mail.vetted_email.subject',
105
            ['%commonName%' => $commonName->getCommonName(), '%email%' => $email->getEmail()],
106
            'messages',
107
            $locale->getLocale()
108
        );
109
110
        $emailTemplate = $this->emailTemplateService->findByName('vetted', $locale->getLocale(), $this->fallbackLocale);
111
        $parameters = [
112
            'templateString'   => $emailTemplate->htmlContent,
113
            'locale'           => $locale->getLocale(),
114
            'commonName'       => $commonName->getCommonName(),
115
            'email'            => $email->getEmail(),
116
        ];
117
118
        // Rendering file template instead of string
119
        // (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248)
120
        $body = $this->templateEngine->render(
121
            'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig',
122
            $parameters
123
        );
124
125
        /** @var Message $message */
126
        $message = $this->mailer->createMessage();
127
        $message
128
            ->setFrom($this->sender->getEmail(), $this->sender->getName())
129
            ->addTo($email->getEmail(), $commonName->getCommonName())
130
            ->setSubject($subject)
131
            ->setBody($body, 'text/html', 'utf-8');
132
133
        $this->mailer->send($message);
134
    }
135
}
136