Completed
Push — feature/helper-methods-verif-s... ( 6da473 )
by
unknown
03:25
created

VerifiedSecondFactor::hasExpired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\StepupMiddlewareClientBundle\Identity\Dto;
20
21
use DateTime;
22
use DateInterval;
23
use Surfnet\StepupMiddlewareClientBundle\Dto\Dto;
24
use Symfony\Component\Validator\Constraints as Assert;
25
26
class VerifiedSecondFactor implements Dto
27
{
28
    /**
29
     * @Assert\NotBlank(message="middleware_client.dto.verified_second_factor.id.must_not_be_blank")
30
     * @Assert\Type(type="string", message="middleware_client.dto.verified_second_factor.id.must_be_string")
31
     * @var string
32
     */
33
    public $id;
34
35
    /**
36
     * @Assert\NotBlank(message="middleware_client.dto.verified_second_factor.type.must_not_be_blank")
37
     * @Assert\Type(type="string", message="middleware_client.dto.verified_second_factor.type.must_be_string")
38
     * @var string
39
     */
40
    public $type;
41
42
    /**
43
     * @Assert\NotBlank(
44
     *     message="middleware_client.dto.verified_second_factor.second_factor_identifier.must_not_be_blank"
45
     * )
46
     * @Assert\Type(
47
     *     type="string",
48
     *     message="middleware_client.dto.verified_second_factor.second_factor_identifier.must_be_string"
49
     * )
50
     * @var string
51
     */
52
    public $secondFactorIdentifier;
53
54
    /**
55
     * @Assert\NotBlank(message="middleware_client.dto.verified_second_factor.registration_code.must_not_be_blank")
56
     * @Assert\Type(
57
     *     type="string",
58
     *     message="middleware_client.dto.verified_second_factor.registration_code.must_be_string"
59
     * )
60
     * @var string
61
     */
62
    public $registrationCode;
63
64
65
    /**
66
     * @var DateTime
67
     */
68
    public $registrationRequestedAt;
69
70
    /**
71
     * @Assert\NotBlank(message="middleware_client.dto.verified_second_factor.identity_id.must_not_be_blank")
72
     * @Assert\Type(
73
     *     type="string",
74
     *     message="middleware_client.dto.verified_second_factor.identity_id.must_be_string"
75
     * )
76
     * @var string
77
     */
78
    public $identityId;
79
80
    /**
81
     * @Assert\NotBlank(message="middleware_client.dto.verified_second_factor.institution.must_not_be_blank")
82
     * @Assert\Type(
83
     *     type="string",
84
     *     message="middleware_client.dto.verified_second_factor.institution.must_be_string"
85
     * )
86
     * @var string
87
     */
88
    public $institution;
89
90
    /**
91
     * @Assert\NotBlank(message="middleware_client.dto.verified_second_factor.common_name.must_not_be_blank")
92
     * @Assert\Type(
93
     *     type="string",
94
     *     message="middleware_client.dto.verified_second_factor.common_name.must_be_string"
95
     * )
96
     * @var string
97
     */
98
    public $commonName;
99
100
    /**
101
     * The current time, this field can be set for testing purposes
102
     * @var DateTime
103
     */
104
    private $now;
105
106
    public static function fromData(array $data, $now = null)
107
    {
108
        $secondFactor = new self();
109
        $secondFactor->id = $data['id'];
110
        $secondFactor->type = $data['type'];
111
        $secondFactor->secondFactorIdentifier = $data['second_factor_identifier'];
112
        $secondFactor->registrationCode = $data['registration_code'];
113
        $secondFactor->registrationRequestedAt = new DateTime(
114
            $data['registration_requested_at']
115
        );
116
117
        $secondFactor->identityId = $data['identity_id'];
118
        $secondFactor->institution = $data['institution'];
119
        $secondFactor->commonName = $data['common_name'];
120
121
        if (isset($now) && $now instanceof DateTime) {
122
            $secondFactor->now = $now;
123
        }
124
125
        return $secondFactor;
126
    }
127
128
    public function expiresAt()
129
    {
130
        $expirationDate = clone $this->registrationRequestedAt;
131
        return $expirationDate->add(
132
            new DateInterval('P14D')
133
        );
134
    }
135
136
    public function hasExpired()
137
    {
138
        $now = $this->getNow();
139
        return $this->expiresAt() <= $now;
140
    }
141
142
    private function getNow()
143
    {
144
        if (is_null($this->now)) {
145
            $this->now = new DateTime();
146
        }
147
        return $this->now;
148
    }
149
}
150