Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.77% covered (success)
96.77%
30 / 31
90.91% covered (success)
90.91%
10 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
MSearchResponses
96.77% covered (success)
96.77%
30 / 31
90.91% covered (success)
90.91%
10 / 11
17
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getResultSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 transformAndGetMulti
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 transformAndGetSingle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 transformAsResultSet
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasResponses
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasFailure
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFailure
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 hasTimeout
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 hasResultsFor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 dumpResults
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace CirrusSearch\Search;
4
5use Elastica\ResultSet as ElasticaResultSet;
6use Elastica\Search;
7use MediaWiki\Status\Status;
8use Wikimedia\Assert\Assert;
9
10/**
11 * Holds the Elastica result sets returned by Elastic when issuing search requests.
12 * Used to keep track of which search request led to which result set.
13 */
14class MSearchResponses {
15    /**
16     * @var ElasticaResultSet[]
17     */
18    private $resultSets;
19
20    /**
21     * @var Search[]
22     */
23    private $requests;
24
25    /**
26     * @var Status|null
27     */
28    private $failure;
29
30    /**
31     * @param ElasticaResultSet[] $resultSets
32     * @param Search[] $requests
33     * @param Status|null $status failure
34     */
35    public function __construct( array $resultSets, array $requests, Status $status = null ) {
36        $this->resultSets = $resultSets;
37        $this->requests = $requests;
38        Assert::parameter( $status === null || !$status->isOK(), '$status', 'must be a failure if set' );
39        $this->failure = $status;
40    }
41
42    /**
43     * @param string $key
44     * @return ElasticaResultSet
45     */
46    public function getResultSet( $key ): ElasticaResultSet {
47        return $this->resultSets[$key];
48    }
49
50    /**
51     * Transform all the resultsets identified by keys and wrap them inside a success Status stored as an array.
52     *
53     * @param ResultsType $transformation
54     * @param string[] $keys
55     * @param callable|null $reordering reordering function
56     * @return Status
57     */
58    public function transformAndGetMulti( ResultsType $transformation, array $keys, callable $reordering = null ): Status {
59        $result = [];
60        $input = array_intersect_key( $this->resultSets, array_flip( $keys ) );
61        foreach ( $input as $k => $v ) {
62            $result[$k] = $transformation->transformElasticsearchResult( $v );
63        }
64        if ( $reordering !== null ) {
65            $result = $reordering( $result );
66        }
67        return Status::newGood( $result );
68    }
69
70    /**
71     * Transform the resultset identified by key and wrap it inside a success Status
72     * @param ResultsType $transformation
73     * @param string $key
74     * @return Status
75     */
76    public function transformAndGetSingle( ResultsType $transformation, $key ): Status {
77        return Status::newGood( $transformation->transformElasticsearchResult( $this->resultSets[$key] ) );
78    }
79
80    /**
81     * Transform the resultset identified by key and returns it as a CirrusSearch ResultSet
82     * NOTE: The $tranformation provided must emit a CirrusResultSet
83     * @param ResultsType $transformation
84     * @param string $key
85     * @return CirrusSearchResultSet
86     */
87    public function transformAsResultSet( ResultsType $transformation, $key ): CirrusSearchResultSet {
88        return $transformation->transformElasticsearchResult( $this->resultSets[$key] );
89    }
90
91    /**
92     * @return bool
93     */
94    public function hasResponses() {
95        return $this->resultSets !== [];
96    }
97
98    /**
99     * @return bool
100     */
101    public function hasFailure() {
102        return $this->failure !== null;
103    }
104
105    /**
106     * @return Status
107     */
108    public function getFailure(): Status {
109        Assert::precondition( $this->failure !== null, 'must have failed' );
110        return $this->failure;
111    }
112
113    /**
114     * @return bool
115     */
116    public function hasTimeout() {
117        foreach ( $this->resultSets as $resultSet ) {
118            if ( $resultSet->hasTimedOut() ) {
119                return true;
120            }
121        }
122        return false;
123    }
124
125    /**
126     * @param string $key
127     * @return bool
128     */
129    public function hasResultsFor( $key ) {
130        return array_key_exists( $key, $this->resultSets );
131    }
132
133    /**
134     * @param string $description
135     * @return Status
136     */
137    public function dumpResults( $description ): Status {
138        $retval = [];
139        foreach ( $this->resultSets as $key => $resultSet ) {
140            $retval[$key] = [
141                'description' => $description,
142                'path' => $this->requests[$key]->getPath(),
143                'result' => $resultSet->getResponse()->getData(),
144            ];
145        }
146        return Status::newGood( $retval );
147    }
148}