Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
LimitBatchResult
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
5 / 5
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isAllowed
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 getFailedResults
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getPassedResults
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getAllResults
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikimedia\WRStats;
4
5/**
6 * A class representing the results from a batch operation.
7 *
8 * @since 1.39
9 */
10class LimitBatchResult {
11    /** @var LimitOperationResult[] */
12    private $results;
13
14    /** @var bool|null */
15    private $allowed;
16
17    /**
18     * @internal Use WRStatsFactory::createRateLimiter() instead
19     * @param LimitOperationResult[] $results
20     */
21    public function __construct( $results ) {
22        $this->results = $results;
23    }
24
25    /**
26     * Determine whether the batch as a whole is/was allowed
27     *
28     * @return bool
29     */
30    public function isAllowed() {
31        if ( $this->allowed === null ) {
32            $this->allowed = true;
33            foreach ( $this->results as $result ) {
34                if ( !$result->isAllowed() ) {
35                    $this->allowed = false;
36                    break;
37                }
38            }
39        }
40        return $this->allowed;
41    }
42
43    /**
44     * Get LimitOperationResult objects for operations exceeding the limit.
45     *
46     * The keys will match the input array. For input arrays constructed by
47     * LimitBatch, the keys will be the condition names.
48     *
49     * @return LimitOperationResult[]
50     */
51    public function getFailedResults() {
52        $failed = [];
53        foreach ( $this->results as $i => $result ) {
54            if ( !$result->isAllowed() ) {
55                $failed[$i] = $result;
56            }
57        }
58        return $failed;
59    }
60
61    /**
62     * Get LimitOperationResult objects for operations not exceeding the limit.
63     *
64     * The keys will match the input array. For input arrays constructed by
65     * LimitBatch, the keys will be the condition names.
66     *
67     * @return LimitOperationResult[]
68     */
69    public function getPassedResults() {
70        $passed = [];
71        foreach ( $this->results as $i => $result ) {
72            if ( $result->isAllowed() ) {
73                $passed[$i] = $result;
74            }
75        }
76        return $passed;
77    }
78
79    /**
80     * Get LimitOperationResult objects for all operations in the batch.
81     *
82     * The keys will match the input array. For input arrays constructed by
83     * LimitBatch, the keys will be the condition names.
84     *
85     * @return LimitOperationResult[]
86     */
87    public function getAllResults() {
88        return $this->results;
89    }
90}