Completed
Push — feature/second-factor-revocati... ( a5170e )
by A.
13:46
created

handleVettedSecondFactorRevokedEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 16
loc 16
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
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\Processor;
20
21
use Broadway\Processor\Processor;
22
use Surfnet\Stepup\Configuration\Value\Institution;
23
use Surfnet\Stepup\Identity\Event\CompliedWithVettedSecondFactorRevocationEvent;
24
use Surfnet\Stepup\Identity\Event\EmailVerifiedEvent;
25
use Surfnet\Stepup\Identity\Event\GssfPossessionProvenEvent;
26
use Surfnet\Stepup\Identity\Event\PhonePossessionProvenEvent;
27
use Surfnet\Stepup\Identity\Event\SecondFactorRevokedEvent;
28
use Surfnet\Stepup\Identity\Event\SecondFactorVettedEvent;
29
use Surfnet\Stepup\Identity\Event\U2fDevicePossessionProvenEvent;
30
use Surfnet\Stepup\Identity\Event\YubikeyPossessionProvenEvent;
31
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Service\InstitutionConfigurationOptionsService;
32
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Service\RaLocationService;
33
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\IdentityService;
34
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\RaListingService;
35
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\RegistrationAuthorityCredentials;
36
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Service\SecondFactorMailService;
37
38
class EmailProcessor extends Processor
39
{
40
    /**
41
     * @var SecondFactorMailService
42
     */
43
    private $mailService;
44
45
    /**
46
     * @var RaListingService
47
     */
48
    private $raListingService;
49
50
    /**
51
     * @var InstitutionConfigurationOptionsService
52
     */
53
    private $institutionConfigurationOptionsService;
54
55
    /**
56
     * @var RaLocationService
57
     */
58
    private $raLocationsService;
59
60
    /**
61
     * @var IdentityService
62
     */
63
    private $identityService;
64
65
    /**
66
     * @param SecondFactorMailService $mailService
67
     * @param RaListingService $raListingService
68
     * @param InstitutionConfigurationOptionsService $institutionConfigurationOptionsService
69
     * @param RaLocationService $raLocationsService
70
     * @param IdentityService $identityService
71
     */
72
    public function __construct(
73
        SecondFactorMailService $mailService,
74
        RaListingService $raListingService,
75
        InstitutionConfigurationOptionsService $institutionConfigurationOptionsService,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $institutionConfigurationOptionsService exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
76
        RaLocationService $raLocationsService,
77
        IdentityService $identityService
78
    ) {
79
        $this->mailService                            = $mailService;
80
        $this->raListingService                       = $raListingService;
81
        $this->institutionConfigurationOptionsService = $institutionConfigurationOptionsService;
82
        $this->raLocationsService                     = $raLocationsService;
83
        $this->identityService                        = $identityService;
84
    }
85
86
    public function handlePhonePossessionProvenEvent(PhonePossessionProvenEvent $event)
87
    {
88
        $this->mailService->sendEmailVerificationEmail(
89
            (string) $event->preferredLocale,
90
            (string) $event->commonName,
91
            (string) $event->email,
92
            $event->emailVerificationNonce
93
        );
94
    }
95
96
    public function handleYubikeyPossessionProvenEvent(YubikeyPossessionProvenEvent $event)
97
    {
98
        $this->mailService->sendEmailVerificationEmail(
99
            (string) $event->preferredLocale,
100
            (string) $event->commonName,
101
            (string) $event->email,
102
            $event->emailVerificationNonce
103
        );
104
    }
105
106
    public function handleGssfPossessionProvenEvent(GssfPossessionProvenEvent $event)
107
    {
108
        $this->mailService->sendEmailVerificationEmail(
109
            (string) $event->preferredLocale,
110
            (string) $event->commonName,
111
            (string) $event->email,
112
            $event->emailVerificationNonce
113
        );
114
    }
115
116
    public function handleU2fDevicePossessionProvenEvent(U2fDevicePossessionProvenEvent $event)
117
    {
118
        $this->mailService->sendEmailVerificationEmail(
119
            (string) $event->preferredLocale,
120
            (string) $event->commonName,
121
            (string) $event->email,
122
            $event->emailVerificationNonce
123
        );
124
    }
125
126
    public function handleEmailVerifiedEvent(EmailVerifiedEvent $event)
127
    {
128
        $institution = new Institution($event->identityInstitution->getInstitution());
129
        $institutionConfigurationOptions = $this->institutionConfigurationOptionsService
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $institutionConfigurationOptions exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
130
            ->findInstitutionConfigurationOptionsFor($institution);
131
132
        if ($institutionConfigurationOptions->useRaLocationsOption->isEnabled()) {
133
            $this->sendRegistrationEmailWithRaLocations($event, $institution);
134
135
            return;
136
        }
137
138
        $ras = $this->raListingService->listRegistrationAuthoritiesFor($event->identityInstitution);
139
140
        if ($institutionConfigurationOptions->showRaaContactInformationOption->isEnabled()) {
141
            $this->sendRegistrationEmailWithRas($event, $ras);
142
143
            return;
144
        }
145
146
        $rasWithoutRaas = array_filter($ras, function (RegistrationAuthorityCredentials $ra) {
147
            return !$ra->isRaa();
148
        });
149
150
        $this->sendRegistrationEmailWithRas($event, $rasWithoutRaas);
151
    }
152
153
    public function handleSecondFactorVettedEvent(SecondFactorVettedEvent $event)
154
    {
155
        $this->mailService->sendVettedEmail($event->preferredLocale, $event->commonName, $event->email);
156
    }
157
158 View Code Duplication
    public function handleCompliedWithVettedSecondFactorRevocationEvent(
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...
159
        CompliedWithVettedSecondFactorRevocationEvent $event
160
    ) {
161
        $identity = $this->identityService->find($event->identityId->getIdentityId());
162
163
        if ($identity === null) {
164
            return;
165
        }
166
167
        $this->mailService->sendVettedSecondFactorRevokedByRaEmail(
168
            $identity->preferredLocale,
169
            $identity->commonName,
170
            $identity->email,
171
            $event->secondFactorType,
172
            $event->secondFactorIdentifier
173
        );
174
    }
175
176 View Code Duplication
    public function handleVettedSecondFactorRevokedEvent(SecondFactorRevokedEvent $event)
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...
177
    {
178
        $identity = $this->identityService->find($event->identityId->getIdentityId());
179
180
        if ($identity === null) {
181
            return;
182
        }
183
184
        $this->mailService->sendVettedSecondFactorRevokedBySelfEmail(
185
            $identity->preferredLocale,
186
            $identity->commonName,
187
            $identity->email,
188
            $event->secondFactorType,
189
            $event->secondFactorIdentifier
190
        );
191
    }
192
193
    /**
194
     * @param EmailVerifiedEvent $event
195
     * @param Institution $institution
196
     */
197
    private function sendRegistrationEmailWithRaLocations(EmailVerifiedEvent $event, Institution $institution)
198
    {
199
        $this->mailService->sendRegistrationEmailWithRaLocations(
200
            (string)$event->preferredLocale,
201
            (string)$event->commonName,
202
            (string)$event->email,
203
            $event->registrationCode,
204
            $this->raLocationsService->listRaLocationsFor($institution)
205
        );
206
    }
207
208
    /**
209
     * @param EmailVerifiedEvent $event
210
     * @param RegistrationAuthorityCredentials[] $ras
211
     */
212
    private function sendRegistrationEmailWithRas(EmailVerifiedEvent $event, array $ras)
213
    {
214
        $this->mailService->sendRegistrationEmailWithRas(
215
            (string)$event->preferredLocale,
216
            (string)$event->commonName,
217
            (string)$event->email,
218
            $event->registrationCode,
219
            $ras
220
        );
221
    }
222
}
223