Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractProvider
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
9 / 9
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getStore
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getValidator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 doStoreValidConfiguration
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
4
 storeValidConfiguration
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 alwaysStoreValidConfiguration
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOptionValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\CommunityConfiguration\Provider;
4
5use MediaWiki\Extension\CommunityConfiguration\Store\IConfigurationStore;
6use MediaWiki\Extension\CommunityConfiguration\Validation\IValidator;
7use MediaWiki\Message\Message;
8use MediaWiki\Permissions\Authority;
9use MessageLocalizer;
10use Psr\Log\LoggerAwareTrait;
11use Psr\Log\NullLogger;
12use StatusValue;
13
14abstract class AbstractProvider implements IConfigurationProvider {
15    use LoggerAwareTrait;
16
17    private string $providerId;
18    private IConfigurationStore $store;
19    private IValidator $validator;
20    private array $options;
21
22    /**
23     * Constructs a new instance of a provider.
24     *
25     * @param string $providerId The unique identifier for the provider.
26     * @param array $options
27     *         Configuration options for the provider, may be structured as follows:
28     *         - 'skipDashboardListing' (bool, optional): Indicates whether this provider
29     *             should be skipped on the dashboard.
30     *         - 'helpPage' (string, optional): Title of the help page of the feature on mediawiki.org.
31     *             e.g., Help:Growth/Mentorship'
32     *         - 'helpURL' (string, optional): Full URL to a help resource.
33     *             'helpURL' should be used if the help content is on a specific section of a page or is an external help
34     *             resource, e.g., 'https://www.mediawiki.org/wiki/Special:MyLanguage/Page#Section'. 'helpURL' should be a
35     *             full URL pointing to that section with an anchor.
36     *         - At most one of helpURL and helpPage should be provided, not both.
37     * @param IConfigurationStore $store The store used by the provider.
38     * @param IValidator $validator The validator used by the provider.
39     */
40    public function __construct(
41        string $providerId,
42        array $options,
43        IConfigurationStore $store,
44        IValidator $validator
45    ) {
46        $this->providerId = $providerId;
47        $this->options = $options;
48        $this->store = $store;
49        $this->validator = $validator;
50        $this->setLogger( new NullLogger() );
51    }
52
53    /**
54     * @inheritDoc
55     */
56    public function getId(): string {
57        return $this->providerId;
58    }
59
60    /**
61     * @param MessageLocalizer $localizer
62     * @inheritDoc
63     */
64    public function getName( MessageLocalizer $localizer ): Message {
65        return $localizer->msg(
66            sprintf( 'communityconfiguration-%s-title', strtolower( $this->getId() ) )
67        );
68    }
69
70    /**
71     * @inheritDoc
72     */
73    public function getStore(): IConfigurationStore {
74        return $this->store;
75    }
76
77    /**
78     * @inheritDoc
79     */
80    public function getValidator(): IValidator {
81        return $this->validator;
82    }
83
84    /**
85     * Store configuration
86     *
87     * Providers should override this if they want to modify how _both_ storeValidConfiguration()
88     * and alwaysStoreValidConfiguration() behave.
89     *
90     * @param mixed $newConfig The configuration value to store. Can be any JSON serializable type
91     * @param Authority $authority
92     * @param string $summary
93     * @param bool $bypassPermissionCheck Whether IConfigurationStore::alwaysStoreConfiguration
94     * should be used.
95     * @return StatusValue
96     */
97    private function doStoreValidConfiguration(
98        $newConfig,
99        Authority $authority,
100        string $summary,
101        bool $bypassPermissionCheck
102    ): StatusValue {
103        $validationStatus = $this->getValidator()->validateStrictly( $newConfig );
104        if ( !$validationStatus->isGood() ) {
105            return $validationStatus;
106        }
107
108        $args = [
109            $newConfig,
110            $this->getValidator()->areSchemasSupported()
111                ? $this->getValidator()->getSchemaVersion()
112                : null,
113            $authority,
114            $summary,
115        ];
116
117        if ( $bypassPermissionCheck ) {
118            return $this->getStore()->alwaysStoreConfiguration( ...$args );
119        } else {
120            return $this->getStore()->storeConfiguration( ...$args );
121        }
122    }
123
124    /**
125     * @inheritDoc
126     */
127    public function storeValidConfiguration(
128        $newConfig,
129        Authority $authority,
130        string $summary = ''
131    ): StatusValue {
132        return $this->doStoreValidConfiguration( $newConfig, $authority, $summary, false );
133    }
134
135    /**
136     * @inheritDoc
137     */
138    public function alwaysStoreValidConfiguration(
139        $newConfig,
140        Authority $authority,
141        string $summary = ''
142    ): StatusValue {
143        return $this->doStoreValidConfiguration( $newConfig, $authority, $summary, true );
144    }
145
146    /**
147     * @inheritDoc
148     */
149    public function getOptionValue( string $optionName ) {
150        return $this->options[ $optionName ] ?? null;
151    }
152}