Completed
Push — master ( 5467e4...183ea4 )
by Boy
05:06 queued 01:02
created

RaSecondFactorProjector   C

Complexity

Total Complexity 22

Size/Duplication

Total Lines 200
Duplicated Lines 48 %

Coupling/Cohesion

Components 1
Dependencies 23

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 22
lcom 1
cbo 23
dl 96
loc 200
rs 5.5
c 4
b 0
f 1

18 Methods

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