VerifiedSecondFactor::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 6
dl 0
loc 17
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\Stepup\Identity\Entity;
20
21
use DateInterval;
22
use Surfnet\Stepup\DateTime\DateTime;
23
use Surfnet\Stepup\Identity\Api\Identity;
24
use Surfnet\Stepup\Identity\Event\CompliedWithVerifiedSecondFactorRevocationEvent;
25
use Surfnet\Stepup\Identity\Event\IdentityForgottenEvent;
26
use Surfnet\Stepup\Identity\Event\SecondFactorVettedEvent;
27
use Surfnet\Stepup\Identity\Event\SecondFactorVettedWithoutTokenProofOfPossession;
28
use Surfnet\Stepup\Identity\Event\VerifiedSecondFactorRevokedEvent;
29
use Surfnet\Stepup\Identity\Value\IdentityId;
30
use Surfnet\Stepup\Identity\Value\SecondFactorId;
31
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifier;
32
use Surfnet\Stepup\Identity\Value\VettingType;
33
use Surfnet\StepupBundle\Service\SecondFactorTypeService;
34
use Surfnet\StepupBundle\Value\SecondFactorType;
35
use Surfnet\StepupBundle\Value\VettingType as StepupVettingType;
36
37
/**
38
 * A second factor whose possession has been proven by the registrant and the registrant's e-mail address has been
39
 * verified. The registrant must visit a registration authority next.
40
 *
41
 * @SuppressWarnings("PHPMD.UnusedPrivateFields")
42
 * @SuppressWarnings("PHPMD.CouplingBetweenObjects")
43
 */
