Completed
Push — feature/168109427-remove-u2f-s... ( 0a8d64 )
by
unknown
02:09
created

RaSecondFactorProjector::saveRaSecondFactor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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