Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
AlternativeIndex
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 7
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getConfig
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getIndex
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 isUse
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isInstanceIndex
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace CirrusSearch\Search;
4
5use CirrusSearch\AlternativeIndices;
6use CirrusSearch\Connection;
7use CirrusSearch\HashSearchConfig;
8use CirrusSearch\SearchConfig;
9use Elastica\Index;
10
11class AlternativeIndex {
12    private int $id;
13    private string $type;
14    private bool $use;
15    private SearchConfig $hostConfig;
16    private array $overrides;
17    private ?SearchConfig $config = null;
18
19    public function __construct( int $id, string $type, bool $use, SearchConfig $config, array $overrides ) {
20        $this->id = $id;
21        $this->type = $type;
22        $this->use = $use;
23        $this->hostConfig = $config;
24        $this->overrides = $overrides;
25    }
26
27    public function getId(): int {
28        return $this->id;
29    }
30
31    public function getType(): string {
32        return $this->type;
33    }
34
35    public function getConfig(): SearchConfig {
36        if ( $this->config === null ) {
37            $this->config = new HashSearchConfig( $this->overrides, [ HashSearchConfig::FLAG_INHERIT ], $this->hostConfig );
38        }
39        return $this->config;
40    }
41
42    public function getIndex( Connection $connection ): Index {
43        switch ( $this->type ) {
44            case AlternativeIndices::COMPLETION:
45                $type = Connection::TITLE_SUGGEST_INDEX_SUFFIX;
46                break;
47            default:
48                throw new \LogicException( "Unknown alternative index type {$this->type}" );
49        }
50        return $connection->getIndex( $this->getConfig()->get( 'CirrusSearchIndexBaseName' ), $type, false, true, $this->id );
51    }
52
53    /**
54     * Can this index be used at query time.
55     * @return bool
56     */
57    public function isUse(): bool {
58        return $this->use;
59    }
60
61    public function isInstanceIndex( string $indexName, Connection $connection ): bool {
62        return str_starts_with( $indexName, $this->getIndex( $connection )->getName() . '_' );
63    }
64}