Completed
Push — feature/ra-optional-vetting ( 241f76...b2f623 )
by
unknown
03:37 queued 12s
created

applySecondFactorVettedPossessionSkippedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
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\ApiBundle\Identity\Projector;
20
21
use Broadway\ReadModel\Projector;
22
use Surfnet\Stepup\Identity\Event\CompliedWithUnverifiedSecondFactorRevocationEvent;
23
use Surfnet\Stepup\Identity\Event\CompliedWithVerifiedSecondFactorRevocationEvent;
24
use Surfnet\Stepup\Identity\Event\CompliedWithVettedSecondFactorRevocationEvent;
25
use Surfnet\Stepup\Identity\Event\EmailVerifiedEvent;
26
use Surfnet\Stepup\Identity\Event\GssfPossessionProvenAndVerifiedEvent;
27
use Surfnet\Stepup\Identity\Event\GssfPossessionProvenEvent;
28
use Surfnet\Stepup\Identity\Event\IdentityEmailChangedEvent;
29
use Surfnet\Stepup\Identity\Event\IdentityForgottenEvent;
30
use Surfnet\Stepup\Identity\Event\IdentityRenamedEvent;
31
use Surfnet\Stepup\Identity\Event\PhonePossessionProvenAndVerifiedEvent;
32
use Surfnet\Stepup\Identity\Event\PhonePossessionProvenEvent;
33
use Surfnet\Stepup\Identity\Event\SecondFactorVettedPossessionSkippedEvent;
34
use Surfnet\Stepup\Identity\Event\SecondFactorVettedEvent;
35
use Surfnet\Stepup\Identity\Event\U2fDevicePossessionProvenAndVerifiedEvent;
36
use Surfnet\Stepup\Identity\Event\U2fDevicePossessionProvenEvent;
37
use Surfnet\Stepup\Identity\Event\UnverifiedSecondFactorRevokedEvent;
38
use Surfnet\Stepup\Identity\Event\VerifiedSecondFactorRevokedEvent;
39
use Surfnet\Stepup\Identity\Event\VettedSecondFactorRevokedEvent;
40
use Surfnet\Stepup\Identity\Event\YubikeyPossessionProvenAndVerifiedEvent;
41
use Surfnet\Stepup\Identity\Event\YubikeyPossessionProvenEvent;
42
use Surfnet\Stepup\Identity\Event\YubikeySecondFactorBootstrappedEvent;
43
use Surfnet\Stepup\Identity\Value\CommonName;
44
use Surfnet\Stepup\Identity\Value\DocumentNumber;
45
use Surfnet\Stepup\Identity\Value\Email;
46
use Surfnet\Stepup\Identity\Value\SecondFactorId;
47
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaSecondFactor;
48
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\IdentityRepository;
49
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\RaSecondFactorRepository;
50
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\SecondFactorStatus;
51
52
/**
53
 * @SuppressWarnings(PHPMD.TooManyMethods)
54
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
55
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
56
 */
