Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.74% covered (success)
94.74%
18 / 19
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
PersistentCacheEntry
94.74% covered (success)
94.74%
18 / 19
83.33% covered (warning)
83.33%
5 / 6
10.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 key
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 value
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 exptime
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 tag
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasExpired
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Cache;
5
6use DateTime;
7use InvalidArgumentException;
8
9/**
10 * Represents a single result from the persistent cache
11 * @author Abijeet Patro
12 * @license GPL-2.0-or-later
13 * @since 2020.12
14 */
15class PersistentCacheEntry {
16    private const MAX_KEY_LENGTH = 255;
17    private const MAX_TAG_LENGTH = 255;
18
19    private string $key;
20    /** @var mixed */
21    private $value;
22    private ?int $exptime;
23    private ?string $tag;
24
25    public function __construct(
26        string $key,
27        $value = null,
28        ?int $exptime = null,
29        ?string $tag = null
30    ) {
31        if ( strlen( $key ) > self::MAX_KEY_LENGTH ) {
32            throw new InvalidArgumentException(
33                "The length of key: $key is greater than allowed " . self::MAX_KEY_LENGTH
34            );
35        }
36
37        if ( $tag && strlen( $tag ) > self::MAX_TAG_LENGTH ) {
38            throw new InvalidArgumentException(
39                "The length of tag: $tag is greater than allowed " . self::MAX_TAG_LENGTH
40            );
41        }
42
43        $this->key = $key;
44        $this->value = $value;
45        $this->exptime = $exptime;
46        $this->tag = $tag;
47    }
48
49    public function key(): string {
50        return $this->key;
51    }
52
53    /** @return mixed */
54    public function value() {
55        return $this->value;
56    }
57
58    public function exptime(): ?int {
59        return $this->exptime;
60    }
61
62    public function tag(): ?string {
63        return $this->tag;
64    }
65
66    public function hasExpired(): bool {
67        if ( $this->exptime ) {
68            return $this->exptime < ( new DateTime() )->getTimestamp();
69        }
70
71        return false;
72    }
73}