Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.43% |
27 / 28 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
ExpiryLock | |
96.43% |
27 / 28 |
|
80.00% |
4 / 5 |
10 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
makeKey | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
2.01 | |||
lock | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
isLockedInternal | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
isLocked | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace WikibaseQuality\ConstraintReport\Api; |
4 | |
5 | use BagOStuff; |
6 | use Wikimedia\Assert\Assert; |
7 | use Wikimedia\Assert\ParameterTypeException; |
8 | use Wikimedia\Timestamp\ConvertibleTimestamp; |
9 | use Wikimedia\Timestamp\TimestampException; |
10 | |
11 | /** |
12 | * A thin wrapper around a BagOStuff |
13 | * that caches a timestamp and create a lock |
14 | * until a certain time is over. |
15 | * |
16 | * @author Matthias Geisler |
17 | * @license GPL-2.0-or-later |
18 | */ |
19 | class ExpiryLock { |
20 | |
21 | /** |
22 | * @var BagOStuff |
23 | */ |
24 | private $cache; |
25 | |
26 | /** |
27 | * @param BagOStuff $cache |
28 | */ |
29 | public function __construct( BagOStuff $cache ) { |
30 | $this->cache = $cache; |
31 | } |
32 | |
33 | /** |
34 | * @param string $id of the lock |
35 | * |
36 | * @return string cache key |
37 | * |
38 | * @throws \Wikimedia\Assert\ParameterTypeException |
39 | */ |
40 | private function makeKey( $id ) { |
41 | if ( trim( $id ) === '' ) { |
42 | throw new ParameterTypeException( '$id', 'non-empty string' ); |
43 | } |
44 | |
45 | Assert::parameterType( 'string', $id, '$id' ); |
46 | |
47 | return $this->cache->makeKey( |
48 | 'WikibaseQualityConstraints', |
49 | 'ExpiryLock', |
50 | (string)$id |
51 | ); |
52 | } |
53 | |
54 | /** |
55 | * @param string $id of the lock |
56 | * @param ConvertibleTimestamp $expiryTimestamp |
57 | * |
58 | * @return boolean success |
59 | * |
60 | * @throws \Wikimedia\Assert\ParameterTypeException |
61 | */ |
62 | public function lock( $id, ConvertibleTimestamp $expiryTimestamp ) { |
63 | |
64 | $cacheId = $this->makeKey( $id ); |
65 | |
66 | if ( !$this->isLockedInternal( $cacheId ) ) { |
67 | return $this->cache->set( |
68 | $cacheId, |
69 | $expiryTimestamp->getTimestamp( TS_UNIX ), |
70 | (int)$expiryTimestamp->getTimestamp( TS_UNIX ) |
71 | ); |
72 | } else { |
73 | return false; |
74 | } |
75 | } |
76 | |
77 | /** |
78 | * @param string $cacheId the converted cache id |
79 | * |
80 | * @return boolean representing if the Lock is Locked |
81 | * |
82 | * @throws \Wikimedia\Assert\ParameterTypeException |
83 | */ |
84 | private function isLockedInternal( $cacheId ) { |
85 | $expiryTime = $this->cache->get( $cacheId ); |
86 | if ( !$expiryTime ) { |
87 | return false; |
88 | } |
89 | |
90 | try { |
91 | $lockExpiryTimeStamp = new ConvertibleTimestamp( $expiryTime ); |
92 | } catch ( TimestampException $exception ) { |
93 | return false; |
94 | } |
95 | |
96 | $now = new ConvertibleTimestamp(); |
97 | if ( $now->timestamp < $lockExpiryTimeStamp->timestamp ) { |
98 | return true; |
99 | } else { |
100 | return false; |
101 | } |
102 | } |
103 | |
104 | /** |
105 | * @param string $id of the lock |
106 | * |
107 | * @return boolean representing if the Lock is Locked |
108 | * |
109 | * @throws \Wikimedia\Assert\ParameterTypeException |
110 | */ |
111 | public function isLocked( $id ) { |
112 | return $this->isLockedInternal( $this->makeKey( $id ) ); |
113 | } |
114 | |
115 | } |