aboutsummaryrefslogtreecommitdiffstats
path: root/includes/libs/WRStats/LimitBatchResult.php
blob: cb01f017192a5b91d5533d05ae211ca3372a5235 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php

namespace Wikimedia\WRStats;

/**
 * A class representing the results from a batch operation.
 *
 * @since 1.39
 */
class LimitBatchResult {
	/** @var LimitOperationResult[] */
	private $results;

	/** @var bool|null */
	private $allowed;

	/**
	 * @internal Use WRStatsFactory::createRateLimiter() instead
	 * @param LimitOperationResult[] $results
	 */
	public function __construct( $results ) {
		$this->results = $results;
	}

	/**
	 * Determine whether the batch as a whole is/was allowed
	 *
	 * @return bool
	 */
	public function isAllowed() {
		if ( $this->allowed === null ) {
			$this->allowed = true;
			foreach ( $this->results as $result ) {
				if ( !$result->isAllowed() ) {
					$this->allowed = false;
					break;
				}
			}
		}
		return $this->allowed;
	}

	/**
	 * Get LimitOperationResult objects for operations exceeding the limit.
	 *
	 * The keys will match the input array. For input arrays constructed by
	 * LimitBatch, the keys will be the condition names.
	 *
	 * @return LimitOperationResult[]
	 */
	public function getFailedResults() {
		$failed = [];
		foreach ( $this->results as $i => $result ) {
			if ( !$result->isAllowed() ) {
				$failed[$i] = $result;
			}
		}
		return $failed;
	}

	/**
	 * Get LimitOperationResult objects for operations not exceeding the limit.
	 *
	 * The keys will match the input array. For input arrays constructed by
	 * LimitBatch, the keys will be the condition names.
	 *
	 * @return LimitOperationResult[]
	 */
	public function getPassedResults() {
		$passed = [];
		foreach ( $this->results as $i => $result ) {
			if ( $result->isAllowed() ) {
				$passed[$i] = $result;
			}
		}
		return $passed;
	}

	/**
	 * Get LimitOperationResult objects for all operations in the batch.
	 *
	 * The keys will match the input array. For input arrays constructed by
	 * LimitBatch, the keys will be the condition names.
	 *
	 * @return LimitOperationResult[]
	 */
	public function getAllResults() {
		return $this->results;
	}
}