1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Distribution\Resolver; |
6
|
|
|
|
7
|
|
|
use Spiral\Distribution\ExpirationAwareResolverInterface; |
8
|
|
|
use Spiral\Distribution\Internal\DateTimeFactory; |
9
|
|
|
use Spiral\Distribution\Internal\DateTimeFactoryInterface; |
10
|
|
|
use Spiral\Distribution\Internal\DateTimeIntervalFactory; |
11
|
|
|
use Spiral\Distribution\Internal\DateTimeIntervalFactoryInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @psalm-import-type DateIntervalFormat from DateTimeIntervalFactoryInterface |
15
|
|
|
*/ |
16
|
|
|
abstract class ExpirationAwareResolver extends UriResolver implements ExpirationAwareResolverInterface |
17
|
|
|
{ |
18
|
|
|
protected const DEFAULT_EXPIRATION_INTERVAL = 'PT60M'; |
19
|
|
|
|
20
|
|
|
protected \DateInterval $expiration; |
21
|
|
|
protected DateTimeIntervalFactoryInterface $intervals; |
22
|
|
|
protected DateTimeFactoryInterface $dates; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* ExpirationAwareResolver constructor. |
26
|
|
|
*/ |
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->dates = new DateTimeFactory(); |
30
|
|
|
$this->intervals = new DateTimeIntervalFactory($this->dates); |
31
|
|
|
$this->expiration = $this->intervals->create(static::DEFAULT_EXPIRATION_INTERVAL); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getExpirationDate(): \DateInterval |
35
|
|
|
{ |
36
|
|
|
return $this->expiration; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
public function withExpirationDate(mixed $duration): ExpirationAwareResolverInterface |
43
|
|
|
{ |
44
|
|
|
$self = clone $this; |
45
|
|
|
$self->expiration = $self->intervals->create($duration); |
46
|
|
|
|
47
|
|
|
return $self; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function withDateTimeFactory(DateTimeFactoryInterface $factory): self |
51
|
|
|
{ |
52
|
|
|
$self = clone $this; |
53
|
|
|
$self->dates = $factory; |
54
|
|
|
|
55
|
|
|
return $self; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function withDateTimeIntervalFactory(DateTimeIntervalFactoryInterface $factory): self |
59
|
|
|
{ |
60
|
|
|
$self = clone $this; |
61
|
|
|
$self->intervals = $factory; |
62
|
|
|
|
63
|
|
|
return $self; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function getExpirationDateTime(mixed $expiration): \DateTimeInterface |
67
|
|
|
{ |
68
|
|
|
$expiration = $this->resolveExpirationInterval($expiration); |
69
|
|
|
|
70
|
|
|
return $this->intervals->toDateTime($expiration); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function resolveExpirationInterval(mixed $expiration): \DateInterval |
74
|
|
|
{ |
75
|
|
|
if ($expiration === null) { |
76
|
|
|
return $this->expiration; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->intervals->create($expiration); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|