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