applyIdentityForgottenEvent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\GatewayBundle\Projector;
20
21
use Surfnet\Stepup\Identity\Event\CompliedWithVettedSecondFactorRevocationEvent;
22
use Surfnet\Stepup\Identity\Event\IdentityForgottenEvent;
23
use Surfnet\Stepup\Identity\Event\LocalePreferenceExpressedEvent;
24
use Surfnet\Stepup\Identity\Event\SecondFactorMigratedEvent;
25
use Surfnet\Stepup\Identity\Event\SecondFactorVettedEvent;
26
use Surfnet\Stepup\Identity\Event\SecondFactorVettedWithoutTokenProofOfPossession;
27
use Surfnet\Stepup\Identity\Event\VettedSecondFactorRevokedEvent;
28
use Surfnet\Stepup\Identity\Event\YubikeySecondFactorBootstrappedEvent;
29
use Surfnet\Stepup\Identity\Value\VettingType;
30
use Surfnet\Stepup\Projector\Projector;
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
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
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