Completed
Push — feature/show-ra-sf-document-nu... ( c73369...87b5b6 )
by Boy
16:45 queued 11:57
created

applyYubikeyPossessionProvenEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 16
loc 16
rs 9.4285
c 2
b 0
f 0
cc 1
eloc 11
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\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
    public function applyIdentityRenamedEvent(IdentityRenamedEvent $event)
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
    public function applyIdentityEmailChangedEvent(IdentityEmailChangedEvent $event)
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
        $secondFactor = $this->raSecondFactorRepository->find((string) $event->secondFactorId);
197
198
        $secondFactor->documentNumber = $event->documentNumber;
199
        $secondFactor->status = SecondFactorStatus::vetted();
200
201
        $this->raSecondFactorRepository->save($secondFactor);
202
    }
203
204
    protected function applyUnverifiedSecondFactorRevokedEvent(UnverifiedSecondFactorRevokedEvent $event)
205
    {
206
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
207
    }
208
209
    protected function applyCompliedWithUnverifiedSecondFactorRevocationEvent(
210
        CompliedWithUnverifiedSecondFactorRevocationEvent $event
211
    ) {
212
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
213
    }
214
215
    protected function applyVerifiedSecondFactorRevokedEvent(VerifiedSecondFactorRevokedEvent $event)
216
    {
217
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
218
    }
219
220
    protected function applyCompliedWithVerifiedSecondFactorRevocationEvent(
221
        CompliedWithVerifiedSecondFactorRevocationEvent $event
222
    ) {
223
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
224
    }
225
226
    protected function applyVettedSecondFactorRevokedEvent(VettedSecondFactorRevokedEvent $event)
227
    {
228
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
229
    }
230
231
    protected function applyCompliedWithVettedSecondFactorRevocationEvent(
232
        CompliedWithVettedSecondFactorRevocationEvent $event
233
    ) {
234
        $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked());
235
    }
236
237
    protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event)
238
    {
239
        $this->raSecondFactorRepository->removeByIdentityId($event->identityId);
240
    }
241
242
    /**
243
     * @param SecondFactorId $secondFactorId
244
     * @param SecondFactorStatus $status
245
     */
246
    private function updateStatus(SecondFactorId $secondFactorId, SecondFactorStatus $status)
247
    {
248
        $secondFactor = $this->raSecondFactorRepository->find((string) $secondFactorId);
249
        $secondFactor->status = $status;
250
251
        $this->raSecondFactorRepository->save($secondFactor);
252
    }
253
}
254