Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
65.00% |
13 / 20 |
|
66.67% |
8 / 12 |
CRAP | |
0.00% |
0 / 1 |
| FallbackRunnerContextImpl | |
65.00% |
13 / 20 |
|
66.67% |
8 / 12 |
18.17 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| setPreviousResultSet | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| resetSuggestResponse | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setSuggestResponse | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasMethodResponse | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getInitialResultSet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPreviousResultSet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMethodResponse | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| costlyCallAllowed | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| makeSearcher | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getNamespacePrefixParser | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCirrusSearchHookRunner | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace CirrusSearch\Fallbacks; |
| 4 | |
| 5 | use CirrusSearch\CirrusSearchHookRunner; |
| 6 | use CirrusSearch\Parser\NamespacePrefixParser; |
| 7 | use CirrusSearch\Search\CirrusSearchResultSet; |
| 8 | use CirrusSearch\Search\SearchQuery; |
| 9 | use CirrusSearch\Searcher; |
| 10 | use Elastica\ResultSet as ElasticaResultSet; |
| 11 | use 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 | */ |
| 18 | class 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 | */ |
| 80 | public function setPreviousResultSet( CirrusSearchResultSet $previousResultSet ) { |
| 81 | $this->previousResultSet = $previousResultSet; |
| 82 | } |
| 83 | |
| 84 | public function resetSuggestResponse() { |
| 85 | $this->suggestResponse = null; |
| 86 | } |
| 87 | |
| 88 | public function setSuggestResponse( ElasticaResultSet $suggestResponse ) { |
| 89 | $this->suggestResponse = $suggestResponse; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @inheritDoc |
| 94 | */ |
| 95 | public function hasMethodResponse() { |
| 96 | return $this->suggestResponse !== null; |
| 97 | } |
| 98 | |
| 99 | public function getInitialResultSet(): CirrusSearchResultSet { |
| 100 | return $this->initialResultSet; |
| 101 | } |
| 102 | |
| 103 | public function getPreviousResultSet(): CirrusSearchResultSet { |
| 104 | return $this->previousResultSet; |
| 105 | } |
| 106 | |
| 107 | public function getMethodResponse(): ElasticaResultSet { |
| 108 | Assert::precondition( $this->suggestResponse !== null, 'Must have a resultset set' ); |
| 109 | return $this->suggestResponse; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @return bool |
| 114 | */ |
| 115 | public function costlyCallAllowed() { |
| 116 | return $this->canMakeCostlyCall; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @param \CirrusSearch\Search\SearchQuery $rewrittenQuery |
| 121 | * @return Searcher |
| 122 | */ |
| 123 | public function makeSearcher( SearchQuery $rewrittenQuery ): Searcher { |
| 124 | Assert::precondition( $this->canMakeCostlyCall, |
| 125 | 'Costly calls are no longer accepted, check costlyCallAllowed before calling makeSearcher' ); |
| 126 | // For now we just allow a single call, we might prefer a time constrained approach |
| 127 | // So that multiple calls can be made if we still have some processing time left. |
| 128 | $this->canMakeCostlyCall = false; |
| 129 | return $this->searcherFactory->makeSearcher( $rewrittenQuery ); |
| 130 | } |
| 131 | |
| 132 | public function getNamespacePrefixParser(): NamespacePrefixParser { |
| 133 | return $this->namespacePrefixParser; |
| 134 | } |
| 135 | |
| 136 | public function getCirrusSearchHookRunner(): CirrusSearchHookRunner { |
| 137 | return $this->cirrusSearchHookRunner; |
| 138 | } |
| 139 | } |