Completed
Push — master ( a3c457...aab356 )
by
unknown
03:38
created

applyYubikeyPossessionProvenAndVerifiedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

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