Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
52.63% covered (warning)
52.63%
20 / 38
37.50% covered (danger)
37.50%
3 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiEchoPushSubscriptions
52.63% covered (warning)
52.63%
20 / 38
37.50% covered (danger)
37.50%
3 / 8
20.63
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 getModuleManager
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
 getAllowedParams
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 checkLoginState
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isInternal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\Notifications\Push\Api;
4
5use ApiBase;
6use ApiModuleManager;
7use ApiUsageException;
8use MediaWiki\MediaWikiServices;
9use Wikimedia\ParamValidator\ParamValidator;
10
11/**
12 * API parent module for administering push subscriptions.
13 * Each operation (command) is implemented as a submodule. This module just performs some basic
14 * checks and dispatches the execute() call.
15 */
16class ApiEchoPushSubscriptions extends ApiBase {
17
18    /** array Module name => module class */
19    private const SUBMODULES = [
20        'create' => ApiEchoPushSubscriptionsCreate::class,
21        'delete' => ApiEchoPushSubscriptionsDelete::class,
22    ];
23
24    /** @var ApiModuleManager */
25    private $moduleManager;
26
27    /** @inheritDoc */
28    public function execute(): void {
29        $this->checkLoginState();
30        $this->checkUserRightsAny( 'editmyprivateinfo' );
31        $command = $this->getParameter( 'command' );
32        $module = $this->moduleManager->getModule( $command, 'command' );
33        $module->execute();
34        $module->getResult()->addValue(
35            null,
36            $module->getModuleName(),
37            [ 'result' => 'Success' ]
38        );
39    }
40
41    /** @inheritDoc */
42    public function getModuleManager(): ApiModuleManager {
43        if ( !$this->moduleManager ) {
44            $submodules = array_map( static function ( $class ) {
45                return [
46                    'class' => $class,
47                    'factory' => "$class::factory",
48                ];
49            }, self::SUBMODULES );
50            $this->moduleManager = new ApiModuleManager(
51                $this,
52                MediaWikiServices::getInstance()->getObjectFactory()
53            );
54            $this->moduleManager->addModules( $submodules, 'command' );
55        }
56        return $this->moduleManager;
57    }
58
59    /** @inheritDoc */
60    protected function getAllowedParams(): array {
61        return [
62            'command' => [
63                ParamValidator::PARAM_TYPE => 'submodule',
64                ParamValidator::PARAM_REQUIRED => true,
65            ],
66        ];
67    }
68
69    /**
70     * Bail out with an API error if the user is not logged in.
71     * @throws ApiUsageException
72     */
73    private function checkLoginState(): void {
74        if ( !$this->getUser()->isRegistered() ) {
75            $this->dieWithError(
76                [ 'apierror-mustbeloggedin', $this->msg( 'action-editmyprivateinfo' ) ],
77                'notloggedin'
78            );
79        }
80    }
81
82    /** @inheritDoc */
83    public function getHelpUrls(): string {
84        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:Echo#API';
85    }
86
87    /** @inheritDoc */
88    public function isWriteMode(): bool {
89        return true;
90    }
91
92    /** @inheritDoc */
93    public function needsToken(): string {
94        return 'csrf';
95    }
96
97    /** @inheritDoc */
98    public function isInternal(): bool {
99        // experimental!
100        return true;
101    }
102
103}