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