forked from rollerworks/split-token
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractSplitTokenFactory.php
More file actions
50 lines (38 loc) · 1.37 KB
/
AbstractSplitTokenFactory.php
File metadata and controls
50 lines (38 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
declare(strict_types=1);
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
namespace Rollerworks\Component\SplitToken;
use Psr\Clock\ClockInterface;
use Symfony\Contracts\Service\Attribute\Required;
abstract class AbstractSplitTokenFactory implements SplitTokenFactory
{
private ?\DateInterval $defaultLifeTime = null;
private ?ClockInterface $clock;
public function __construct(\DateInterval | string | null $defaultLifeTime = null)
{
if (\is_string($defaultLifeTime)) {
$defaultLifeTime = new \DateInterval($defaultLifeTime);
}
$this->defaultLifeTime = $defaultLifeTime;
}
#[Required]
public function setClock(ClockInterface $clock): void
{
$this->clock = $clock;
}
final protected function getExpirationTimestamp(\DateTimeImmutable | \DateInterval | null $expiration = null): ?\DateTimeImmutable
{
if ($expiration instanceof \DateTimeImmutable) {
return $expiration;
}
$expiration ??= $this->defaultLifeTime;
if ($expiration !== null) {
return (isset($this->clock) ? $this->clock->now() : new \DateTimeImmutable('now'))->add($expiration);
}
return null;
}
}