Completed
Push — feature/verify-email-proven-ev... ( 178167 )
by
unknown
04:44
created

RaSecondFactorProjector   C

Complexity

Total Complexity 23

Size/Duplication

Total Lines 210
Duplicated Lines 45.71 %

Coupling/Cohesion

Components 1
Dependencies 24

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 24
dl 96
loc 210
rs 5.238
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A applyIdentityRenamedEvent() 16 16 3
A applyIdentityEmailChangedEvent() 16 16 3
A applyYubikeySecondFactorBootstrappedEvent() 0 17 1
A applyYubikeyPossessionProvenEvent() 16 16 1
A applyPhonePossessionProvenEvent() 16 16 1
A applyGssfPossessionProvenEvent() 16 16 1
A applyU2fDevicePossessionProvenEvent() 16 16 1
A applyEmailVerifiedEvent() 0 4 1
A applyImplicitlyVerifiedByIdpEvent() 0 4 1
A applySecondFactorVettedEvent() 0 9 1
A applyUnverifiedSecondFactorRevokedEvent() 0 4 1
A applyCompliedWithUnverifiedSecondFactorRevocationEvent() 0 5 1
A applyVerifiedSecondFactorRevokedEvent() 0 4 1
A applyCompliedWithVerifiedSecondFactorRevocationEvent() 0 5 1
A applyVettedSecondFactorRevokedEvent() 0 4 1
A applyCompliedWithVettedSecondFactorRevocationEvent() 0 5 1
A applyIdentityForgottenEvent() 0 4 1
A updateStatus() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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