Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
WelcomeSurveySkipHandler | |
0.00% |
0 / 18 |
|
0.00% |
0 / 4 |
30 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
getBodyParamSettings | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
getSupportedRequestTypes | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\Rest\Handler; |
4 | |
5 | use GrowthExperiments\WelcomeSurveyFactory; |
6 | use MediaWiki\Context\RequestContext; |
7 | use MediaWiki\Rest\Handler; |
8 | use MediaWiki\Rest\HttpException; |
9 | use MediaWiki\Rest\Response; |
10 | use Wikimedia\ParamValidator\ParamValidator; |
11 | |
12 | /** |
13 | * Handle POST requests to /growthexperiments/v0/welcomesurvey/skip |
14 | * |
15 | * Dismisses the welcome survey (in practice, used to dismiss reminder notices about the survey). |
16 | * |
17 | * As a no-JS fallback Special:WelcomeSurvey/skip is used. |
18 | */ |
19 | class WelcomeSurveySkipHandler extends Handler { |
20 | |
21 | /** @var WelcomeSurveyFactory */ |
22 | private $welcomeSurveyFactory; |
23 | |
24 | /** |
25 | * @param WelcomeSurveyFactory $welcomeSurveyFactory |
26 | */ |
27 | public function __construct( |
28 | WelcomeSurveyFactory $welcomeSurveyFactory |
29 | ) { |
30 | $this->welcomeSurveyFactory = $welcomeSurveyFactory; |
31 | } |
32 | |
33 | /** |
34 | * @return array|Response |
35 | * @throws HttpException |
36 | */ |
37 | public function execute() { |
38 | $body = $this->getValidatedBody(); |
39 | '@phan-var array $body'; |
40 | |
41 | if ( !$this->getSession()->getToken( 'welcomesurvey' )->match( $body['token'] ) ) { |
42 | throw new HttpException( 'Invalid token', 400 ); |
43 | } |
44 | |
45 | $welcomeSurvey = $this->welcomeSurveyFactory->newWelcomeSurvey( RequestContext::getMain() ); |
46 | $welcomeSurvey->dismiss(); |
47 | |
48 | return [ 'success' => true ]; |
49 | } |
50 | |
51 | /** @inheritDoc */ |
52 | public function getBodyParamSettings(): array { |
53 | return [ |
54 | 'token' => [ |
55 | self::PARAM_SOURCE => 'body', |
56 | ParamValidator::PARAM_TYPE => 'string', |
57 | ParamValidator::PARAM_REQUIRED => true, |
58 | ], |
59 | ]; |
60 | } |
61 | |
62 | /** |
63 | * Support x-www-form-urlencoded (and nothing else), as required by RFC 6749. |
64 | * @return string[] |
65 | */ |
66 | public function getSupportedRequestTypes(): array { |
67 | return [ |
68 | 'application/x-www-form-urlencoded' |
69 | ]; |
70 | } |
71 | |
72 | } |