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

SecondFactorProjector::applyEmailVerifiedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 16
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 12
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\IdentityForgottenEvent;
28
use Surfnet\Stepup\Identity\Event\PhonePossessionProvenEvent;
29
use Surfnet\Stepup\Identity\Event\SecondFactorVettedEvent;
30
use Surfnet\Stepup\Identity\Event\U2fDevicePossessionProvenEvent;
31
use Surfnet\Stepup\Identity\Event\UnverifiedSecondFactorRevokedEvent;
32
use Surfnet\Stepup\Identity\Event\VerifiedSecondFactorRevokedEvent;
33
use Surfnet\Stepup\Identity\Event\VettedSecondFactorRevokedEvent;
34
use Surfnet\Stepup\Identity\Event\YubikeyPossessionProvenEvent;
35
use Surfnet\Stepup\Identity\Event\YubikeySecondFactorBootstrappedEvent;
36
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\UnverifiedSecondFactor;
37
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\VerifiedSecondFactor;
38
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\VettedSecondFactor;
39
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\IdentityRepository;
40
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\UnverifiedSecondFactorRepository;
41
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\VerifiedSecondFactorRepository;
42
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\VettedSecondFactorRepository;
43
44
/**
45
 * @SuppressWarnings(PHPMD.TooManyMethods)
46
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
47
 */
48
class SecondFactorProjector extends Projector
49
{
50
    /**
51
     * @var \Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\UnverifiedSecondFactorRepository
52
     */
53
    private $unverifiedRepository;
54
55
    /**
56
     * @var \Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\VerifiedSecondFactorRepository
57
     */
58
    private $verifiedRepository;
59
60
    /**
61
     * @var \Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\VettedSecondFactorRepository
62
     */
63
    private $vettedRepository;
64
65
    /**
66
     * @var \Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\IdentityRepository
67
     */
68
    private $identityRepository;
69
70
    public function __construct(
71
        UnverifiedSecondFactorRepository $unverifiedRepository,
72
        VerifiedSecondFactorRepository $verifiedRepository,
73
        VettedSecondFactorRepository $vettedRepository,
74
        IdentityRepository $identityRepository
75
    ) {
76
        $this->unverifiedRepository = $unverifiedRepository;
77
        $this->verifiedRepository = $verifiedRepository;
78
        $this->vettedRepository = $vettedRepository;
79
        $this->identityRepository = $identityRepository;
80
    }
81
82
    public function applyYubikeySecondFactorBootstrappedEvent(YubikeySecondFactorBootstrappedEvent $event)
83
    {
84
        $secondFactor = new VettedSecondFactor();
85
        $secondFactor->id = $event->secondFactorId->getSecondFactorId();
86
        $secondFactor->identityId = $event->identityId->getIdentityId();
87
        $secondFactor->type = 'yubikey';
88
        $secondFactor->secondFactorIdentifier = $event->yubikeyPublicId->getValue();
89
90
        $this->vettedRepository->save($secondFactor);
91
    }
92
93 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...
94
    {
95
        $secondFactor = new UnverifiedSecondFactor();
96
        $secondFactor->id = $event->secondFactorId->getSecondFactorId();
97
        $secondFactor->identityId = $event->identityId->getIdentityId();
98
        $secondFactor->type = 'yubikey';
99
        $secondFactor->secondFactorIdentifier = $event->yubikeyPublicId->getValue();
100
        $secondFactor->verificationNonce = $event->emailVerificationNonce;
101
        $secondFactor->verificationNonceValidUntil = $event->emailVerificationWindow->openUntil();
102
103
        $this->unverifiedRepository->save($secondFactor);
104
    }
105
106 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...
107
    {
108
        $secondFactor = new UnverifiedSecondFactor();
109
        $secondFactor->id = $event->secondFactorId->getSecondFactorId();
110
        $secondFactor->identityId = $event->identityId->getIdentityId();
111
        $secondFactor->type = 'sms';
112
        $secondFactor->secondFactorIdentifier = $event->phoneNumber->getValue();
113
        $secondFactor->verificationNonce = $event->emailVerificationNonce;
114
        $secondFactor->verificationNonceValidUntil = $event->emailVerificationWindow->openUntil();
115
116
        $this->unverifiedRepository->save($secondFactor);
117
    }
118
119 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...
120
    {
121
        $secondFactor = new UnverifiedSecondFactor();
122
        $secondFactor->id = $event->secondFactorId->getSecondFactorId();
123
        $secondFactor->identityId = $event->identityId->getIdentityId();
124
        $secondFactor->type = $event->stepupProvider->getStepupProvider();
125
        $secondFactor->secondFactorIdentifier = $event->gssfId->getValue();
126
        $secondFactor->verificationNonce = $event->emailVerificationNonce;
127
        $secondFactor->verificationNonceValidUntil = $event->emailVerificationWindow->openUntil();
128
129
        $this->unverifiedRepository->save($secondFactor);
130
    }
131
132 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...
133
    {
134
        $secondFactor = new UnverifiedSecondFactor();
135
        $secondFactor->id = $event->secondFactorId->getSecondFactorId();
136
        $secondFactor->identityId = $event->identityId->getIdentityId();
137
        $secondFactor->type = 'u2f';
138
        $secondFactor->secondFactorIdentifier = $event->keyHandle->getValue();
139
        $secondFactor->verificationNonce = $event->emailVerificationNonce;
140
        $secondFactor->verificationNonceValidUntil = $event->emailVerificationWindow->openUntil();
141
142
        $this->unverifiedRepository->save($secondFactor);
143
    }
144
145
    public function applyEmailVerifiedEvent(EmailVerifiedEvent $event)
146
    {
147
        $unverified = $this->unverifiedRepository->find($event->secondFactorId->getSecondFactorId());
148
149
        $verified = new VerifiedSecondFactor();
150
        $verified->id = $event->secondFactorId->getSecondFactorId();
151
        $verified->identityId = $event->identityId->getIdentityId();
152
        $verified->institution = $event->identityInstitution->getInstitution();
153
        $verified->commonName = $event->commonName->getCommonName();
154
        $verified->type = $event->secondFactorType->getSecondFactorType();
155
        $verified->secondFactorIdentifier = $unverified->secondFactorIdentifier;
156
        $verified->registrationCode = $event->registrationCode;
157
158
        $this->verifiedRepository->save($verified);
159
        $this->unverifiedRepository->remove($unverified);
160
    }
161
162
    public function applySecondFactorVettedEvent(SecondFactorVettedEvent $event)
163
    {
164
        $verified = $this->verifiedRepository->find($event->secondFactorId->getSecondFactorId());
165
166
        $vetted = new VettedSecondFactor();
167
        $vetted->id = $event->secondFactorId->getSecondFactorId();
168
        $vetted->identityId = $event->identityId->getIdentityId();
169
        $vetted->type = $event->secondFactorType->getSecondFactorType();
170
        $vetted->secondFactorIdentifier = $event->secondFactorIdentifier->getValue();
171
172
        $this->vettedRepository->save($vetted);
173
        $this->verifiedRepository->remove($verified);
0 ignored issues
show
Bug introduced by
It seems like $verified defined by $this->verifiedRepositor...d->getSecondFactorId()) on line 164 can be null; however, Surfnet\StepupMiddleware...torRepository::remove() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
174
    }
