Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
23.53% covered (danger)
23.53%
8 / 34
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
NewcomerTaskCompleteHandler
23.53% covered (danger)
23.53%
8 / 34
25.00% covered (danger)
25.00%
1 / 4
65.11
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 run
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
2.01
 getParamSettings
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 getErrorCodeForMessage
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3namespace GrowthExperiments\Rest\Handler;
4
5use GrowthExperiments\NewcomerTasks\NewcomerTasksChangeTagsManager;
6use MediaWiki\Rest\HttpException;
7use MediaWiki\Rest\SimpleHandler;
8use Wikimedia\ParamValidator\ParamValidator;
9
10/**
11 * Handle POST requests to /growthexperiments/v0/newcomertasks/complete
12 *
13 * Used for applying relevant change tags to revisions.
14 */
15class NewcomerTaskCompleteHandler extends SimpleHandler {
16
17    /**
18     * @var NewcomerTasksChangeTagsManager
19     */
20    private $newcomerTasksChangeTagsManager;
21
22    /**
23     * @param NewcomerTasksChangeTagsManager $newcomerTasksChangeTagsManager
24     */
25    public function __construct( NewcomerTasksChangeTagsManager $newcomerTasksChangeTagsManager ) {
26        $this->newcomerTasksChangeTagsManager = $newcomerTasksChangeTagsManager;
27    }
28
29    /**
30     * @return array
31     * @throws HttpException
32     */
33    public function run(): array {
34        $params = $this->getValidatedParams();
35
36        $result = $this->newcomerTasksChangeTagsManager->apply(
37            $params['taskTypeId'], $params['revId'], $this->getAuthority()->getUser()
38        );
39        if ( !$result->isGood() ) {
40            // HACK: We know we're not merging status values, so we can
41            // just use the first one.
42            $error = current( $result->getErrors() );
43            throw new HttpException( $error['message'], $this->getErrorCodeForMessage( $error['message'] ) );
44        }
45        return [ $result->getValue() ];
46    }
47
48    /** @inheritDoc */
49    public function getParamSettings() {
50        return [
51            'taskTypeId' => [
52                self::PARAM_SOURCE => 'query',
53                ParamValidator::PARAM_TYPE => 'string',
54                ParamValidator::PARAM_REQUIRED => true,
55            ],
56            'revId' => [
57                self::PARAM_SOURCE => 'query',
58                ParamValidator::PARAM_TYPE => 'integer',
59                ParamValidator::PARAM_REQUIRED => true,
60            ],
61        ];
62    }
63
64    /**
65     * @param string $message
66     * @return int
67     */
68    private function getErrorCodeForMessage( string $message ): int {
69        if ( strpos( $message, 'Invalid task type ID' ) !== false ) {
70            return 400;
71        } elseif ( strpos( $message, ' is not a valid revision ID' ) !== false ) {
72            return 400;
73        } elseif ( strpos( $message, 'revision does not match logged-in user ID' ) !== false ) {
74            return 400;
75        } elseif ( strpos( $message, 'Revision already has newcomer task tag.' ) !== false ) {
76            return 400;
77        } elseif ( strpos( $message, 'Suggested edits are not enabled or activated for your user.' ) !== false ) {
78            return 403;
79        } elseif ( strpos( $message, 'You must be logged-in' ) !== false ) {
80            return 403;
81        }
82        return 500;
83    }
84
85}