Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
82.05% |
32 / 39 |
|
55.56% |
5 / 9 |
CRAP | |
0.00% |
0 / 1 |
ApiEchoPushSubscriptionsCreate | |
82.05% |
32 / 39 |
|
55.56% |
5 / 9 |
12.83 | |
0.00% |
0 / 1 |
factory | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
getParent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAllowedParams | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
getExamplesMessages | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
isWriteMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
mustBePosted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isInternal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Notifications\Push\Api; |
4 | |
5 | use ApiBase; |
6 | use ApiMain; |
7 | use MediaWiki\Extension\Notifications\Push\SubscriptionManager; |
8 | use MediaWiki\Extension\Notifications\Push\Utils; |
9 | use MediaWiki\Extension\Notifications\Services; |
10 | use Wikimedia\ParamValidator\ParamValidator; |
11 | |
12 | class ApiEchoPushSubscriptionsCreate extends ApiBase { |
13 | |
14 | /** |
15 | * Supported push notification providers: |
16 | * (1) fcm: Firebase Cloud Messaging |
17 | * (2) apns: Apple Push Notification Service |
18 | */ |
19 | private const PROVIDERS = [ 'fcm', 'apns' ]; |
20 | |
21 | /** @var ApiBase */ |
22 | private $parent; |
23 | |
24 | /** @var SubscriptionManager */ |
25 | private $subscriptionManager; |
26 | |
27 | /** |
28 | * Static entry point for initializing the module |
29 | * @param ApiBase $parent Parent module |
30 | * @param string $name Module name |
31 | * @return ApiEchoPushSubscriptionsCreate |
32 | */ |
33 | public static function factory( ApiBase $parent, string $name ): ApiEchoPushSubscriptionsCreate { |
34 | $subscriptionManger = Services::getInstance()->getPushSubscriptionManager(); |
35 | $module = new self( $parent->getMain(), $name, $subscriptionManger ); |
36 | $module->parent = $parent; |
37 | return $module; |
38 | } |
39 | |
40 | /** |
41 | * @param ApiMain $mainModule |
42 | * @param string $moduleName |
43 | * @param SubscriptionManager $subscriptionManager |
44 | */ |
45 | public function __construct( |
46 | ApiMain $mainModule, |
47 | string $moduleName, |
48 | SubscriptionManager $subscriptionManager |
49 | ) { |
50 | parent::__construct( $mainModule, $moduleName ); |
51 | $this->subscriptionManager = $subscriptionManager; |
52 | } |
53 | |
54 | /** |
55 | * Entry point for executing the module. |
56 | * @inheritDoc |
57 | */ |
58 | public function execute(): void { |
59 | $provider = $this->getParameter( 'provider' ); |
60 | $token = $this->getParameter( 'providertoken' ); |
61 | $topic = null; |
62 | // check if metadata is a JSON string correctly encoded |
63 | if ( $provider === 'apns' ) { |
64 | $topic = $this->getParameter( 'topic' ); |
65 | if ( !$topic ) { |
66 | $this->dieWithError( 'apierror-echo-push-topic-required' ); |
67 | } |
68 | } |
69 | $userId = Utils::getPushUserId( $this->getUser() ); |
70 | $success = $this->subscriptionManager->create( $provider, $token, $userId, $topic ); |
71 | if ( !$success ) { |
72 | $this->dieWithError( 'apierror-echo-push-token-exists' ); |
73 | } |
74 | } |
75 | |
76 | /** |
77 | * Get the parent module. |
78 | * @return ApiBase |
79 | */ |
80 | public function getParent(): ApiBase { |
81 | return $this->parent; |
82 | } |
83 | |
84 | /** @inheritDoc */ |
85 | protected function getAllowedParams(): array { |
86 | return [ |
87 | 'provider' => [ |
88 | ParamValidator::PARAM_TYPE => self::PROVIDERS, |
89 | ParamValidator::PARAM_REQUIRED => true, |
90 | ], |
91 | 'providertoken' => [ |
92 | ParamValidator::PARAM_TYPE => 'string', |
93 | ParamValidator::PARAM_REQUIRED => true, |
94 | ], |
95 | 'topic' => [ |
96 | ParamValidator::PARAM_TYPE => 'string', |
97 | ParamValidator::PARAM_REQUIRED => false, |
98 | ], |
99 | ]; |
100 | } |
101 | |
102 | /** @inheritDoc */ |
103 | protected function getExamplesMessages(): array { |
104 | return [ |
105 | "action=echopushsubscriptions&command=create&provider=fcm&providertoken=ABC123" => |
106 | "apihelp-echopushsubscriptions+create-example" |
107 | ]; |
108 | } |
109 | |
110 | // The parent module already enforces these but they make documentation nicer. |
111 | |
112 | /** @inheritDoc */ |
113 | public function isWriteMode(): bool { |
114 | return true; |
115 | } |
116 | |
117 | /** @inheritDoc */ |
118 | public function mustBePosted(): bool { |
119 | return true; |
120 | } |
121 | |
122 | /** @inheritDoc */ |
123 | public function isInternal(): bool { |
124 | // experimental! |
125 | return true; |
126 | } |
127 | |
128 | } |