Completed
Pull Request — develop (#114)
by Boy
08:17 queued 03:26
created

RaSecondFactorProjector::updateDocumentNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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