Passed
Pull Request — master (#407)
by Kirill
07:03
created

ExpirationAwareResolver::getExpirationDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 1
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Distribution\Resolver;
13
14
use Spiral\Distribution\ExpirationAwareResolverInterface;
15
use Spiral\Distribution\Internal\DateTimeFactory;
16
use Spiral\Distribution\Internal\DateTimeFactoryInterface;
17
use Spiral\Distribution\Internal\DateTimeIntervalFactory;
18
use Spiral\Distribution\Internal\DateTimeIntervalFactoryInterface;
19
20
/**
21
 * @psalm-import-type DateIntervalFormat from DateTimeIntervalFactoryInterface
22
 */
23
abstract class ExpirationAwareResolver extends UriResolver implements ExpirationAwareResolverInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    protected const DEFAULT_EXPIRATION_INTERVAL = 'PT60M';
29
30
    /**
31
     * @var \DateInterval
32
     */
33
    protected $expiration;
34
35
    /**
36
     * @var DateTimeIntervalFactoryInterface
37
     */
38
    protected $intervals;
39
40
    /**
41
     * @var DateTimeFactoryInterface
42
     */
43
    protected $dates;
44
45
    /**
46
     * ExpirationAwareResolver constructor.
47
     */
48
    public function __construct()
49
    {
50
        $this->dates = new DateTimeFactory();
51
        $this->intervals = new DateTimeIntervalFactory($this->dates);
52
        $this->expiration = $this->intervals->create(static::DEFAULT_EXPIRATION_INTERVAL);
53
    }
54
55
    /**
56
     * @return \DateInterval
57
     */
58
    public function getExpirationDate(): \DateInterval
59
    {
60
        return $this->expiration;
61
    }
62
63
    /**
64
     * @param DateIntervalFormat $duration
0 ignored issues
show
Bug introduced by
The type Spiral\Distribution\Resolver\DateIntervalFormat was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
65
     * @return $this
66
     */
67
    public function withExpirationDate($duration): ExpirationAwareResolverInterface
68
    {
69
        $self = clone $this;
70
        $self->expiration = $self->intervals->create($duration);
71
72
        return $self;
73
    }
74
75
    /**
76
     * @param DateTimeFactoryInterface $factory
77
     * @return $this
78
     */
79
    public function withDateTimeFactory(DateTimeFactoryInterface $factory): self
80
    {
81
        $self = clone $this;
82
        $self->dates = $factory;
83
84
        return $self;
85
    }
86
87
    /**
88
     * @param DateTimeIntervalFactoryInterface $factory
89
     * @return $this
90
     */
91
    public function withDateTimeIntervalFactory(DateTimeIntervalFactoryInterface $factory): self
92
    {
93
        $self = clone $this;
94
        $self->intervals = $factory;
95
96
        return $self;
97
    }
98
99
    /**
100
     * @param DateIntervalFormat|null $expiration
101
     * @return \DateTimeInterface
102
     */
103
    protected function getExpirationDateTime($expiration): \DateTimeInterface
104
    {
105
        $expiration = $this->resolveExpirationInterval($expiration);
106
107
        return $this->intervals->toDateTime($expiration);
108
    }
109
110
    /**
111
     * @param DateIntervalFormat|null $expiration
112
     * @return \DateInterval
113
     */
114
    private function resolveExpirationInterval($expiration): \DateInterval
115
    {
116
        if ($expiration === null) {
117
            return $this->expiration;
118
        }
119
120
        return $this->intervals->create($expiration);
121
    }
122
}
123