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

applyImplicitlyVerifiedByIdpEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
163
164
        $this->verifiedRepository->save($verified);
165
        $this->unverifiedRepository->remove($unverified);
166
    }
167
168
    public function applyImplicitlyVerifiedByIdpEvent(ImplicitlyVerifiedByIdp $event)
169
    {
170
        $verified = new VerifiedSecondFactor();
171
        $verified->id = $event->secondFactorId->getSecondFactorId();
172
        $verified->identityId = $event->identityId->getIdentityId();
173
        $verified->institution = $event->identityInstitution->getInstitution();
174
        $verified->type = $event->secondFactorType->getSecondFactorType();
175
        $verified->secondFactorIdentifier = $event->secondFactorIdentifier;
0 ignored issues
show
Documentation Bug introduced by
It seems like $event->secondFactorIdentifier of type object<Surfnet\Stepup\Id...SecondFactorIdentifier> is incompatible with the declared type string of property $secondFactorIdentifier.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
176
        $verified->registrationCode = $event->registrationCode;
177
        $verified->registrationRequestedAt = $event->registrationRequestedAt;
0 ignored issues
show
Documentation Bug introduced by
It seems like $event->registrationRequestedAt of type object<Surfnet\Stepup\DateTime\DateTime> is incompatible with the declared type object<DateTime> of property $registrationRequestedAt.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
178
179
        $this->verifiedRepository->save($verified);
180
    }
181
182
    public function applySecondFactorVettedEvent(SecondFactorVettedEvent $event)
183
    {
184
        $verified = $this->verifiedRepository->find($event->secondFactorId->getSecondFactorId());
185
186
        $vetted = new VettedSecondFactor();
187
        $vetted->id = $event->secondFactorId->getSecondFactorId();
188
        $vetted->identityId = $event->identityId->getIdentityId();
189
        $vetted->type = $event->secondFactorType->getSecondFactorType();
190
        $vetted->secondFactorIdentifier = $event->secondFactorIdentifier->getValue();
191
192
        $this->vettedRepository->save($vetted);
193
        $this->verifiedRepository->remove($verified);
0 ignored issues
show
Bug introduced by
It seems like $verified defined by $this->verifiedRepositor...d->getSecondFactorId()) on line 184 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...
194
    }
195
196
    protected function applyUnverifiedSecondFactorRevokedEvent(UnverifiedSecondFactorRevokedEvent $event)
197
    {
198
        $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...
199
    }
200
201
    protected function applyCompliedWithUnverifiedSecondFactorRevocationEvent(
202
        CompliedWithUnverifiedSecondFactorRevocationEvent $event
203
    ) {
204
        $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...
205
    }
206
207
    protected function applyVerifiedSecondFactorRevokedEvent(VerifiedSecondFactorRevokedEvent $event)
208
    {
209
        $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...
210
    }
211
212
    protected function applyCompliedWithVerifiedSecondFactorRevocationEvent(
213
        CompliedWithVerifiedSecondFactorRevocationEvent $event
214
    ) {
215
        $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...
216
    }
217
218
    protected function applyVettedSecondFactorRevokedEvent(VettedSecondFactorRevokedEvent $event)
219
    {
220
        $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...
221
    }
222
223
    protected function applyCompliedWithVettedSecondFactorRevocationEvent(
224
        CompliedWithVettedSecondFactorRevocationEvent $event
225
    ) {
226
        $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...
227
    }
228
229
    protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event)
230
    {
231
        $this->unverifiedRepository->removeByIdentityId($event->identityId);
232
        $this->verifiedRepository->removeByIdentityId($event->identityId);
233
        $this->vettedRepository->removeByIdentityId($event->identityId);
234
    }
235
}
236