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

VerifiedSecondFactor::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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 Surfnet\Stepup\DateTime\DateTime;
22
use Surfnet\Stepup\Exception\InvalidArgumentException;
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\ImplicitlyVerifiedByIdp;
27
use Surfnet\Stepup\Identity\Event\SecondFactorVettedEvent;
28
use Surfnet\Stepup\Identity\Event\VerifiedSecondFactorRevokedEvent;
29
use Surfnet\Stepup\Identity\Value\DocumentNumber;
30
use Surfnet\Stepup\Identity\Value\IdentityId;
31
use Surfnet\Stepup\Identity\Value\SecondFactorId;
32
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifier;
33
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifierFactory;
34
use Surfnet\StepupBundle\Security\OtpGenerator;
35
use Surfnet\StepupBundle\Value\SecondFactorType;
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
    /**
47
     * @var \Surfnet\Stepup\Identity\Value\SecondFactorId
48
     */
49
    private $id;
50
51
    /**
52
     * @var \Surfnet\Stepup\Identity\Api\Identity
53
     */
54
    private $identity;
55
56
    /**
57
     * @var \Surfnet\StepupBundle\Value\SecondFactorType
58
     */
59
    private $type;
60
61
    /**
62
     * @var \Surfnet\Stepup\Identity\Value\SecondFactorIdentifier
63
     */
64
    private $secondFactorIdentifier;
65
66
    /**
67
     * @var \Surfnet\Stepup\DateTime\DateTime
68
     */
69
    private $registrationRequestedAt;
70
71
    /**
72
     * @var string
73
     */
74
    private $registrationCode;
75
76
    /**
77
     * @param SecondFactorId $id
78
     * @param Identity $identity
79
     * @param SecondFactorType $type
80
     * @param SecondFactorIdentifier $secondFactorIdentifier
81
     * @param DateTime $registrationRequestedAt
82
     * @param string $registrationCode
83
     * @return self
84
     */
85 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...
86
        SecondFactorId $id,
87
        Identity $identity,
88
        SecondFactorType $type,
89
        SecondFactorIdentifier $secondFactorIdentifier,
90
        DateTime $registrationRequestedAt,
91
        $registrationCode
92
    ) {
93
        if (!is_string($registrationCode)) {
94
            throw InvalidArgumentException::invalidType('string', 'registrationCode', $registrationCode);
95
        }
96
97
        $secondFactor = new self;
98
        $secondFactor->id = $id;
99
        $secondFactor->identity = $identity;
100
        $secondFactor->type = $type;
101
        $secondFactor->secondFactorIdentifier = $secondFactorIdentifier;
102
        $secondFactor->registrationRequestedAt = $registrationRequestedAt;
103
        $secondFactor->registrationCode = $registrationCode;
104
105
        return $secondFactor;
106
    }
107
108
    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...
109
    {
110
    }
111
112
    /**
113
     * @return SecondFactorId
114
     */
115
    public function getId()
116
    {
117
        return $this->id;
118
    }
119
120
    /**
121
     * @param string $registrationCode
122
     * @param SecondFactorIdentifier $secondFactorIdentifier
123
     * @return bool
124
     */
125
    public function hasRegistrationCodeAndIdentifier($registrationCode, SecondFactorIdentifier $secondFactorIdentifier)
126
    {
127
        return strcasecmp($registrationCode, $this->registrationCode) === 0
128
            && $secondFactorIdentifier->equals($this->secondFactorIdentifier);
129
    }
130
131
    public function verifyImplicitly()
132
    {
133
        $this->apply(
134
            new ImplicitlyVerifiedByIdp(
135
                $this->identity->getId(),
136
                $this->identity->getInstitution(),
137
                $this->id,
138
                $this->type,
139
                $this->secondFactorIdentifier,
140
                DateTime::now(),
141
                OtpGenerator::generate(8)
142
            )
143
        );
144
    }
145
146
    /**
147
     * @return bool
148
     */
149
    public function canBeVettedNow()
150
    {
151
        return !DateTime::now()->comesAfter(
152
            $this->registrationRequestedAt
153
                ->add(new \DateInterval('P14D'))
154
                ->endOfDay()
155
        );
156
    }
157
158
    public function vet(DocumentNumber $documentNumber)
159
    {
160
        $this->apply(
161
            new SecondFactorVettedEvent(
162
                $this->identity->getId(),
163
                $this->identity->getNameId(),
164
                $this->identity->getInstitution(),
165
                $this->id,
166
                $this->type,
167
                $this->secondFactorIdentifier,
168
                $documentNumber,
169
                $this->identity->getCommonName(),
170
                $this->identity->getEmail(),
171
                $this->identity->getPreferredLocale()
172
            )
173
        );
174
    }
175
176 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...
177
    {
178
        $this->apply(
179
            new VerifiedSecondFactorRevokedEvent(
180
                $this->identity->getId(),
181
                $this->identity->getInstitution(),
182
                $this->id,
183
                $this->type,
184
                $this->secondFactorIdentifier
185
            )
186
        );
187
    }
188
189 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...
190
    {
191
        $this->apply(
192
            new CompliedWithVerifiedSecondFactorRevocationEvent(
193
                $this->identity->getId(),
194
                $this->identity->getInstitution(),
195
                $this->id,
196
                $this->type,
197
                $this->secondFactorIdentifier,
198
                $authorityId
199
            )
200
        );
201
    }
202
203
    /**
204
     * @return VettedSecondFactor
205
     */
206
    public function asVetted()
207
    {
208
        return VettedSecondFactor::create(
209
            $this->id,
210
            $this->identity,
211
            $this->type,
212
            $this->secondFactorIdentifier
213
        );
214
    }
215
216
    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...
217
    {
218
        $secondFactorIdentifierClass = get_class($this->secondFactorIdentifier);
219
220
        $this->secondFactorIdentifier = $secondFactorIdentifierClass::unknown();
221
    }
222
223
    public function getType()
224
    {
225
        return $this->type;
226
    }
227
}
228