Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
65.00% covered (warning)
65.00%
13 / 20
66.67% covered (warning)
66.67%
8 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
FallbackRunnerContextImpl
65.00% covered (warning)
65.00%
13 / 20
66.67% covered (warning)
66.67%
8 / 12
18.17
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 setPreviousResultSet
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 resetSuggestResponse
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSuggestResponse
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasMethodResponse
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getInitialResultSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPreviousResultSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMethodResponse
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 costlyCallAllowed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 makeSearcher
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getNamespacePrefixParser
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCirrusSearchHookRunner
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace CirrusSearch\Fallbacks;
4
5use CirrusSearch\CirrusSearchHookRunner;
6use CirrusSearch\Parser\NamespacePrefixParser;
7use CirrusSearch\Search\CirrusSearchResultSet;
8use CirrusSearch\Search\SearchQuery;
9use CirrusSearch\Searcher;
10use Elastica\ResultSet as ElasticaResultSet;
11use Wikimedia\Assert\Assert;
12
13/**
14 * Basic implementation of a FallbackRunnerContext.
15 * Should only be visible by FallbackRunner as its states should be closely
16 * maintained by the FallbackRunner.
17 */
18class FallbackRunnerContextImpl implements FallbackRunnerContext {
19    /**
20     * Initial ResultSet as returned by the main search query
21     * @var CirrusSearchResultSet (final)
22     */
23    private $initialResultSet;
24
25    /**
26     * The resultset as returned by the last call to FallbackMethod::rewrite()
27     * @var CirrusSearchResultSet (mutable)
28     */
29    private $previousResultSet;
30
31    /**
32     * @var ElasticaResultSet|null Response to elasticsearch request
33     *  issued by either ElasticSearchRequestFallbackMethod or
34     *  ElasticSearchSuggestFallbackMethod.
35     */
36    private $suggestResponse;
37
38    /**
39     * @var SearcherFactory
40     */
41    private $searcherFactory;
42
43    /**
44     * @var bool
45     */
46    private $canMakeCostlyCall = true;
47
48    /**
49     * @var NamespacePrefixParser
50     */
51    private $namespacePrefixParser;
52    /**
53     * @var CirrusSearchHookRunner
54     */
55    private $cirrusSearchHookRunner;
56
57    /**
58     * @param CirrusSearchResultSet $initialResultSet
59     * @param SearcherFactory $searcherFactory
60     * @param NamespacePrefixParser $namespacePrefixParser
61     * @param CirrusSearchHookRunner $cirrusSearchHookRunner
62     */
63    public function __construct(
64        CirrusSearchResultSet $initialResultSet,
65        SearcherFactory $searcherFactory,
66        NamespacePrefixParser $namespacePrefixParser,
67        CirrusSearchHookRunner $cirrusSearchHookRunner
68    ) {
69        $this->initialResultSet = $initialResultSet;
70        $this->previousResultSet = $initialResultSet;
71        $this->searcherFactory = $searcherFactory;
72        $this->namespacePrefixParser = $namespacePrefixParser;
73        $this->cirrusSearchHookRunner = $cirrusSearchHookRunner;
74    }
75
76    /**
77     * Initialize the previous resultset
78     * (only visible by FallbackRunner)
79     * @param CirrusSearchResultSet $previousResultSet
80     */
81    public function setPreviousResultSet( CirrusSearchResultSet $previousResultSet ) {
82        $this->previousResultSet = $previousResultSet;
83    }
84
85    public function resetSuggestResponse() {
86        $this->suggestResponse = null;
87    }
88
89    /**
90     * @param ElasticaResultSet $suggestResponse
91     */
92    public function setSuggestResponse( ElasticaResultSet $suggestResponse ) {
93        $this->suggestResponse = $suggestResponse;
94    }
95
96    /**
97     * @inheritDoc
98     */
99    public function hasMethodResponse() {
100        return $this->suggestResponse !== null;
101    }
102
103    /**
104     * @return CirrusSearchResultSet
105     */
106    public function getInitialResultSet(): CirrusSearchResultSet {
107        return $this->initialResultSet;
108    }
109
110    /**
111     * @return CirrusSearchResultSet
112     */
113    public function getPreviousResultSet(): CirrusSearchResultSet {
114        return $this->previousResultSet;
115    }
116
117    /**
118     * @return ElasticaResultSet
119     */
120    public function getMethodResponse(): ElasticaResultSet {
121        Assert::precondition( $this->suggestResponse !== null, 'Must have a resultset set' );
122        return $this->suggestResponse;
123    }
124
125    /**
126     * @return bool
127     */
128    public function costlyCallAllowed() {
129        return $this->canMakeCostlyCall;
130    }
131
132    /**
133     * @param \CirrusSearch\Search\SearchQuery $rewrittenQuery
134     * @return Searcher
135     */
136    public function makeSearcher( SearchQuery $rewrittenQuery ): Searcher {
137        Assert::precondition( $this->canMakeCostlyCall,
138            'Costly calls are no longer accepted, check costlyCallAllowed before calling makeSearcher' );
139        // For now we just allow a single call, we might prefer a time constrained approach
140        // So that multiple calls can be made if we still have some processing time left.
141        $this->canMakeCostlyCall = false;
142        return $this->searcherFactory->makeSearcher( $rewrittenQuery );
143    }
144
145    /**
146     * @return NamespacePrefixParser
147     */
148    public function getNamespacePrefixParser(): NamespacePrefixParser {
149        return $this->namespacePrefixParser;
150    }
151
152    /**
153     * @return CirrusSearchHookRunner
154     */
155    public function getCirrusSearchHookRunner(): CirrusSearchHookRunner {
156        return $this->cirrusSearchHookRunner;
157    }
158}