Completed
Push — master ( 5467e4...183ea4 )
by Boy
05:06 queued 01:02
created

UnverifiedSecondFactor::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
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\CompliedWithUnverifiedSecondFactorRevocationEvent;
25
use Surfnet\Stepup\Identity\Event\EmailVerifiedEvent;
26
use Surfnet\Stepup\Identity\Event\IdentityForgottenEvent;
27
use Surfnet\Stepup\Identity\Event\UnverifiedSecondFactorRevokedEvent;
28
use Surfnet\Stepup\Identity\Value\EmailVerificationWindow;
29
use Surfnet\Stepup\Identity\Value\IdentityId;
30
use Surfnet\Stepup\Identity\Value\SecondFactorId;
31
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifier;
32
use Surfnet\StepupBundle\Security\OtpGenerator;
33
use Surfnet\StepupBundle\Value\SecondFactorType;
34
35
/**
36
 * A second factor whose possession has been proven by the registrant. The registrant must verify his/her e-mail
37
 * address to verify this second factor.
38
 *
39
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
40
 */
41
class UnverifiedSecondFactor extends AbstractSecondFactor
42
{
43
    /**
44
     * @var \Surfnet\Stepup\Identity\Api\Identity
45
     */
46
    private $identity;
47
48
    /**
49
     * @var \Surfnet\Stepup\Identity\Value\SecondFactorId
50
     */
51
    private $id;
52
53
    /**
54
     * @var \Surfnet\StepupBundle\Value\SecondFactorType
55
     */
56
    private $type;
57
58
    /**
59
     * @var \Surfnet\Stepup\Identity\Value\SecondFactorIdentifier
60
     */
61
    private $secondFactorIdentifier;
62
63
    /**
64
     * @var \Surfnet\Stepup\Identity\Value\EmailVerificationWindow
65
     */
66
    private $verificationWindow;
67
68
    /**
69
     * @var string
70
     */
71
    private $verificationNonce;
72
73
    /**
74
     * @param SecondFactorId          $id
75
     * @param Identity                $identity
76
     * @param SecondFactorType        $type
77
     * @param SecondFactorIdentifier  $secondFactorIdentifier
78
     * @param EmailVerificationWindow $emailVerificationWindow
79
     * @param string                  $verificationNonce
80
     * @return UnverifiedSecondFactor
81
     */
82 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...
83
        SecondFactorId $id,
84
        Identity $identity,
85
        SecondFactorType $type,
86
        $secondFactorIdentifier,
87
        EmailVerificationWindow $emailVerificationWindow,
88
        $verificationNonce
89
    ) {
90
        if (!is_string($verificationNonce)) {
91
            throw InvalidArgumentException::invalidType('string', 'verificationNonce', $verificationNonce);
92
        }
93
94
        if (empty($verificationNonce)) {
95
            throw new InvalidArgumentException("'verificationNonce' may not be empty");
96
        }
97
98
        $secondFactor = new self();
99
        $secondFactor->id = $id;
100
        $secondFactor->identity = $identity;
101
        $secondFactor->type = $type;
102
        $secondFactor->secondFactorIdentifier = $secondFactorIdentifier;
103
        $secondFactor->verificationWindow = $emailVerificationWindow;
104
        $secondFactor->verificationNonce = $verificationNonce;
105
106
        return $secondFactor;
107
    }
108
109
    final public function __construct()
110
    {
111
    }
112
113
    /**
114
     * @return SecondFactorId
115
     */
116
    public function getId()
117
    {
118
        return $this->id;
119
    }
120
121
    /**
122
     * @param string $verificationNonce
123
     * @return bool
124
     */
125
    public function hasNonce($verificationNonce)
126
    {
127
        return $this->verificationNonce === $verificationNonce;
128
    }
129
130
    /**
131
     * @return bool
132
     */
133
    public function canBeVerifiedNow()
134
    {
135
        return $this->verificationWindow->isOpen();
136
    }
137
138
    public function verifyEmail()
139
    {
140
        $this->apply(
141
            new EmailVerifiedEvent(
142
                $this->identity->getId(),
143
                $this->identity->getInstitution(),
144
                $this->id,
145
                $this->type,
146
                $this->secondFactorIdentifier,
147
                DateTime::now(),
148
                OtpGenerator::generate(8),
149
                $this->identity->getCommonName(),
150
                $this->identity->getEmail(),
151
                $this->identity->getPreferredLocale()
152
            )
153
        );
154
    }
155
156 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...
157
    {
158
        $this->apply(
159
            new UnverifiedSecondFactorRevokedEvent(
160
                $this->identity->getId(),
161
                $this->identity->getInstitution(),
162
                $this->id,
163
                $this->type,
164
                $this->secondFactorIdentifier
165
            )
166
        );
167
    }
168
169 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...
170
    {
171
        $this->apply(
172
            new CompliedWithUnverifiedSecondFactorRevocationEvent(
173
                $this->identity->getId(),
174
                $this->identity->getInstitution(),
175
                $this->id,
176
                $this->type,
177
                $this->secondFactorIdentifier,
178
                $authorityId
179
            )
180
        );
181
    }
182
183
    /**
184
     * @param DateTime $registrationRequestedAt
185
     * @param string   $registrationCode
186
     * @return VerifiedSecondFactor
187
     */
188
    public function asVerified($registrationRequestedAt, $registrationCode)
189
    {
190
        return VerifiedSecondFactor::create(
191
            $this->id,
192
            $this->identity,
193
            $this->type,
194
            $this->secondFactorIdentifier,
195
            $registrationRequestedAt,
196
            $registrationCode
197
        );
198
    }
199
200
    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...
201
    {
202
        $secondFactorIdentifierClass = get_class($this->secondFactorIdentifier);
203
204
        $this->secondFactorIdentifier = $secondFactorIdentifierClass::unknown();
205
    }
206
207
    protected function getType()
208
    {
209
        return $this->type;
210
    }
211
}
212