Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 52 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
ApiSetMenteeStatus | |
0.00% |
0 / 52 |
|
0.00% |
0 / 6 |
182 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
72 | |||
needsToken | |
0.00% |
0 / 1 |
|
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 | |||
getAllowedParams | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\Api; |
4 | |
5 | use ApiBase; |
6 | use ApiMain; |
7 | use GrowthExperiments\Mentorship\MentorManager; |
8 | use GrowthExperiments\Mentorship\Store\MentorStore; |
9 | use LogicException; |
10 | use MediaWiki\Config\Config; |
11 | use Wikimedia\ParamValidator\ParamValidator; |
12 | |
13 | class ApiSetMenteeStatus extends ApiBase { |
14 | |
15 | /** @var Config */ |
16 | private $wikiConfig; |
17 | |
18 | /** @var MentorManager */ |
19 | private $mentorManager; |
20 | |
21 | /** @var MentorStore */ |
22 | private $mentorStore; |
23 | |
24 | /** |
25 | * @param ApiMain $mainModule |
26 | * @param string $moduleName |
27 | * @param Config $wikiConfig |
28 | * @param MentorManager $mentorManager |
29 | * @param MentorStore $mentorStore |
30 | */ |
31 | public function __construct( |
32 | ApiMain $mainModule, |
33 | $moduleName, |
34 | Config $wikiConfig, |
35 | MentorManager $mentorManager, |
36 | MentorStore $mentorStore |
37 | ) { |
38 | parent::__construct( $mainModule, $moduleName ); |
39 | |
40 | $this->wikiConfig = $wikiConfig; |
41 | $this->mentorManager = $mentorManager; |
42 | $this->mentorStore = $mentorStore; |
43 | } |
44 | |
45 | /** |
46 | * @inheritDoc |
47 | */ |
48 | public function execute() { |
49 | if ( !$this->wikiConfig->get( 'GEMentorshipEnabled' ) ) { |
50 | $this->dieWithError( [ 'apierror-permissiondenied-generic' ] ); |
51 | } |
52 | |
53 | $params = $this->extractRequestParams(); |
54 | $user = $this->getAuthority()->getUser(); |
55 | |
56 | switch ( $params['state'] ) { |
57 | case 'enabled': |
58 | $newState = MentorManager::MENTORSHIP_ENABLED; |
59 | break; |
60 | case 'disabled': |
61 | $newState = MentorManager::MENTORSHIP_DISABLED; |
62 | break; |
63 | case 'optout': |
64 | $newState = MentorManager::MENTORSHIP_OPTED_OUT; |
65 | break; |
66 | default: |
67 | $newState = null; |
68 | break; |
69 | } |
70 | |
71 | if ( $newState === null ) { |
72 | // should not happen, unless getAllowedParams is wrong |
73 | throw new LogicException( 'Invalid mentee status passed through validation' ); |
74 | } |
75 | |
76 | $currentState = $this->mentorManager->getMentorshipStateForUser( $user ); |
77 | if ( $currentState === $newState ) { |
78 | $this->dieWithError( [ |
79 | 'apierror-growthexperiments-setmenteestatus-no-change' |
80 | ] ); |
81 | } |
82 | |
83 | $this->mentorManager->setMentorshipStateForUser( |
84 | $user, |
85 | $newState |
86 | ); |
87 | |
88 | $this->getResult()->addValue( null, $this->getModuleName(), [ |
89 | 'status' => 'ok', |
90 | ] ); |
91 | } |
92 | |
93 | /** |
94 | * @inheritDoc |
95 | */ |
96 | public function needsToken() { |
97 | return 'csrf'; |
98 | } |
99 | |
100 | /** |
101 | * @inheritDoc |
102 | */ |
103 | public function isWriteMode() { |
104 | return true; |
105 | } |
106 | |
107 | /** |
108 | * @inheritDoc |
109 | */ |
110 | public function mustBePosted() { |
111 | return true; |
112 | } |
113 | |
114 | /** |
115 | * @inheritDoc |
116 | */ |
117 | protected function getAllowedParams() { |
118 | return [ |
119 | 'state' => [ |
120 | ParamValidator::PARAM_REQUIRED => true, |
121 | ParamValidator::PARAM_TYPE => [ |
122 | 'enabled', |
123 | 'disabled', |
124 | 'optout' |
125 | ], |
126 | ApiBase::PARAM_HELP_MSG_PER_VALUE => [ |
127 | 'enabled' => 'apihelp-growthsetmenteestatus-param-state-enabled', |
128 | 'disabled' => 'apihelp-growthsetmenteestatus-param-state-disabled', |
129 | 'optout' => 'apihelp-growthsetmenteestatus-param-state-optout', |
130 | ] |
131 | ] |
132 | ]; |
133 | } |
134 | } |