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