175
176
    protected function applyUnverifiedSecondFactorRevokedEvent(UnverifiedSecondFactorRevokedEvent $event)
177
    {
178
        $this->unverifiedRepository->remove($this->unverifiedRepository->find($event->secondFactorId->getSecondFactorId()));
0 ignored issues
show
Bug introduced by
It seems like $this->unverifiedReposit...d->getSecondFactorId()) can be null; however, remove() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
179
    }
180
181
    protected function applyCompliedWithUnverifiedSecondFactorRevocationEvent(
182
        CompliedWithUnverifiedSecondFactorRevocationEvent $event
183
    ) {
184
        $this->unverifiedRepository->remove($this->unverifiedRepository->find($event->secondFactorId->getSecondFactorId()));
0 ignored issues
show
Bug introduced by
It seems like $this->unverifiedReposit...d->getSecondFactorId()) can be null; however, remove() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
185
    }
186
187
    protected function applyVerifiedSecondFactorRevokedEvent(VerifiedSecondFactorRevokedEvent $event)
188
    {
189
        $this->verifiedRepository->remove($this->verifiedRepository->find($event->secondFactorId->getSecondFactorId()));
0 ignored issues
show
Bug introduced by
It seems like $this->verifiedRepositor...d->getSecondFactorId()) can be null; however, remove() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
190
    }
191
192
    protected function applyCompliedWithVerifiedSecondFactorRevocationEvent(
193
        CompliedWithVerifiedSecondFactorRevocationEvent $event
194
    ) {
195
        $this->verifiedRepository->remove($this->verifiedRepository->find($event->secondFactorId->getSecondFactorId()));
0 ignored issues
show
Bug introduced by
It seems like $this->verifiedRepositor...d->getSecondFactorId()) can be null; however, remove() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
196
    }
197
198
    protected function applyVettedSecondFactorRevokedEvent(VettedSecondFactorRevokedEvent $event)
199
    {
200
        $this->vettedRepository->remove($this->vettedRepository->find($event->secondFactorId->getSecondFactorId()));
0 ignored issues
show
Bug introduced by
It seems like $this->vettedRepository-...d->getSecondFactorId()) can be null; however, remove() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
201
    }
202
203
    protected function applyCompliedWithVettedSecondFactorRevocationEvent(
204
        CompliedWithVettedSecondFactorRevocationEvent $event
205
    ) {
206
        $this->vettedRepository->remove($this->vettedRepository->find($event->secondFactorId->getSecondFactorId()));
0 ignored issues
show
Bug introduced by
It seems like $this->vettedRepository-...d->getSecondFactorId()) can be null; however, remove() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
207
    }
208
209
    protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event)
210
    {
211
        $this->unverifiedRepository->removeByIdentityId($event->identityId);
212
        $this->verifiedRepository->removeByIdentityId($event->identityId);
213
        $this->vettedRepository->removeByIdentityId($event->identityId);
214
    }
215
}
216