57
class RaSecondFactorProjector extends Projector
58
{
59
    /**
60
     * @var RaSecondFactorRepository
61
     */
62
    private $raSecondFactorRepository;
63
64
    /**
65
     * @var IdentityRepository
66
     */
67
    private $identityRepository;
68
69
    public function __construct(
70
        RaSecondFactorRepository $raSecondFactorRepository,
71
        IdentityRepository $identityRepository
72
    ) {
73
        $this->raSecondFactorRepository = $raSecondFactorRepository;
74
        $this->identityRepository = $identityRepository;
75
    }
76
77 View Code Duplication
    public function applyIdentityRenamedEvent(IdentityRenamedEvent $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...
78
    {
79
        $secondFactors = $this->raSecondFactorRepository->findByIdentityId((string) $event->identityId);
80
81
        if (count($secondFactors) === 0) {
82
            return;
83
        }
84
85
        $commonName = $event->commonName;
86
87
        foreach ($secondFactors as $secondFactor) {
88
            $secondFactor->name = $commonName;
89
        }
90
91
        $this->raSecondFactorRepository->saveAll($secondFactors);
92
    }
93
94 View Code Duplication
    public function applyIdentityEmailChangedEvent(IdentityEmailChangedEvent $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...
95
    {
96
        $secondFactors = $this->raSecondFactorRepository->findByIdentityId((string) $event->identityId);
97
98
        if (count($secondFactors) === 0) {
99
            return;
100
        }
101
102
        $email = $event->email;
103
104
        foreach ($secondFactors as $secondFactor) {
105
            $secondFactor->email = $email;
106
        }
107
108
        $this->raSecondFactorRepository->saveAll($secondFactors);
109
    }
110
111
    public function applyYubikeySecondFactorBootstrappedEvent(YubikeySecondFactorBootstrappedEvent $event)
112
    {
113
        $identity = $this->identityRepository->find((string) $event->identityId);
114
115
        $secondFactor = new RaSecondFactor(
116
            (string) $event->secondFactorId,
117
            'yubikey',
118
            (string) $event->yubikeyPublicId,
119
            $identity->id,
120
            $identity->institution,
121
            $event->commonName,
122
            $event->email
123
        );
124
        $secondFactor->status = SecondFactorStatus::vetted();
125
126
        $this->raSecondFactorRepository->save($secondFactor);
127
    }
128
129 View Code Duplication
    public function applyYubikeyPossessionProvenEvent(YubikeyPossessionProvenEvent $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...
130
    {
131
        $this->saveRaSecondFactor(
132
            (string) $event->identityId,
133
            (string) $event->secondFactorId,
134
            'yubikey',
135
            (string) $event->yubikeyPublicId,
136
            $event->commonName,
137
            $event->email
138
        );
139
    }
140
141 View Code Duplication
    public function applyYubikeyPossessionProvenAndVerifiedEvent(YubikeyPossessionProvenAndVerifiedEvent $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...
142
    {
143
        $this->saveRaSecondFactor(
144
            (string) $event->identityId,
145
            (string) $event->secondFactorId,
146
            'yubikey',
147
            (string) $event->yubikeyPublicId,
148
            $event->commonName,
149
            $event->email
150
        );
151
    }
152
153 View Code Duplication
    public function applyPhonePossessionProvenEvent(PhonePossessionProvenEvent $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...
154
    {
155
        $this->saveRaSecondFactor(
156
            (string) $event->identityId,
157
            (string) $event->secondFactorId,
158
            'sms',
159
            (string) $event->phoneNumber,
160
            $event->commonName,
161
            $event->email
162
        );
163
    }
164
165 View Code Duplication
    public function applyPhonePossessionProvenAndVerifiedEvent(PhonePossessionProvenAndVerifiedEvent $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...
166
    {
167
        $this->saveRaSecondFactor(
168
            (string) $event->identityId,
169
            (string) $event->secondFactorId,
170
            'sms',
171
            (string) $event->phoneNumber,
172
            $event->commonName,
173
            $event->email
174
        );
175
    }
176
177 View Code Duplication
    public function applyGssfPossessionProvenEvent(GssfPossessionProvenEvent $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...
178
    {
179
        $this->saveRaSecondFactor(
180
            (string) $event->identityId,
181
            (string) $event->secondFactorId,
182
            (string) $event->stepupProvider,
183
            (string) $event->gssfId,
184
            $event->commonName,
185
            $event->email
186
        );
187
    }
188
189 View Code Duplication
    public function applyGssfPossessionProvenAndVerifiedEvent(GssfPossessionProvenAndVerifiedEvent $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...
190
    {
191
        $this->saveRaSecondFactor(
192
            (string) $event->identityId,
193
            (string) $event->secondFactorId,
194
            (string) $event->stepupProvider,
195
            (string) $event->gssfId,
196
            $event->commonName,
197
            $event->email
198
        );
199
    }
200
201 View Code Duplication
    public function applyU2fDevicePossessionProvenEvent(U2fDevicePossessionProvenEvent $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...
202
    {
203
        $this->saveRaSecondFactor(
204
            (string) $event->identityId,
205
            (string) $event->secondFactorId,
206
            'u2f',
207
            $event->keyHandle->getValue(),
208
            $event->commonName,
209
            $event->email
210
        );
211
    }
212
213 View Code Duplication
    public function applyU2fDevicePossessionProvenAndVerifiedEvent(U2fDevicePossessionProvenAndVerifiedEvent $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...
214
    {
215
        $this->saveRaSecondFactor(
216
            (string) $event->identityId,
217
            (string) $event->secondFactorId,
218
            'u2f',
219
            $event->keyHandle->getValue(),
220
            $event->commonName,
221
            $event->email
222
        );
223
    }
224
225
    /**
226
     * @param string $identityId
227
     * @param string $secondFactorId
228
     * @param string $secondFactorType
229
     * @param string $secondFactorIdentifier
230
     * @param CommonName $commonName
231
     * @param Email $email
232
     */
233
    private function saveRaSecondFactor(
234
        $identityId,
235
        $secondFactorId,
236
        $secondFactorType,
237
        $secondFactorIdentifier,
238
        CommonName $commonName,
239
        Email $email
240
    ) {
241
        $identity = $this->identityRepository->find($identityId);
242
243
        $this->raSecondFactorRepository->save(
244
            new RaSecondFactor(
245
                (string) $secondFactorId,
246
                $secondFactorType,
247
                $secondFactorIdentifier,
248
                $identity->id,
249
                $identity->institution,
250
                $commonName,
251
                $email
252
            )
253
        );
254
    }
255
256
    public function applyEmailVerifiedEvent(EmailVerifiedEvent $event)
257
    {
258
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::verified());
259
    }
260
261 View Code Duplication
    public function applySecondFactorVettedEvent(SecondFactorVettedEvent $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...
262
    {
263
        $secondFactor = $this->raSecondFactorRepository->find((string) $event->secondFactorId);
264
265
        $secondFactor->documentNumber = $event->documentNumber;
266
        $secondFactor->status = SecondFactorStatus::vetted();
267
268
        $this->raSecondFactorRepository->save($secondFactor);
269
    }
270
271 View Code Duplication
    public function applySecondFactorVettedPossessionSkippedEvent(SecondFactorVettedPossessionSkippedEvent $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...
272
    {
273
        $secondFactor = $this->raSecondFactorRepository->find((string) $event->secondFactorId);
274
275
        $secondFactor->documentNumber = $event->documentNumber;
276
        $secondFactor->status = SecondFactorStatus::vetted();
277
278
        $this->raSecondFactorRepository->save($secondFactor);
279
    }
280
281
    protected function applyUnverifiedSecondFactorRevokedEvent(UnverifiedSecondFactorRevokedEvent $event)
282
    {
283
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
284
    }
285
286
    protected function applyCompliedWithUnverifiedSecondFactorRevocationEvent(
287
        CompliedWithUnverifiedSecondFactorRevocationEvent $event
288
    ) {
289
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
290
    }
291
292
    protected function applyVerifiedSecondFactorRevokedEvent(VerifiedSecondFactorRevokedEvent $event)
293
    {
294
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
295
    }
296
297
    protected function applyCompliedWithVerifiedSecondFactorRevocationEvent(
298
        CompliedWithVerifiedSecondFactorRevocationEvent $event
299
    ) {
300
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
301
    }
302
303
    protected function applyVettedSecondFactorRevokedEvent(VettedSecondFactorRevokedEvent $event)
304
    {
305
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
306
    }
307
308
    protected function applyCompliedWithVettedSecondFactorRevocationEvent(
309
        CompliedWithVettedSecondFactorRevocationEvent $event
310
    ) {
311
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
312
    }
313
314
    protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event)
315
    {
316
        $this->raSecondFactorRepository->removeByIdentityId($event->identityId);
317
    }
318
319
    /**
320
     * @param SecondFactorId $secondFactorId
321
     * @param SecondFactorStatus $status
322
     */
323
    private function updateStatus(SecondFactorId $secondFactorId, SecondFactorStatus $status)
324
    {
325
        $secondFactor = $this->raSecondFactorRepository->find((string) $secondFactorId);
326
        $secondFactor->status = $status;
327
328
        $this->raSecondFactorRepository->save($secondFactor);
329
    }
330
}
331