44
class VerifiedSecondFactor extends AbstractSecondFactor
45
{
46
    private ?SecondFactorId $id = null;
47
48
    private ?Identity $identity = null;
49
50
    private ?SecondFactorType $type = null;
51
52
    /**
53
     * @var SecondFactorIdentifier
54
     */
55
    private SecondFactorIdentifier $secondFactorIdentifier;
56
57
    private ?DateTime $registrationRequestedAt = null;
58
59
    private ?string $registrationCode = null;
60
61
    public static function create(
62
        SecondFactorId         $id,
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$id"; 9 found
Loading history...
63
        Identity               $identity,
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$identity"; 15 found
Loading history...
64
        SecondFactorType       $type,
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$type"; 7 found
Loading history...
65
        SecondFactorIdentifier $secondFactorIdentifier,
66
        DateTime               $registrationRequestedAt,
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$registrationRequestedAt"; 15 found
Loading history...
67
        string                 $registrationCode,
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$registrationCode"; 17 found
Loading history...
68
    ): self {
69
        $secondFactor = new self;
0 ignored issues
show
Coding Style introduced by
Parentheses must be used when instantiating a new class
Loading history...
70
        $secondFactor->id = $id;
71
        $secondFactor->identity = $identity;
72
        $secondFactor->type = $type;
73
        $secondFactor->secondFactorIdentifier = $secondFactorIdentifier;
74
        $secondFactor->registrationRequestedAt = $registrationRequestedAt;
75
        $secondFactor->registrationCode = $registrationCode;
76
77
        return $secondFactor;
78
    }
79
80
    final private function __construct()
81
    {
82
    }
83
84
    public function getId(): ?SecondFactorId
85
    {
86
        return $this->id;
87
    }
88
89
    public function hasRegistrationCodeAndIdentifier(
90
        string                 $registrationCode,
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$registrationCode"; 17 found
Loading history...
91
        SecondFactorIdentifier $secondFactorIdentifier,
92
    ): bool {
93
        return strcasecmp($registrationCode, (string)$this->registrationCode) === 0
94
            && $secondFactorIdentifier->equals($this->secondFactorIdentifier);
95
    }
96
97
    /**
98
     * @return bool
99
     */
100
    public function canBeVettedNow(): bool
101
    {
102
        return !DateTime::now()->comesAfter(
103
            $this->registrationRequestedAt
104
                ->add(new DateInterval('P14D'))
0 ignored issues
show
Bug introduced by
The method add() does not exist on null. ( Ignorable by Annotation )

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

104
                ->/** @scrutinizer ignore-call */ 
105
                  add(new DateInterval('P14D'))

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
                ->endOfDay(),
106
        );
107
    }
108
109
    public function vet(bool $provePossessionSkipped, VettingType $type): void
110
    {
111
        if ($provePossessionSkipped) {
112
            $this->apply(
113
                new SecondFactorVettedWithoutTokenProofOfPossession(
114
                    $this->identity->getId(),
0 ignored issues
show
Bug introduced by
The method getId() does not exist on null. ( Ignorable by Annotation )

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

114
                    $this->identity->/** @scrutinizer ignore-call */ 
115
                                     getId(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
                    $this->identity->getNameId(),
116
                    $this->identity->getInstitution(),
117
                    $this->id,
0 ignored issues
show
Bug introduced by
It seems like $this->id can also be of type null; however, parameter $secondFactorId of Surfnet\Stepup\Identity\...ssession::__construct() does only seem to accept Surfnet\Stepup\Identity\Value\SecondFactorId, 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

117
                    /** @scrutinizer ignore-type */ $this->id,
Loading history...
118
                    $this->type,
0 ignored issues
show
Bug introduced by
It seems like $this->type can also be of type null; however, parameter $secondFactorType of Surfnet\Stepup\Identity\...ssession::__construct() does only seem to accept Surfnet\StepupBundle\Value\SecondFactorType, 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

118
                    /** @scrutinizer ignore-type */ $this->type,
Loading history...
119
                    $this->secondFactorIdentifier,
120
                    $this->identity->getCommonName(),
121
                    $this->identity->getEmail(),
122
                    $this->identity->getPreferredLocale(),
123
                    $type,
124
                ),
125
            );
126
            return;
127
        }
128
129
        $this->apply(
130
            new SecondFactorVettedEvent(
131
                $this->identity->getId(),
132
                $this->identity->getNameId(),
133
                $this->identity->getInstitution(),
134
                $this->id,
0 ignored issues
show
Bug introduced by
It seems like $this->id can also be of type null; however, parameter $secondFactorId of Surfnet\Stepup\Identity\...tedEvent::__construct() does only seem to accept Surfnet\Stepup\Identity\Value\SecondFactorId, 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

134
                /** @scrutinizer ignore-type */ $this->id,
Loading history...
135
                $this->type,
0 ignored issues
show
Bug introduced by
It seems like $this->type can also be of type null; however, parameter $secondFactorType of Surfnet\Stepup\Identity\...tedEvent::__construct() does only seem to accept Surfnet\StepupBundle\Value\SecondFactorType, 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

135
                /** @scrutinizer ignore-type */ $this->type,
Loading history...
136
                $this->secondFactorIdentifier,
137
                $this->identity->getCommonName(),
138
                $this->identity->getEmail(),
139
                $this->identity->getPreferredLocale(),
140
                $type,
141
            ),
142
        );
143
    }
144
145
    public function revoke(): void
146
    {
147
        $this->apply(
148
            new VerifiedSecondFactorRevokedEvent(
149
                $this->identity->getId(),
150
                $this->identity->getInstitution(),
151
                $this->id,
0 ignored issues
show
Bug introduced by
It seems like $this->id can also be of type null; however, parameter $secondFactorId of Surfnet\Stepup\Identity\...kedEvent::__construct() does only seem to accept Surfnet\Stepup\Identity\Value\SecondFactorId, 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

151
                /** @scrutinizer ignore-type */ $this->id,
Loading history...
152
                $this->type,
0 ignored issues
show
Bug introduced by
It seems like $this->type can also be of type null; however, parameter $secondFactorType of Surfnet\Stepup\Identity\...kedEvent::__construct() does only seem to accept Surfnet\StepupBundle\Value\SecondFactorType, 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

152
                /** @scrutinizer ignore-type */ $this->type,
Loading history...
153
                $this->secondFactorIdentifier,
154
            ),
155
        );
156
    }
157
158
    public function complyWithRevocation(IdentityId $authorityId): void
159
    {
160
        $this->apply(
161
            new CompliedWithVerifiedSecondFactorRevocationEvent(
162
                $this->identity->getId(),
163
                $this->identity->getInstitution(),
164
                $this->id,
0 ignored issues
show
Bug introduced by
It seems like $this->id can also be of type null; however, parameter $secondFactorId of Surfnet\Stepup\Identity\...ionEvent::__construct() does only seem to accept Surfnet\Stepup\Identity\Value\SecondFactorId, 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

164
                /** @scrutinizer ignore-type */ $this->id,
Loading history...
165
                $this->type,
0 ignored issues
show
Bug introduced by
It seems like $this->type can also be of type null; however, parameter $secondFactorType of Surfnet\Stepup\Identity\...ionEvent::__construct() does only seem to accept Surfnet\StepupBundle\Value\SecondFactorType, 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

165
                /** @scrutinizer ignore-type */ $this->type,
Loading history...
166
                $this->secondFactorIdentifier,
167
                $authorityId,
168
            ),
169
        );
170
    }
171
172
    public function asVetted(VettingType $vettingType): VettedSecondFactor
173
    {
174
        return VettedSecondFactor::create(
175
            $this->id,
0 ignored issues
show
Bug introduced by
It seems like $this->id can also be of type null; however, parameter $id of Surfnet\Stepup\Identity\...dSecondFactor::create() does only seem to accept Surfnet\Stepup\Identity\Value\SecondFactorId, 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

175
            /** @scrutinizer ignore-type */ $this->id,
Loading history...
176
            $this->identity,
0 ignored issues
show
Bug introduced by
It seems like $this->identity can also be of type null; however, parameter $identity of Surfnet\Stepup\Identity\...dSecondFactor::create() does only seem to accept Surfnet\Stepup\Identity\Api\Identity, 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

176
            /** @scrutinizer ignore-type */ $this->identity,
Loading history...
177
            $this->type,
0 ignored issues
show
Bug introduced by
It seems like $this->type can also be of type null; however, parameter $type of Surfnet\Stepup\Identity\...dSecondFactor::create() does only seem to accept Surfnet\StepupBundle\Value\SecondFactorType, 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

177
            /** @scrutinizer ignore-type */ $this->type,
Loading history...
178
            $this->secondFactorIdentifier,
179
            $vettingType,
180
        );
181
    }
182
183
    public function getLoaLevel(SecondFactorTypeService $secondFactorTypeService): float
184
    {
185
        return $secondFactorTypeService->getLevel($this->type, new StepupVettingType(VettingType::TYPE_UNKNOWN));
0 ignored issues
show
Bug introduced by
It seems like $this->type can also be of type null; however, parameter $secondFactorType of Surfnet\StepupBundle\Ser...TypeService::getLevel() does only seem to accept Surfnet\StepupBundle\Value\SecondFactorType, 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

185
        return $secondFactorTypeService->getLevel(/** @scrutinizer ignore-type */ $this->type, new StepupVettingType(VettingType::TYPE_UNKNOWN));
Loading history...
186
    }
187
188
    protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event): void
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

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

188
    protected function applyIdentityForgottenEvent(/** @scrutinizer ignore-unused */ IdentityForgottenEvent $event): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
189
    {
190
        $secondFactorIdentifierClass = $this->secondFactorIdentifier::class;
191
192
        $identifier = $secondFactorIdentifierClass::unknown();
193
        assert($identifier instanceof SecondFactorIdentifier);
194
        $this->secondFactorIdentifier = $identifier;
195
    }
196
197
    public function getType(): SecondFactorType
198
    {
199
        return $this->type;
200
    }
201
202
    public function getIdentifier(): SecondFactorIdentifier
203
    {
204
        return $this->secondFactorIdentifier;
205
    }
206
}
207