Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.42% covered (warning)
77.42%
24 / 31
55.56% covered (warning)
55.56%
5 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiEchoPushSubscriptionsDelete
77.42% covered (warning)
77.42%
24 / 31
55.56% covered (warning)
55.56%
5 / 9
13.66
0.00% covered (danger)
0.00%
0 / 1
 factory
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 getParent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAllowedParams
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
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
 mustBePosted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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 ApiMain;
7use ApiUsageException;
8use MediaWiki\Extension\Notifications\Push\SubscriptionManager;
9use MediaWiki\Extension\Notifications\Push\Utils;
10use MediaWiki\Extension\Notifications\Services;
11use Wikimedia\ParamValidator\ParamValidator;
12
13class ApiEchoPushSubscriptionsDelete extends ApiBase {
14
15    /** @var ApiBase */
16    private $parent;
17
18    /** @var SubscriptionManager */
19    private $subscriptionManager;
20
21    /**
22     * Static entry point for initializing the module
23     * @param ApiBase $parent Parent module
24     * @param string $name Module name
25     * @return ApiEchoPushSubscriptionsDelete
26     */
27    public static function factory( ApiBase $parent, string $name ): ApiEchoPushSubscriptionsDelete {
28        $subscriptionManager = Services::getInstance()->getPushSubscriptionManager();
29        $module = new self( $parent->getMain(), $name, $subscriptionManager );
30        $module->parent = $parent;
31        return $module;
32    }
33
34    /**
35     * @param ApiMain $mainModule
36     * @param string $moduleName
37     * @param SubscriptionManager $subscriptionManager
38     */
39    public function __construct(
40        ApiMain $mainModule,
41        string $moduleName,
42        SubscriptionManager $subscriptionManager
43    ) {
44        parent::__construct( $mainModule, $moduleName );
45        $this->subscriptionManager = $subscriptionManager;
46    }
47
48    /**
49     * Entry point for executing the module.
50     * @inheritDoc
51     */
52    public function execute(): void {
53        $tokens = $this->getParameter( 'providertoken' );
54        $userId = null;
55
56        if ( !$tokens ) {
57            $this->dieWithError( [ 'apierror-paramempty', 'providertoken' ], 'paramempty_providertoken' );
58        }
59        // Restrict deletion to the user's own token(s) if not a push subscription manager
60        try {
61            $this->checkUserRightsAny( 'manage-all-push-subscriptions' );
62        } catch ( ApiUsageException $e ) {
63            $userId = Utils::getPushUserId( $this->getUser() );
64        }
65
66        $numRowsDeleted = $this->subscriptionManager->delete( $tokens, $userId );
67        if ( $numRowsDeleted == 0 ) {
68            $this->dieWithError( 'apierror-echo-push-token-not-found' );
69        }
70    }
71
72    /**
73     * Get the parent module.
74     * @return ApiBase
75     */
76    public function getParent(): ApiBase {
77        return $this->parent;
78    }
79
80    /** @inheritDoc */
81    protected function getAllowedParams(): array {
82        return [
83            'providertoken' => [
84                ParamValidator::PARAM_TYPE => 'string',
85                ParamValidator::PARAM_REQUIRED => true,
86                ParamValidator::PARAM_ISMULTI => true,
87            ],
88        ];
89    }
90
91    /** @inheritDoc */
92    protected function getExamplesMessages(): array {
93        return [
94            "action=echopushsubscriptions&command=delete&providertoken=ABC123" =>
95                "apihelp-echopushsubscriptions+delete-example"
96        ];
97    }
98
99    // The parent module already enforces these but they make documentation nicer.
100
101    /** @inheritDoc */
102    public function isWriteMode(): bool {
103        return true;
104    }
105
106    /** @inheritDoc */
107    public function mustBePosted(): bool {
108        return true;
109    }
110
111    /** @inheritDoc */
112    public function isInternal(): bool {
113        // experimental!
114        return true;
115    }
116
117}