Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryRead
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
30
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\CommunityConfiguration\Api;
4
5use ApiQuery;
6use ApiQueryBase;
7use InvalidArgumentException;
8use MediaWiki\Extension\CommunityConfiguration\Provider\ConfigurationProviderFactory;
9use MediaWiki\Logger\LoggerFactory;
10use Psr\Log\LoggerAwareTrait;
11use Wikimedia\ParamValidator\ParamValidator;
12
13class ApiQueryRead extends ApiQueryBase {
14    use LoggerAwareTrait;
15
16    private ConfigurationProviderFactory $configurationProviderFactory;
17
18    /**
19     * @param ApiQuery $queryModule
20     * @param string $moduleName
21     * @param ConfigurationProviderFactory $configurationProviderFactory
22     */
23    public function __construct(
24        ApiQuery $queryModule,
25        $moduleName,
26        ConfigurationProviderFactory $configurationProviderFactory
27    ) {
28        parent::__construct( $queryModule, $moduleName, 'ccr' );
29        $this->setLogger( LoggerFactory::getInstance( 'CommunityConfiguration' ) );
30
31        $this->configurationProviderFactory = $configurationProviderFactory;
32    }
33
34    /**
35     * @inheritDoc
36     */
37    public function execute() {
38        $params = $this->extractRequestParams();
39        try {
40            $provider = $this->configurationProviderFactory->newProvider( $params['provider'] );
41        } catch ( InvalidArgumentException $e ) {
42            // NOTE: Assuming the list of supported keys in getAllowedParams() are correct, this
43            // branch should be never triggered.
44
45            $this->logger->error(
46                __METHOD__ . ' failed to construct the {provider} provider',
47                [ 'provider' => $params['provider'], 'exception' => $e ],
48            );
49            $this->dieWithException( $e );
50        }
51
52        $loadedConfig = $provider->loadValidConfiguration();
53        if ( !$loadedConfig->isOK() ) {
54            $this->dieStatus( $loadedConfig );
55        }
56
57        $version = $provider->getStore()->getVersion();
58        // intentionally outside the $version if, because if the client wants a specific
59        // version only, and no version data is available, then it's reasonable to treat that as
60        // a version mismatch
61        if ( $version !== $params['assertversion'] ) {
62            $this->dieWithError( [
63                'apierror-communityconfiguration-version-assertion-failure',
64                    $version,
65                    $params['assertversion']
66                ]
67            );
68        }
69
70        $result = [
71            'data' => $loadedConfig->getValue(),
72        ];
73
74        if ( $version ) {
75            $result['version'] = $version;
76        }
77
78        $this->getResult()->addValue( null, $this->getModuleName(), $result );
79    }
80
81    /**
82     * @inheritDoc
83     */
84    protected function getAllowedParams() {
85        return [
86            'provider' => [
87                ParamValidator::PARAM_TYPE =>
88                    $this->configurationProviderFactory->getSupportedKeys(),
89                ParamValidator::PARAM_REQUIRED => true,
90            ],
91            'assertversion' => [
92                ParamValidator::PARAM_TYPE => 'string',
93            ],
94        ];
95    }
96}