Completed
Push — feature/self-vet ( 66ee82...7edffb )
by Michiel
03:58
created

VerifiedSecondFactor::getLoaLevel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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\Stepup\Identity\Entity;
20
21
use DateInterval;
22
use Surfnet\Stepup\DateTime\DateTime;
23
use Surfnet\Stepup\Exception\InvalidArgumentException;
24
use Surfnet\Stepup\Identity\Api\Identity;
25
use Surfnet\Stepup\Identity\Event\CompliedWithVerifiedSecondFactorRevocationEvent;
26
use Surfnet\Stepup\Identity\Event\IdentityForgottenEvent;
27
use Surfnet\Stepup\Identity\Event\SecondFactorVettedWithoutTokenProofOfPossession;
28
use Surfnet\Stepup\Identity\Event\SecondFactorVettedEvent;
29
use Surfnet\Stepup\Identity\Event\VerifiedSecondFactorRevokedEvent;
30
use Surfnet\Stepup\Identity\Value\DocumentNumber;
31
use Surfnet\Stepup\Identity\Value\IdentityId;
32
use Surfnet\Stepup\Identity\Value\SecondFactorId;
33
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifier;
34
use Surfnet\Stepup\Identity\Value\VettingType;
35
use Surfnet\StepupBundle\Service\SecondFactorTypeService;
36
use Surfnet\StepupBundle\Value\Loa;
37
use Surfnet\StepupBundle\Value\SecondFactorType;
38
39
/**
40
 * A second factor whose possession has been proven by the registrant and the registrant's e-mail address has been
41
 * verified. The registrant must visit a registration authority next.
42
 *
43
 * @SuppressWarnings(PHPMD.UnusedPrivateFields)
44
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
45
 */
46
class VerifiedSecondFactor extends AbstractSecondFactor
47
{
48
    /**
49
     * @var \Surfnet\Stepup\Identity\Value\SecondFactorId
50
     */
51
    private $id;
52
53
    /**
54
     * @var \Surfnet\Stepup\Identity\Api\Identity
55
     */
56
    private $identity;
57
58
    /**
59
     * @var \Surfnet\StepupBundle\Value\SecondFactorType
60
     */
61
    private $type;
62
63
    /**
64
     * @var \Surfnet\Stepup\Identity\Value\SecondFactorIdentifier
65
     */
66
    private $secondFactorIdentifier;
67
68
    /**
69
     * @var \Surfnet\Stepup\DateTime\DateTime
70
     */
71
    private $registrationRequestedAt;
72
73
    /**
74
     * @var string
75
     */
76
    private $registrationCode;
77
78
    /**
79
     * @param SecondFactorId $id
80
     * @param Identity $identity
81
     * @param SecondFactorType $type
82
     * @param SecondFactorIdentifier $secondFactorIdentifier
83
     * @param DateTime $registrationRequestedAt
84
     * @param string $registrationCode
85
     * @return self
86
     */
87 View Code Duplication
    public static function create(
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...
88
        SecondFactorId $id,
89
        Identity $identity,
90
        SecondFactorType $type,
91
        SecondFactorIdentifier $secondFactorIdentifier,
92
        DateTime $registrationRequestedAt,
93
        $registrationCode
94
    ) {
95
        if (!is_string($registrationCode)) {
96
            throw InvalidArgumentException::invalidType('string', 'registrationCode', $registrationCode);
97
        }
98
99
        $secondFactor = new self;
100
        $secondFactor->id = $id;
101
        $secondFactor->identity = $identity;
102
        $secondFactor->type = $type;
103
        $secondFactor->secondFactorIdentifier = $secondFactorIdentifier;
104
        $secondFactor->registrationRequestedAt = $registrationRequestedAt;
105
        $secondFactor->registrationCode = $registrationCode;
106
107
        return $secondFactor;
108
    }
109
110
    final private function __construct()
0 ignored issues
show
introduced by
Instead of declaring the constructor as final, maybe you should declare the entire class as final.
Loading history...
111
    {
112
    }
113
114
    /**
115
     * @return SecondFactorId
116
     */
117
    public function getId()
118
    {
119
        return $this->id;
120
    }
121
122
    /**
123
     * @param string $registrationCode
124
     * @param SecondFactorIdentifier $secondFactorIdentifier
125
     * @return bool
126
     */
127
    public function hasRegistrationCodeAndIdentifier($registrationCode, SecondFactorIdentifier $secondFactorIdentifier)
128
    {
129
        return strcasecmp($registrationCode, $this->registrationCode) === 0
130
            && $secondFactorIdentifier->equals($this->secondFactorIdentifier);
131
    }
132
133
    /**
134
     * @return bool
135
     */
136
    public function canBeVettedNow()
137
    {
138
        return !DateTime::now()->comesAfter(
139
            $this->registrationRequestedAt
140
                ->add(new DateInterval('P14D'))
141
                ->endOfDay()
142
        );
143
    }
144
145
    public function vet($provePossessionSkipped, VettingType $type)
146
    {
147
        if ($provePossessionSkipped) {
148
            $this->apply(
149
                new SecondFactorVettedWithoutTokenProofOfPossession(
150
                    $this->identity->getId(),
151
                    $this->identity->getNameId(),
152
                    $this->identity->getInstitution(),
153
                    $this->id,
154
                    $this->type,
155
                    $this->secondFactorIdentifier,
156
                    $this->identity->getCommonName(),
157
                    $this->identity->getEmail(),
158
                    $this->identity->getPreferredLocale(),
159
                    $type
160
                )
161
            );
162
            return;
163
        }
164
165
        $this->apply(
166
            new SecondFactorVettedEvent(
167
                $this->identity->getId(),
168
                $this->identity->getNameId(),
169
                $this->identity->getInstitution(),
170
                $this->id,
171
                $this->type,
172
                $this->secondFactorIdentifier,
173
                $this->identity->getCommonName(),
174
                $this->identity->getEmail(),
175
                $this->identity->getPreferredLocale(),
176
                $type
177
            )
178
        );
179
    }
180
181 View Code Duplication
    public function revoke()
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...
182
    {
183
        $this->apply(
184
            new VerifiedSecondFactorRevokedEvent(
185
                $this->identity->getId(),
186
                $this->identity->getInstitution(),
187
                $this->id,
188
                $this->type,
189
                $this->secondFactorIdentifier
190
            )
191
        );
192
    }
193
194 View Code Duplication
    public function complyWithRevocation(IdentityId $authorityId)
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...
195
    {
196
        $this->apply(
197
            new CompliedWithVerifiedSecondFactorRevocationEvent(
198
                $this->identity->getId(),
199
                $this->identity->getInstitution(),
200
                $this->id,
201
                $this->type,
202
                $this->secondFactorIdentifier,
203
                $authorityId
204
            )
205
        );
206
    }
207
208
    /**
209
     * @return VettedSecondFactor
210
     */
211
    public function asVetted()
212
    {
213
        return VettedSecondFactor::create(
214
            $this->id,
215
            $this->identity,
216
            $this->type,
217
            $this->secondFactorIdentifier
218
        );
219
    }
220
221
    public function getLoaLevel(SecondFactorTypeService $secondFactorTypeService): int
222
    {
223
        return $secondFactorTypeService->getLevel($this->type);
224
    }
225
226
    protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

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

Loading history...
227
    {
228
        $secondFactorIdentifierClass = get_class($this->secondFactorIdentifier);
229
230
        $this->secondFactorIdentifier = $secondFactorIdentifierClass::unknown();
231
    }
232
233
    public function getType()
234
    {
235
        return $this->type;
236
    }
237
}
238