Completed
Pull Request — develop (#159)
by A.
07:02 queued 03:30
created

handleCompliedWithVettedSecondFactorRevocationEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 17
loc 17
rs 9.4285
cc 2
eloc 11
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
/**
39
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) Uses several services for information needed for relevant emails
40
 */
41
class EmailProcessor extends Processor
42
{
43
    /**
44
     * @var SecondFactorMailService
45
     */
46
    private $mailService;
47
48
    /**
49
     * @var RaListingService
50
     */
51
    private $raListingService;
52
53
    /**
54
     * @var InstitutionConfigurationOptionsService
55
     */
56
    private $institutionConfigurationOptionsService;
57
58
    /**
59
     * @var RaLocationService
60
     */
61
    private $raLocationsService;
62
63
    /**
64
     * @var IdentityService
65
     */
66
    private $identityService;
67
68
    /**
69
     * @param SecondFactorMailService $mailService
70
     * @param RaListingService $raListingService
71
     * @param InstitutionConfigurationOptionsService $institutionConfigurationOptionsService
72
     * @param RaLocationService $raLocationsService
73
     * @param IdentityService $identityService
74
     */
75
    public function __construct(
76
        SecondFactorMailService $mailService,
77
        RaListingService $raListingService,
78
        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...
79
        RaLocationService $raLocationsService,
80
        IdentityService $identityService
81
    ) {
82
        $this->mailService                            = $mailService;
83
        $this->raListingService                       = $raListingService;
84
        $this->institutionConfigurationOptionsService = $institutionConfigurationOptionsService;
85
        $this->raLocationsService                     = $raLocationsService;
86
        $this->identityService                        = $identityService;
87
    }
88
89
    public function handlePhonePossessionProvenEvent(PhonePossessionProvenEvent $event)
90
    {
91
        $this->mailService->sendEmailVerificationEmail(
92
            (string) $event->preferredLocale,
93
            (string) $event->commonName,
94
            (string) $event->email,
95
            $event->emailVerificationNonce
96
        );
97
    }
98
99
    public function handleYubikeyPossessionProvenEvent(YubikeyPossessionProvenEvent $event)
100
    {
101
        $this->mailService->sendEmailVerificationEmail(
102
            (string) $event->preferredLocale,
103
            (string) $event->commonName,
104
            (string) $event->email,
105
            $event->emailVerificationNonce
106
        );
107
    }
108
109
    public function handleGssfPossessionProvenEvent(GssfPossessionProvenEvent $event)
110
    {
111
        $this->mailService->sendEmailVerificationEmail(
112
            (string) $event->preferredLocale,
113
            (string) $event->commonName,
114
            (string) $event->email,
115
            $event->emailVerificationNonce
116
        );
117
    }
118
119
    public function handleU2fDevicePossessionProvenEvent(U2fDevicePossessionProvenEvent $event)
120
    {
121
        $this->mailService->sendEmailVerificationEmail(
122
            (string) $event->preferredLocale,
123
            (string) $event->commonName,
124
            (string) $event->email,
125
            $event->emailVerificationNonce
126
        );
127
    }
128
129
    public function handleEmailVerifiedEvent(EmailVerifiedEvent $event)
130
    {
131
        $institution = new Institution($event->identityInstitution->getInstitution());
132
        $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...
133
            ->findInstitutionConfigurationOptionsFor($institution);
134
135
        if ($institutionConfigurationOptions->useRaLocationsOption->isEnabled()) {
136
            $this->sendRegistrationEmailWithRaLocations($event, $institution);
137
138
            return;
139
        }
140
141
        $ras = $this->raListingService->listRegistrationAuthoritiesFor($event->identityInstitution);
142
143
        if ($institutionConfigurationOptions->showRaaContactInformationOption->isEnabled()) {
144
            $this->sendRegistrationEmailWithRas($event, $ras);
145
146
            return;
147
        }
148
149
        $rasWithoutRaas = array_filter($ras, function (RegistrationAuthorityCredentials $ra) {
150
            return !$ra->isRaa();
151
        });
152
153
        $this->sendRegistrationEmailWithRas($event, $rasWithoutRaas);
154
    }
155
156
    public function handleSecondFactorVettedEvent(SecondFactorVettedEvent $event)
157
    {
158
        $this->mailService->sendVettedEmail($event->preferredLocale, $event->commonName, $event->email);
159
    }
160
161 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...
162
        CompliedWithVettedSecondFactorRevocationEvent $event
163
    ) {
164
        $identity = $this->identityService->find($event->identityId->getIdentityId());
165
166
        if ($identity === null) {
167
            return;
168
        }
169
170
        $this->mailService->sendVettedSecondFactorRevokedByRaEmail(
171
            $identity->preferredLocale,
172
            $identity->commonName,
173
            $identity->email,
174
            $event->secondFactorType,
175
            $event->secondFactorIdentifier
176
        );
177
    }
178
179 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...
180
    {
181
        $identity = $this->identityService->find($event->identityId->getIdentityId());
182
183
        if ($identity === null) {
184
            return;
185
        }
186
187
        $this->mailService->sendVettedSecondFactorRevokedBySelfEmail(
188
            $identity->preferredLocale,
189
            $identity->commonName,
190
            $identity->email,
191
            $event->secondFactorType,
192
            $event->secondFactorIdentifier
193
        );
194
    }
195
196
    /**
197
     * @param EmailVerifiedEvent $event
198
     * @param Institution $institution
199
     */
200
    private function sendRegistrationEmailWithRaLocations(EmailVerifiedEvent $event, Institution $institution)
201
    {
202
        $this->mailService->sendRegistrationEmailWithRaLocations(
203
            (string)$event->preferredLocale,
204
            (string)$event->commonName,
205
            (string)$event->email,
206
            $event->registrationCode,
207
            $this->raLocationsService->listRaLocationsFor($institution)
208
        );
209
    }
210
211
    /**
212
     * @param EmailVerifiedEvent $event
213
     * @param RegistrationAuthorityCredentials[] $ras
214
     */
215
    private function sendRegistrationEmailWithRas(EmailVerifiedEvent $event, array $ras)
216
    {
217
        $this->mailService->sendRegistrationEmailWithRas(
218
            (string)$event->preferredLocale,
219
            (string)$event->commonName,
220
            (string)$event->email,
221
            $event->registrationCode,
222
            $ras
223
        );
224
    }
225
}
226