Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
SetTranslationHandler | |
0.00% |
0 / 38 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
run | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
12 | |||
getParamSettings | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
getBodyValidator | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\SecurePoll\Rest; |
4 | |
5 | use MediaWiki\Extension\SecurePoll\Context; |
6 | use MediaWiki\Extension\SecurePoll\TranslationRepo; |
7 | use MediaWiki\Rest\HttpException; |
8 | use MediaWiki\Rest\Response; |
9 | use MediaWiki\Rest\SimpleHandler; |
10 | use MediaWiki\Rest\Validator\JsonBodyValidator; |
11 | use RequestContext; |
12 | use Wikimedia\ParamValidator\ParamValidator; |
13 | |
14 | class SetTranslationHandler extends SimpleHandler { |
15 | |
16 | /** @var TranslationRepo */ |
17 | private $translationRepo; |
18 | |
19 | /** |
20 | * @param TranslationRepo $translationRepo |
21 | */ |
22 | public function __construct( $translationRepo ) { |
23 | $this->translationRepo = $translationRepo; |
24 | } |
25 | |
26 | /** |
27 | * @inheritDoc |
28 | */ |
29 | public function run( $params ): Response { |
30 | $request = $this->getRequest(); |
31 | |
32 | $electionId = (int)$request->getPathParam( 'entityid' ); |
33 | $language = $request->getPathParam( 'language' ); |
34 | $body = $this->getValidatedBody(); |
35 | |
36 | if ( !$body ) { |
37 | throw new HttpException( 'No valid body' ); |
38 | } |
39 | |
40 | $context = RequestContext::getMain(); |
41 | $user = $context->getUser(); |
42 | $sp_context = new Context; |
43 | $election = $sp_context->getElection( $electionId ); |
44 | if ( !$election ) { |
45 | throw new HttpException( 'No valid election' ); |
46 | } |
47 | |
48 | $this->translationRepo->setTranslation( |
49 | $election, |
50 | $body['data'], |
51 | $language, |
52 | $user, |
53 | '' |
54 | ); |
55 | |
56 | return $this->getResponseFactory()->createJson( [ |
57 | 'success' => true |
58 | ] ); |
59 | } |
60 | |
61 | /** @inheritDoc */ |
62 | public function getParamSettings() { |
63 | return [ |
64 | 'entityid' => [ |
65 | static::PARAM_SOURCE => 'path', |
66 | ParamValidator::PARAM_REQUIRED => true, |
67 | ParamValidator::PARAM_TYPE => 'string' |
68 | ], |
69 | 'language' => [ |
70 | static::PARAM_SOURCE => 'path', |
71 | ParamValidator::PARAM_REQUIRED => true, |
72 | ParamValidator::PARAM_TYPE => 'string' |
73 | ] |
74 | ]; |
75 | } |
76 | |
77 | /** |
78 | * @param string $contentType |
79 | * @return JsonBodyValidator |
80 | */ |
81 | public function getBodyValidator( $contentType ) { |
82 | if ( $contentType === 'application/json' ) { |
83 | return new JsonBodyValidator( [] ); |
84 | } |
85 | throw new HttpException( 'Content must be of type application/json', 415 ); |
86 | } |
87 | |
88 | } |