1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2018 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\StepupBundle\DateTime; |
20
|
|
|
|
21
|
|
|
use DateInterval; |
22
|
|
|
use DateTime as CoreDateTime; |
23
|
|
|
use Exception; |
24
|
|
|
use Surfnet\StepupBundle\Exception\InvalidArgumentException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Helps with testing if a registration date of a token is within the expiration time frame. |
28
|
|
|
*/ |
29
|
|
|
class RegistrationExpirationHelper |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* The current time, this field can be set for testing purposes |
33
|
|
|
* @var DateTime |
34
|
|
|
*/ |
35
|
|
|
private $now; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string a DateInterval complient $interval_spec string |
39
|
|
|
*/ |
40
|
|
|
private $expirationWindow; |
41
|
|
|
|
42
|
|
|
public function __construct(CoreDateTime $now = null, $expirationWindow = 'P14D') |
43
|
|
|
{ |
44
|
|
|
$this->now = $now; |
|
|
|
|
45
|
|
|
|
46
|
|
|
try { |
47
|
|
|
$this->expirationWindow = new DateInterval($expirationWindow); |
|
|
|
|
48
|
|
|
} catch (Exception $e) { |
49
|
|
|
throw new InvalidArgumentException( |
50
|
|
|
sprintf( |
51
|
|
|
'The provided DateInterval interval specification ("%s") is invalid', |
52
|
|
|
$expirationWindow |
53
|
|
|
) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function expiresAt(CoreDateTime $registeredAt) |
59
|
|
|
{ |
60
|
|
|
$registrationDate = clone $registeredAt; |
61
|
|
|
return $registrationDate->add($this->expirationWindow); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function hasExpired(CoreDateTime $registeredAt) |
65
|
|
|
{ |
66
|
|
|
$now = $this->getNow(); |
67
|
|
|
return $this->expiresAt($registeredAt) <= $now; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function getNow() |
71
|
|
|
{ |
72
|
|
|
if (is_null($this->now)) { |
73
|
|
|
$this->now = new CoreDateTime(); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
return $this->now; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.