Completed
Push — master ( afdb92...b7b36b )
by
unknown
02:45
created

ExpiryLock::lock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 2
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 ( empty( 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(),
70
				$expiryTimestamp->getTimestamp()
71
			);
72
		} else {
73
			return false;
74
		}
75
	}
76
77
	/**
78
	 * @param string the converted $cacheId
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 ) {
0 ignored issues
show
Bug introduced by
The class Wikimedia\Timestamp\TimestampException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
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
}
116