Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
HashSearchConfig
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 getWikiId
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getHostWikiConfig
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 isLocalWiki
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace CirrusSearch;
4
5use CirrusSearch\Profile\SearchProfileServiceFactoryFactory;
6use GlobalVarConfig;
7use InvalidArgumentException;
8use MultiConfig;
9
10/**
11 * SearchConfig implemenation backed by a simple \HashConfig
12 */
13class HashSearchConfig extends SearchConfig {
14    public const FLAG_INHERIT = 'inherit';
15
16    /** @var bool */
17    private $localWiki = false;
18
19    /**
20     * @param array $settings config vars
21     * @param string[] $flags customization flags:
22     * - inherit: config vars not part the settings provided are fetched from GlobalVarConfig
23     * @param \Config|null $inherited (only useful when the inherit flag is set)
24     * @param SearchProfileServiceFactoryFactory|null $searchProfileServiceFactoryFactory
25     * @throws \MWException
26     */
27    public function __construct(
28        array $settings,
29        array $flags = [],
30        \Config $inherited = null,
31        SearchProfileServiceFactoryFactory $searchProfileServiceFactoryFactory = null
32    ) {
33        parent::__construct( $searchProfileServiceFactoryFactory );
34        $config = new \HashConfig( $settings );
35        $extra = array_diff( $flags, [ self::FLAG_INHERIT ] );
36        if ( $extra ) {
37            throw new InvalidArgumentException( "Unknown config flags: " . implode( ',', $extra ) );
38        }
39
40        if ( in_array( self::FLAG_INHERIT, $flags ) ) {
41            $config = new MultiConfig( [ $config, $inherited ?? new GlobalVarConfig ] );
42            $this->localWiki = !isset( $settings['_wikiID' ] );
43        }
44        $this->setSource( $config );
45    }
46
47    /**
48     * Allow overriding Wiki ID
49     * @return mixed|string
50     */
51    public function getWikiId() {
52        if ( $this->has( '_wikiID' ) ) {
53            return $this->get( '_wikiID' );
54        }
55        return parent::getWikiId();
56    }
57
58    public function getHostWikiConfig(): SearchConfig {
59        if ( $this->localWiki ) {
60            return $this;
61        }
62        return parent::getHostWikiConfig();
63    }
64
65    public function isLocalWiki() {
66        return $this->localWiki;
67    }
68}