Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
76.19% |
32 / 42 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
ApiNewsletterSubscribe | |
76.19% |
32 / 42 |
|
66.67% |
4 / 6 |
13.94 | |
0.00% |
0 / 1 |
execute | |
82.61% |
19 / 23 |
|
0.00% |
0 / 1 |
7.26 | |||
getAllowedParams | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
getExamplesMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
isWriteMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
needsToken | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
mustBePosted | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Newsletter\Api; |
4 | |
5 | use LogicException; |
6 | use MediaWiki\Api\ApiBase; |
7 | use MediaWiki\Extension\Newsletter\Newsletter; |
8 | use Wikimedia\ParamValidator\ParamValidator; |
9 | |
10 | /** |
11 | * @license GPL-2.0-or-later |
12 | * @author Glaisher |
13 | */ |
14 | class ApiNewsletterSubscribe extends ApiBase { |
15 | |
16 | public function execute() { |
17 | $user = $this->getUser(); |
18 | |
19 | if ( !$user->isRegistered() ) { |
20 | $this->dieWithError( 'newsletter-api-error-subscribe-notloggedin', 'notloggedin' ); |
21 | } |
22 | |
23 | $params = $this->extractRequestParams(); |
24 | $newsletter = Newsletter::newFromID( $params['id'] ); |
25 | |
26 | if ( !$newsletter ) { |
27 | $this->dieWithError( 'newsletter-api-error-notfound', 'notfound' ); |
28 | } |
29 | |
30 | switch ( $params['do'] ) { |
31 | case 'subscribe': |
32 | $status = $newsletter->subscribe( $user ); |
33 | break; |
34 | case 'unsubscribe': |
35 | $status = $newsletter->unsubscribe( $user ); |
36 | break; |
37 | default: |
38 | throw new LogicException( 'do action not implemented' ); |
39 | } |
40 | |
41 | if ( !$status->isGood() ) { |
42 | $this->dieStatus( $status ); |
43 | } |
44 | |
45 | $this->getResult()->addValue( null, $this->getModuleName(), |
46 | [ |
47 | 'id' => $newsletter->getId(), |
48 | 'name' => $newsletter->getName(), |
49 | ] |
50 | ); |
51 | } |
52 | |
53 | public function getAllowedParams() { |
54 | return [ |
55 | 'id' => [ |
56 | ParamValidator::PARAM_TYPE => 'integer', |
57 | ParamValidator::PARAM_REQUIRED => true, |
58 | ], |
59 | 'do' => [ |
60 | ParamValidator::PARAM_TYPE => [ 'subscribe', 'unsubscribe' ], |
61 | ParamValidator::PARAM_REQUIRED => true, |
62 | ], |
63 | ]; |
64 | } |
65 | |
66 | /** |
67 | * @see ApiBase::getExamplesMessages() |
68 | * @return array |
69 | */ |
70 | protected function getExamplesMessages() { |
71 | return [ |
72 | 'action=newslettersubscribe&id=1&do=subscribe' |
73 | => 'apihelp-newslettersubscribe-example-1', |
74 | 'action=newslettersubscribe&id=2&do=unsubscribe' |
75 | => 'apihelp-newslettersubscribe-example-2', |
76 | ]; |
77 | } |
78 | |
79 | public function isWriteMode() { |
80 | return true; |
81 | } |
82 | |
83 | public function needsToken() { |
84 | return 'csrf'; |
85 | } |
86 | |
87 | public function mustBePosted() { |
88 | return true; |
89 | } |
90 | |
91 | } |