Completed
Push — feature/verify-email-new-prove... ( 7083d2 )
by
unknown
08:05 queued 04:39
created

applyVerifiedSecondFactorRevokedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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
 */
55
class RaSecondFactorProjector extends Projector
56
{
57
    /**
58
     * @var RaSecondFactorRepository
59
     */
60
    private $raSecondFactorRepository;
61
62
    /**
63
     * @var IdentityRepository
64
     */
65
    private $identityRepository;
66
67
    public function __construct(
68
        RaSecondFactorRepository $raSecondFactorRepository,
69
        IdentityRepository $identityRepository
70
    ) {
71
        $this->raSecondFactorRepository = $raSecondFactorRepository;
72
        $this->identityRepository = $identityRepository;
73
    }
74
75 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...
76
    {
77
        $secondFactors = $this->raSecondFactorRepository->findByIdentityId((string) $event->identityId);
78
79
        if (count($secondFactors) === 0) {
80
            return;
81
        }
82
83
        $commonName = $event->commonName;
84
85
        foreach ($secondFactors as $secondFactor) {
86
            $secondFactor->name = $commonName;
87
        }
88
89
        $this->raSecondFactorRepository->saveAll($secondFactors);
90
    }
91
92 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...
93
    {
94
        $secondFactors = $this->raSecondFactorRepository->findByIdentityId((string) $event->identityId);
95
96
        if (count($secondFactors) === 0) {
97
            return;
98
        }
99
100
        $email = $event->email;
101
102
        foreach ($secondFactors as $secondFactor) {
103
            $secondFactor->email = $email;
104
        }
105
106
        $this->raSecondFactorRepository->saveAll($secondFactors);
107
    }
108
109
    public function applyYubikeySecondFactorBootstrappedEvent(YubikeySecondFactorBootstrappedEvent $event)
110
    {
111
        $identity = $this->identityRepository->find((string) $event->identityId);
0 ignored issues
show
Unused Code introduced by
$identity is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
112
113
        $secondFactor = new RaSecondFactor(
0 ignored issues
show
Bug introduced by
The call to RaSecondFactor::__construct() misses some required arguments starting with $name.
Loading history...
114
            (string) $event->secondFactorId,
115
            'yubikey',
116
            (string) $event->yubikeyPublicId,
117
            $event->commonName,
118
            $event->email
0 ignored issues
show
Documentation introduced by
$event->email is of type object<Surfnet\Stepup\Identity\Value\Email>, but the function expects a object<Surfnet\Stepup\Identity\Value\Institution>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 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...
198
    {
199
        $this->saveRaSecondFactor(
200
            (string) $event->identityId,
201
            (string) $event->secondFactorId,
202
            'u2f',
203
            $event->keyHandle->getValue(),
204
            $event->commonName,
205
            $event->email
206
        );
207
    }
208
209 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...
210
    {
211
        $this->saveRaSecondFactor(
212
            (string) $event->identityId,
213
            (string) $event->secondFactorId,
214
            'u2f',
215
            $event->keyHandle->getValue(),
216
            $event->commonName,
217
            $event->email
218
        );
219
    }
220
221
    /**
222
     * @param string $identityId
223
     * @param string $secondFactorId
224
     * @param string $secondFactorType
225
     * @param string $secondFactorIdentifier
226
     * @param CommonName $commonName
227
     * @param Email $email
228
     */
229
    private function saveRaSecondFactor(
230
        $identityId,
231
        $secondFactorId,
232
        $secondFactorType,
233
        $secondFactorIdentifier,
234
        CommonName $commonName,
235
        Email $email
236
    ) {
237
        $identity = $this->identityRepository->find($identityId);
238
239
        $this->raSecondFactorRepository->save(
240
            new RaSecondFactor(
241
                (string) $secondFactorId,
242
                $secondFactorType,
243
                $secondFactorIdentifier,
244
                $identity->id,
245
                $identity->institution,
246
                $commonName,
247
                $email
248
            )
249
        );
250
    }
251
252
    public function applyEmailVerifiedEvent(EmailVerifiedEvent $event)
253
    {
254
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::verified());
255
    }
256
257
    public function applySecondFactorVettedEvent(SecondFactorVettedEvent $event)
258
    {
259
        $secondFactor = $this->raSecondFactorRepository->find((string) $event->secondFactorId);
260
261
        $secondFactor->documentNumber = $event->documentNumber;
262
        $secondFactor->status = SecondFactorStatus::vetted();
263
264
        $this->raSecondFactorRepository->save($secondFactor);
265
    }
266
267
    protected function applyUnverifiedSecondFactorRevokedEvent(UnverifiedSecondFactorRevokedEvent $event)
268
    {
269
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
270
    }
271
272
    protected function applyCompliedWithUnverifiedSecondFactorRevocationEvent(
273
        CompliedWithUnverifiedSecondFactorRevocationEvent $event
274
    ) {
275
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
276
    }
277
278
    protected function applyVerifiedSecondFactorRevokedEvent(VerifiedSecondFactorRevokedEvent $event)
279
    {
280
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
281
    }
282
283
    protected function applyCompliedWithVerifiedSecondFactorRevocationEvent(
284
        CompliedWithVerifiedSecondFactorRevocationEvent $event
285
    ) {
286
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
287
    }
288
289
    protected function applyVettedSecondFactorRevokedEvent(VettedSecondFactorRevokedEvent $event)
290
    {
291
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
292
    }
293
294
    protected function applyCompliedWithVettedSecondFactorRevocationEvent(
295
        CompliedWithVettedSecondFactorRevocationEvent $event
296
    ) {
297
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
298
    }
299
300
    protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event)
301
    {
302
        $this->raSecondFactorRepository->removeByIdentityId($event->identityId);
303
    }
304
305
    /**
306
     * @param SecondFactorId $secondFactorId
307
     * @param SecondFactorStatus $status
308
     */
309
    private function updateStatus(SecondFactorId $secondFactorId, SecondFactorStatus $status)
310
    {
311
        $secondFactor = $this->raSecondFactorRepository->find((string) $secondFactorId);
312
        $secondFactor->status = $status;
313
314
        $this->raSecondFactorRepository->save($secondFactor);
315
    }
316
}
317