|
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; |
|
|
|
|
|
|
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(DateTime $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(DateTime $registeredAt) |
|
59
|
|
|
{ |
|
60
|
|
|
$registrationDate = clone $registeredAt; |
|
61
|
|
|
return $registrationDate->add($this->expirationWindow); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function hasExpired(DateTime $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 DateTime(); |
|
74
|
|
|
} |
|
75
|
|
|
return $this->now; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: