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
|
|
|
* Class RegistrationExpirationHelper |
28
|
|
|
* @package Surfnet\StepupBundle\DateTime |
29
|
|
|
*/ |
30
|
|
|
class RegistrationExpirationHelper |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* The current time, this field can be set for testing purposes |
34
|
|
|
* @var DateTime |
35
|
|
|
*/ |
36
|
|
|
private $now; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string a DateInterval complient $interval_spec string |
40
|
|
|
*/ |
41
|
|
|
private $expirationWindow; |
42
|
|
|
|
43
|
|
|
public function __construct(DateTime $now = null, $expirationWindow = 'P14D') |
44
|
|
|
{ |
45
|
|
|
$this->now = $now; |
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
$this->expirationWindow = new DateInterval($expirationWindow); |
|
|
|
|
49
|
|
|
} catch (Exception $e) { |
50
|
|
|
throw new InvalidArgumentException( |
51
|
|
|
sprintf( |
52
|
|
|
'The provided DateInterval interval specification ("%s") is invalid', |
53
|
|
|
$expirationWindow |
54
|
|
|
) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function expiresAt(DateTime $registeredAt) |
60
|
|
|
{ |
61
|
|
|
$registrationDate = clone $registeredAt; |
62
|
|
|
return $registrationDate->add($this->expirationWindow); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function hasExpired(DateTime $registeredAt) |
66
|
|
|
{ |
67
|
|
|
$now = $this->getNow(); |
68
|
|
|
return $this->expiresAt($registeredAt) <= $now; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function getNow() |
72
|
|
|
{ |
73
|
|
|
if (is_null($this->now)) { |
74
|
|
|
$this->now = new DateTime(); |
75
|
|
|
} |
76
|
|
|
return $this->now; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: