Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
GetParticipantQuestionsHandler
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
6
 getParamSettings
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare( strict_types=1 );
4
5namespace MediaWiki\Extension\CampaignEvents\Rest;
6
7use MediaWiki\Extension\CampaignEvents\Questions\EventQuestionsRegistry;
8use MediaWiki\Language\Language;
9use MediaWiki\Rest\Handler;
10use MediaWiki\Rest\Response;
11use Wikimedia\Message\IMessageFormatterFactory;
12use Wikimedia\Message\MessageValue;
13use Wikimedia\ParamValidator\ParamValidator;
14
15class GetParticipantQuestionsHandler extends Handler {
16    private EventQuestionsRegistry $eventQuestionsRegistry;
17    private IMessageFormatterFactory $messageFormatterFactory;
18    private Language $contentLanguage;
19
20    /**
21     * @param EventQuestionsRegistry $eventQuestionsRegistry
22     * @param IMessageFormatterFactory $messageFormatterFactory
23     * @param Language $contentLanguage
24     */
25    public function __construct(
26        EventQuestionsRegistry $eventQuestionsRegistry,
27        IMessageFormatterFactory $messageFormatterFactory,
28        Language $contentLanguage
29    ) {
30        $this->eventQuestionsRegistry = $eventQuestionsRegistry;
31        $this->messageFormatterFactory = $messageFormatterFactory;
32        $this->contentLanguage = $contentLanguage;
33    }
34
35    /**
36     * @return Response
37     */
38    public function execute(): Response {
39        $params = $this->getValidatedParams();
40        $questionIDs = $params['question_ids'] ?? null;
41        $questions = $this->eventQuestionsRegistry->getQuestionsForAPI( $questionIDs );
42
43        // FIXME: use appropriate language when T269492 is resolved.
44        $msgFormatter = $this->messageFormatterFactory->getTextFormatter( $this->contentLanguage->getCode() );
45
46        $response = [];
47        foreach ( $questions as $questionID => $question ) {
48            $curQuestionData = [
49                'name' => $question['name'],
50                'type' => $question['type'],
51                'label' => $msgFormatter->format( MessageValue::new( $question['label-message'] ) ),
52            ];
53            if ( isset( $question['options-messages'] ) ) {
54                $curQuestionData['options'] = [];
55                foreach ( $question['options-messages'] as $msgKey => $val ) {
56                    $optionText = $msgFormatter->format( MessageValue::new( $msgKey ) );
57                    $curQuestionData['options'][$optionText] = $val;
58                }
59            }
60            if ( isset( $question['other-options'] ) ) {
61                $curQuestionData['other-options'] = [];
62                foreach ( $question['other-options'] as $showIfVal => $optionData ) {
63                    $optionRespData = [
64                        'type' => $optionData['type'],
65                        'label' => $msgFormatter->format( MessageValue::new( $optionData['label-message'] ) )
66                    ];
67                    $curQuestionData['other-options'][$showIfVal] = $optionRespData;
68                }
69            }
70            $response[$questionID] = $curQuestionData;
71        }
72
73        return $this->getResponseFactory()->createJson( $response );
74    }
75
76    /**
77     * @inheritDoc
78     */
79    public function getParamSettings(): array {
80        return [
81            'question_ids' => [
82                static::PARAM_SOURCE => 'query',
83                ParamValidator::PARAM_TYPE => 'integer',
84                ParamValidator::PARAM_ISMULTI => true
85            ],
86        ];
87    }
88}