Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
19 / 19 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
CachingMetadata | |
100.00% |
19 / 19 |
|
100.00% |
6 / 6 |
9 | |
100.00% |
1 / 1 |
fresh | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
ofMaximumAgeInSeconds | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
merge | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
isCached | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMaximumAgeInSeconds | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
toArray | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Cache; |
6 | |
7 | use Wikimedia\Assert\Assert; |
8 | |
9 | /** |
10 | * Information about whether and how a value was cached. |
11 | * |
12 | * @author Lucas Werkmeister |
13 | * @license GPL-2.0-or-later |
14 | */ |
15 | class CachingMetadata { |
16 | |
17 | /** |
18 | * @var int|bool The maximum age in seconds, |
19 | * or false to indicate that the value wasn’t cached. |
20 | */ |
21 | private $maxAge = false; |
22 | |
23 | /** |
24 | * @return self Indication that a value is fresh, i. e. not cached. |
25 | */ |
26 | public static function fresh(): self { |
27 | return new self; |
28 | } |
29 | |
30 | /** |
31 | * @param int $maxAge The maximum age of the cached value (in seconds). |
32 | * @return self Indication that a value is possibly outdated by up to this many seconds. |
33 | */ |
34 | public static function ofMaximumAgeInSeconds( int $maxAge ): self { |
35 | Assert::parameter( $maxAge > 0, '$maxAge', '$maxage > 0' ); |
36 | $ret = new self; |
37 | $ret->maxAge = $maxAge; |
38 | return $ret; |
39 | } |
40 | |
41 | /** |
42 | * @param self[] $metadatas |
43 | * @return self |
44 | */ |
45 | public static function merge( array $metadatas ): self { |
46 | Assert::parameterElementType( self::class, $metadatas, '$metadatas' ); |
47 | $ret = new self; |
48 | foreach ( $metadatas as $metadata ) { |
49 | $ret->maxAge = max( $ret->maxAge, $metadata->maxAge ); |
50 | } |
51 | return $ret; |
52 | } |
53 | |
54 | /** |
55 | * @return bool Whether the value is cached or not (fresh). |
56 | */ |
57 | public function isCached(): bool { |
58 | return $this->maxAge !== false; |
59 | } |
60 | |
61 | /** |
62 | * @return int The maximum age of the cached value (in seconds), in other words: |
63 | * the value might be outdated by up to this many seconds. |
64 | * For a fresh value, returns 0. |
65 | */ |
66 | public function getMaximumAgeInSeconds(): int { |
67 | if ( is_int( $this->maxAge ) ) { |
68 | return $this->maxAge; |
69 | } else { |
70 | return 0; |
71 | } |
72 | } |
73 | |
74 | /** |
75 | * Serializes the metadata into an array (or null if the value is fresh). |
76 | * @return array|null |
77 | */ |
78 | public function toArray(): ?array { |
79 | return $this->isCached() ? |
80 | [ |
81 | 'maximumAgeInSeconds' => $this->maxAge, |
82 | ] : |
83 | null; |
84 | } |
85 | |
86 | } |