Completed
Pull Request — develop (#221)
by
unknown
06:22
created

applyIdentityRenamedEvent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 16
loc 16
rs 9.4285
cc 3
eloc 8
nc 3
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 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...
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 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...
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
        $secondFactor = new RaSecondFactor(
0 ignored issues
show
Bug introduced by
The call to RaSecondFactor::__construct() misses some required arguments starting with $name.
Loading history...
113
            (string) $event->secondFactorId,
114
            'yubikey',
115
            (string) $event->yubikeyPublicId,
116
            $event->commonName,
117
            $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...
118
        );
119
        $secondFactor->status = SecondFactorStatus::vetted();
120
121
        $this->raSecondFactorRepository->save($secondFactor);
122
    }
123
124 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...
125
    {
126
        $this->saveRaSecondFactor(
127
            (string) $event->identityId,
128
            (string) $event->secondFactorId,
129
            'yubikey',
130
            (string) $event->yubikeyPublicId,
131
            $event->commonName,
132
            $event->email
133
        );
134
    }
135
136 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...
137
    {
138
        $this->saveRaSecondFactor(
139
            (string) $event->identityId,
140
            (string) $event->secondFactorId,
141
            'yubikey',
142
            (string) $event->yubikeyPublicId,
143
            $event->commonName,
144
            $event->email
145
        );
146
    }
147
148 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...
149
    {
150
        $this->saveRaSecondFactor(
151
            (string) $event->identityId,
152
            (string) $event->secondFactorId,
153
            'sms',
154
            (string) $event->phoneNumber,
155
            $event->commonName,
156
            $event->email
157
        );
158
    }
159
160 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...
161
    {
162
        $this->saveRaSecondFactor(
163
            (string) $event->identityId,
164
            (string) $event->secondFactorId,
165
            'sms',
166
            (string) $event->phoneNumber,
167
            $event->commonName,
168
            $event->email
169
        );
170
    }
171
172 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...
173
    {
174
        $this->saveRaSecondFactor(
175
            (string) $event->identityId,
176
            (string) $event->secondFactorId,
177
            (string) $event->stepupProvider,
178
            (string) $event->gssfId,
179
            $event->commonName,
180
            $event->email
181
        );
182
    }
183
184 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...
185
    {
186
        $this->saveRaSecondFactor(
187
            (string) $event->identityId,
188
            (string) $event->secondFactorId,
189
            (string) $event->stepupProvider,
190
            (string) $event->gssfId,
191
            $event->commonName,
192
            $event->email
193
        );
194
    }
195
196 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...
197
    {
198
        $this->saveRaSecondFactor(
199
            (string) $event->identityId,
200
            (string) $event->secondFactorId,
201
            'u2f',
202
            $event->keyHandle->getValue(),
203
            $event->commonName,
204
            $event->email
205
        );
206
    }
207
208 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...
209
    {
210
        $this->saveRaSecondFactor(
211
            (string) $event->identityId,
212
            (string) $event->secondFactorId,
213
            'u2f',
214
            $event->keyHandle->getValue(),
215
            $event->commonName,
216
            $event->email
217
        );
218
    }
219
220
    /**
221
     * @param string $identityId
222
     * @param string $secondFactorId
223
     * @param string $secondFactorType
224
     * @param string $secondFactorIdentifier
225
     * @param CommonName $commonName
226
     * @param Email $email
227
     */
228
    private function saveRaSecondFactor(
229
        $identityId,
230
        $secondFactorId,
231
        $secondFactorType,
232
        $secondFactorIdentifier,
233
        CommonName $commonName,
234
        Email $email
235
    ) {
236
        $identity = $this->identityRepository->find($identityId);
237
238
        $this->raSecondFactorRepository->save(
239
            new RaSecondFactor(
240
                (string) $secondFactorId,
241
                $secondFactorType,
242
                $secondFactorIdentifier,
243
                $identity->id,
244
                $identity->institution,
245
                $commonName,
246
                $email
247
            )
248
        );
249
    }
250
251
    public function applyEmailVerifiedEvent(EmailVerifiedEvent $event)
252
    {
253
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::verified());
254
    }
255
256
    public function applySecondFactorVettedEvent(SecondFactorVettedEvent $event)
257
    {
258
        $secondFactor = $this->raSecondFactorRepository->find((string) $event->secondFactorId);
259
260
        $secondFactor->documentNumber = $event->documentNumber;
261
        $secondFactor->status = SecondFactorStatus::vetted();
262
263
        $this->raSecondFactorRepository->save($secondFactor);
264
    }
265
266
    protected function applyUnverifiedSecondFactorRevokedEvent(UnverifiedSecondFactorRevokedEvent $event)
267
    {
268
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
269
    }
270
271
    protected function applyCompliedWithUnverifiedSecondFactorRevocationEvent(
272
        CompliedWithUnverifiedSecondFactorRevocationEvent $event
273
    ) {
274
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
275
    }
276
277
    protected function applyVerifiedSecondFactorRevokedEvent(VerifiedSecondFactorRevokedEvent $event)
278
    {
279
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
280
    }
281
282
    protected function applyCompliedWithVerifiedSecondFactorRevocationEvent(
283
        CompliedWithVerifiedSecondFactorRevocationEvent $event
284
    ) {
285
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
286
    }
287
288
    protected function applyVettedSecondFactorRevokedEvent(VettedSecondFactorRevokedEvent $event)
289
    {
290
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
291
    }
292
293
    protected function applyCompliedWithVettedSecondFactorRevocationEvent(
294
        CompliedWithVettedSecondFactorRevocationEvent $event
295
    ) {
296
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
297
    }
298
299
    protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event)
300
    {
301
        $this->raSecondFactorRepository->removeByIdentityId($event->identityId);
302
    }
303
304
    /**
305
     * @param SecondFactorId $secondFactorId
306
     * @param SecondFactorStatus $status
307
     */
308
    private function updateStatus(SecondFactorId $secondFactorId, SecondFactorStatus $status)
309
    {
310
        $secondFactor = $this->raSecondFactorRepository->find((string) $secondFactorId);
311
        $secondFactor->status = $status;
312
313
        $this->raSecondFactorRepository->save($secondFactor);
314
    }
315
}
316