Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
10 / 20
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MediaWikiConfigProvider
50.00% covered (danger)
50.00%
10 / 20
0.00% covered (danger)
0.00%
0 / 4
22.50
0.00% covered (danger)
0.00%
0 / 1
 getValidConfigOrDefaults
30.00% covered (danger)
30.00%
3 / 10
0.00% covered (danger)
0.00%
0 / 1
6.09
 get
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 getSupportedConfigVariableNames
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 has
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
1<?php
2
3namespace MediaWiki\Extension\CommunityConfiguration\Provider;
4
5use MediaWiki\Config\Config;
6use MediaWiki\Config\ConfigException;
7use stdClass;
8
9class MediaWikiConfigProvider
10    extends DataProvider
11    implements IConfigurationProvider, Config
12{
13
14    /**
15     * @return stdClass
16     */
17    private function getValidConfigOrDefaults(): stdClass {
18        $status = $this->loadValidConfiguration();
19        if ( !$status->isOK() ) {
20            $this->logger->error(
21                'CommunityConfiguration provider ' . $this->getId() . ' failed to load; '
22                . 'stored configuration is not valid.'
23            );
24
25            return $this->getValidator()->areSchemasSupported()
26                ? $this->getValidator()->getSchemaBuilder()->getDefaultsMap()
27                : new stdClass();
28        }
29
30        return $status->getValue();
31    }
32
33    /**
34     * @inheritDoc
35     */
36    public function get( $name ) {
37        if ( !$this->has( $name ) ) {
38            throw new ConfigException( 'Key ' . $name . ' was not found.' );
39        }
40
41        return $this->getValidConfigOrDefaults()->{$name};
42    }
43
44    /**
45     * Get a list of supported config variables
46     *
47     * @return array|null Null if all variables are supported
48     */
49    public function getSupportedConfigVariableNames(): ?array {
50        if ( !$this->getValidator()->areSchemasSupported() ) {
51            return null;
52        }
53
54        return array_keys( $this->getValidator()->getSchemaBuilder()->getRootProperties() );
55    }
56
57    /**
58     * @inheritDoc
59     */
60    public function has( $name ) {
61        $allowlist = $this->getSupportedConfigVariableNames();
62        if ( $allowlist && !in_array( $name, $allowlist ) ) {
63            // This config value is not supported
64            return false;
65        }
66
67        return property_exists( $this->getValidConfigOrDefaults(), $name );
68    }
69}