SecondFactorProjector   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 61
dl 0
loc 122
rs 10
c 3
b 0
f 0
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A isIdentityVetted() 0 3 1
A applySecondFactorVettedWithoutTokenProofOfPossession() 0 13 1
A applyCompliedWithVettedSecondFactorRevocationEvent() 0 15 2
A applySecondFactorVettedEvent() 0 12 1
A applyLocalePreferenceExpressedEvent() 0 7 2
A applyYubikeySecondFactorBootstrappedEvent() 0 12 1
A applyVettedSecondFactorRevokedEvent() 0 14 2
A applySecondFactorMigratedEvent() 0 12 1
A __construct() 0 2 1
A applyIdentityForgottenEvent() 0 3 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
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupMiddleware\GatewayBundle\Projector;
20
21
use Surfnet\Stepup\Projector\Projector;
22
use Surfnet\Stepup\Identity\Event\CompliedWithVettedSecondFactorRevocationEvent;
23
use Surfnet\Stepup\Identity\Event\IdentityForgottenEvent;
24
use Surfnet\Stepup\Identity\Event\LocalePreferenceExpressedEvent;
25
use Surfnet\Stepup\Identity\Event\SecondFactorMigratedEvent;
26
use Surfnet\Stepup\Identity\Event\SecondFactorVettedEvent;
27
use Surfnet\Stepup\Identity\Event\SecondFactorVettedWithoutTokenProofOfPossession;
28
use Surfnet\Stepup\Identity\Event\VettedSecondFactorRevokedEvent;
29
use Surfnet\Stepup\Identity\Event\YubikeySecondFactorBootstrappedEvent;
30
use Surfnet\Stepup\Identity\Value\VettingType;
31
use Surfnet\StepupMiddleware\GatewayBundle\Entity\SecondFactor;
32
use Surfnet\StepupMiddleware\GatewayBundle\Exception\RuntimeException;
33
use Surfnet\StepupMiddleware\GatewayBundle\Repository\SecondFactorRepository;
34
35
class SecondFactorProjector extends Projector
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SecondFactorProjector
Loading history...
36
{
37
    public function __construct(private readonly SecondFactorRepository $repository)
38
    {
39
    }
40
41
    public function applyYubikeySecondFactorBootstrappedEvent(YubikeySecondFactorBootstrappedEvent $event): void
42
    {
43
        $this->repository->save(
44
            new SecondFactor(
45
                (string)$event->identityId,
46
                (string)$event->nameId,
47
                (string)$event->identityInstitution,
48
                (string)$event->preferredLocale,
49
                (string)$event->secondFactorId,
50
                (string)$event->yubikeyPublicId,
51
                'yubikey',
52
                true,
53
            ),
54
        );
55
    }
56
57
    public function applySecondFactorMigratedEvent(SecondFactorMigratedEvent $event): void
58
    {
59
        $this->repository->save(
60
            new SecondFactor(
61
                (string)$event->identityId,
62
                (string)$event->targetNameId,
63
                (string)$event->identityInstitution,
64
                (string)$event->preferredLocale,
65
                (string)$event->newSecondFactorId,
66
                $event->secondFactorIdentifier,
67
                $event->secondFactorType,
68
                $this->isIdentityVetted($event->vettingType),
69
            ),
70
        );
71
    }
72
73
    public function applySecondFactorVettedEvent(SecondFactorVettedEvent $event): void
74
    {
75
        $this->repository->save(
76
            new SecondFactor(
77
                (string)$event->identityId,
78
                (string)$event->nameId,
79
                (string)$event->identityInstitution,
80
                (string)$event->preferredLocale,
81
                (string)$event->secondFactorId,
82
                $event->secondFactorIdentifier,
83
                $event->secondFactorType,
84
                $this->isIdentityVetted($event->vettingType),
0 ignored issues
show
Bug introduced by
It seems like $event->vettingType can also be of type null; however, parameter $vettingType of Surfnet\StepupMiddleware...tor::isIdentityVetted() does only seem to accept Surfnet\Stepup\Identity\Value\VettingType, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
                $this->isIdentityVetted(/** @scrutinizer ignore-type */ $event->vettingType),
Loading history...
85
            ),
86
        );
87
    }
88
89
    public function applySecondFactorVettedWithoutTokenProofOfPossession(
90
        SecondFactorVettedWithoutTokenProofOfPossession $event,
91
    ): void {
92
        $this->repository->save(
93
            new SecondFactor(
94
                (string)$event->identityId,
95
                (string)$event->nameId,
96
                (string)$event->identityInstitution,
97
                (string)$event->preferredLocale,
98
                (string)$event->secondFactorId,
99
                $event->secondFactorIdentifier,
100
                $event->secondFactorType,
101
                $this->isIdentityVetted($event->vettingType),
0 ignored issues
show
Bug introduced by
It seems like $event->vettingType can also be of type null; however, parameter $vettingType of Surfnet\StepupMiddleware...tor::isIdentityVetted() does only seem to accept Surfnet\Stepup\Identity\Value\VettingType, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
                $this->isIdentityVetted(/** @scrutinizer ignore-type */ $event->vettingType),
Loading history...
102
            ),
103
        );
104
    }
105
106
    private function isIdentityVetted(VettingType $vettingType): bool
0 ignored issues
show
Coding Style introduced by
Private method name "SecondFactorProjector::isIdentityVetted" must be prefixed with an underscore
Loading history...
107
    {
108
        return $vettingType->type() !== VettingType::TYPE_SELF_ASSERTED_REGISTRATION;
109
    }
110
111
    protected function applyVettedSecondFactorRevokedEvent(VettedSecondFactorRevokedEvent $event): void
112
    {
113
        $secondFactor = $this->repository->findOneBySecondFactorId($event->secondFactorId);
114
115
        if ($secondFactor === null) {
116
            throw new RuntimeException(
117
                sprintf(
118
                    'Expected to find a second factor having secondFactorId "%s", found none.',
119
                    $event->secondFactorId,
120
                ),
121
            );
122
        }
123
124
        $this->repository->remove($secondFactor);
125
    }
126
127
    protected function applyCompliedWithVettedSecondFactorRevocationEvent(
128
        CompliedWithVettedSecondFactorRevocationEvent $event,
129
    ): void {
130
        $secondFactor = $this->repository->findOneBySecondFactorId($event->secondFactorId);
131
132
        if ($secondFactor === null) {
133
            throw new RuntimeException(
134
                sprintf(
135
                    'Expected to find a second factor having secondFactorId "%s", found none.',
136
                    $event->secondFactorId,
137
                ),
138
            );
139
        }
140
141
        $this->repository->remove($secondFactor);
142
    }
143
144
    protected function applyLocalePreferenceExpressedEvent(LocalePreferenceExpressedEvent $event): void
145
    {
146
        $secondFactors = $this->repository->findByIdentityId($event->identityId);
147
148
        foreach ($secondFactors as $secondFactor) {
149
            $secondFactor->displayLocale = (string)$event->preferredLocale;
150
            $this->repository->save($secondFactor);
151
        }
152
    }
153
154
    protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event): void
155
    {
156
        $this->repository->removeByIdentityId($event->identityId);
157
    }
158
}
159