UnverifiedSecondFactor::verifyEmail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 14
rs 9.8666
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
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
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;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Identity\...EmailVerificationWindow was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
41
class UnverifiedSecondFactor extends AbstractSecondFactor
42
{
43
    private ?Identity $identity = null;
44
45
    private ?SecondFactorId $id = null;
46
47
    private ?SecondFactorType $type = null;
48
49
    /**
50
     * @var SecondFactorIdentifier
51
     */
52
    private SecondFactorIdentifier $secondFactorIdentifier;
53
54
    private ?EmailVerificationWindow $verificationWindow = null;
55
56
    private ?string $verificationNonce = null;
57
58
    public static function create(
59
        SecondFactorId          $id,
60
        Identity                $identity,
61
        SecondFactorType        $type,
62
        SecondFactorIdentifier  $secondFactorIdentifier,
63
        EmailVerificationWindow $emailVerificationWindow,
64
        string                  $verificationNonce,
65
    ): self {
66
        if ($verificationNonce === '' || $verificationNonce === '0') {
67
            throw new InvalidArgumentException("'verificationNonce' may not be empty");
68
        }
69
70
        $secondFactor = new self();
71
        $secondFactor->id = $id;
72
        $secondFactor->identity = $identity;
73
        $secondFactor->type = $type;
74
        $secondFactor->secondFactorIdentifier = $secondFactorIdentifier;
75
        $secondFactor->verificationWindow = $emailVerificationWindow;
76
        $secondFactor->verificationNonce = $verificationNonce;
77
78
        return $secondFactor;
79
    }
80
81
    final public function __construct()
82
    {
83
    }
84
85
    public function getId(): ?SecondFactorId
86
    {
87
        return $this->id;
88
    }
89
90
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $verificationNonce should have a doc-comment as per coding-style.
Loading history...
91
     * @return bool
92
     */
93
    public function hasNonce(string $verificationNonce): bool
94
    {
95
        return $this->verificationNonce === $verificationNonce;
96
    }
97
98
    /**
99
     * @return bool
100
     */
101
    public function canBeVerifiedNow(): bool
102
    {
103
        return $this->verificationWindow->isOpen();
0 ignored issues
show
Bug introduced by
The method isOpen() 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

103
        return $this->verificationWindow->/** @scrutinizer ignore-call */ isOpen();

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...
104
    }
105
106
    public function verifyEmail(): void
107
    {
108
        $this->apply(
109
            new EmailVerifiedEvent(
110
                $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

110
                $this->identity->/** @scrutinizer ignore-call */ 
111
                                 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...
111
                $this->identity->getInstitution(),
112
                $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\...iedEvent::__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

112
                /** @scrutinizer ignore-type */ $this->id,
Loading history...
113
                $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\...iedEvent::__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

113
                /** @scrutinizer ignore-type */ $this->type,
Loading history...
114
                $this->secondFactorIdentifier,
115
                DateTime::now(),
116
                OtpGenerator::generate(8),
117
                $this->identity->getCommonName(),
118
                $this->identity->getEmail(),
119
                $this->identity->getPreferredLocale(),
120
            ),
121
        );
122
    }
123
124
    public function revoke(): void
125
    {
126
        $this->apply(
127
            new UnverifiedSecondFactorRevokedEvent(
128
                $this->identity->getId(),
129
                $this->identity->getInstitution(),
130
                $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

130
                /** @scrutinizer ignore-type */ $this->id,
Loading history...
131
                $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

131
                /** @scrutinizer ignore-type */ $this->type,
Loading history...
132
                $this->secondFactorIdentifier,
133
            ),
134
        );
135
    }
136
137
    public function complyWithRevocation(IdentityId $authorityId): void
138
    {
139
        $this->apply(
140
            new CompliedWithUnverifiedSecondFactorRevocationEvent(
141
                $this->identity->getId(),
142
                $this->identity->getInstitution(),
143
                $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

143
                /** @scrutinizer ignore-type */ $this->id,
Loading history...
144
                $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

144
                /** @scrutinizer ignore-type */ $this->type,
Loading history...
145
                $this->secondFactorIdentifier,
146
                $authorityId,
147
            ),
148
        );
149
    }
150
151
    public function asVerified(DateTime $registrationRequestedAt, string $registrationCode): VerifiedSecondFactor
152
    {
153
        return VerifiedSecondFactor::create(
154
            $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

154
            /** @scrutinizer ignore-type */ $this->id,
Loading history...
155
            $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

155
            /** @scrutinizer ignore-type */ $this->identity,
Loading history...
156
            $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

156
            /** @scrutinizer ignore-type */ $this->type,
Loading history...
157
            $this->secondFactorIdentifier,
158
            $registrationRequestedAt,
159
            $registrationCode,
160
        );
161
    }
162
163
    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

163
    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...
164
    {
165
        $secondFactorIdentifierClass = $this->secondFactorIdentifier::class;
166
167
        $this->secondFactorIdentifier = $secondFactorIdentifierClass::unknown();
168
    }
169
170
    public function getType(): SecondFactorType
171
    {
172
        return $this->type;
173
    }
174
175
    public function getIdentifier(): SecondFactorIdentifier
176
    {
177
        return $this->secondFactorIdentifier;
178
    }
179
}
180