Debian Codegraph @

extensions/Echo/includes/Push/Api/ApiEchoPushSubscriptionsCreate.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Push\Api;
4
5 use MediaWiki\Api\ApiBase;
6 use MediaWiki\Api\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 public function __construct(
41 ApiMain $mainModule,
42 string $moduleName,
43 SubscriptionManager $subscriptionManager
44 ) {
45 parent::__construct( $mainModule, $moduleName );
46 $this->subscriptionManager = $subscriptionManager;
47 }
48
49 /**
50 * Entry point for executing the module.
51 * @inheritDoc
52 */
53 public function execute(): void {
54 $provider = $this->getParameter( 'provider' );
55 $token = $this->getParameter( 'providertoken' );
56 $topic = null;
57 // check if metadata is a JSON string correctly encoded
58 if ( $provider === 'apns' ) {
59 $topic = $this->getParameter( 'topic' );
60 if ( !$topic ) {
61 $this->dieWithError( 'apierror-echo-push-topic-required' );
62 }
63 }
64 $userId = Utils::getPushUserId( $this->getUser() );
65 $success = $this->subscriptionManager->create( $provider, $token, $userId, $topic );
66 if ( !$success ) {
67 $this->dieWithError( 'apierror-echo-push-token-exists' );
68 }
69 }
70
71 /**
72 * Get the parent module.
73 * @return ApiBase
74 */
75 public function getParent(): ApiBase {
76 return $this->parent;
77 }
78
79 /** @inheritDoc */
80 protected function getAllowedParams(): array {
81 return [
82 'provider' => [
83 ParamValidator::PARAM_TYPE => self::PROVIDERS,
84 ParamValidator::PARAM_REQUIRED => true,
85 ],
86 'providertoken' => [
87 ParamValidator::PARAM_TYPE => 'string',
88 ParamValidator::PARAM_REQUIRED => true,
89 ],
90 'topic' => [
91 ParamValidator::PARAM_TYPE => 'string',
92 ParamValidator::PARAM_REQUIRED => false,
93 ],
94 ];
95 }
96
97 /** @inheritDoc */
98 protected function getExamplesMessages(): array {
99 return [
100 "action=echopushsubscriptions&command=create&provider=fcm&providertoken=ABC123" =>
101 "apihelp-echopushsubscriptions+create-example"
102 ];
103 }
104
105 // The parent module already enforces these but they make documentation nicer.
106
107 /** @inheritDoc */
108 public function isWriteMode(): bool {
109 return true;
110 }
111
112 /** @inheritDoc */
113 public function mustBePosted(): bool {
114 return true;
115 }
116
117 /** @inheritDoc */
118 public function isInternal(): bool {
119 // experimental!
120 return true;
121 }
122
123 }