Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ApiQuestionStore
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 execute
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
2
 getAllowedParams
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace GrowthExperiments\Api;
4
5use ApiBase;
6use GrowthExperiments\HelpPanel\QuestionPoster\HelpdeskQuestionPoster;
7use GrowthExperiments\HelpPanel\QuestionStoreFactory;
8use GrowthExperiments\HomepageModules\Mentorship;
9use GrowthExperiments\HomepageModules\RecentQuestionsFormatter;
10use JsonSerializable;
11use Wikimedia\ParamValidator\ParamValidator;
12
13class ApiQuestionStore extends ApiBase {
14
15    /**
16     * @inheritDoc
17     */
18    public function execute() {
19        $params = $this->extractRequestParams();
20        $questions = QuestionStoreFactory::newFromContextAndStorage(
21            $this->getContext(),
22            $params['storage']
23        )->loadQuestionsAndUpdate();
24        $questionFormatter = new RecentQuestionsFormatter(
25            $this->getContext(),
26            $questions,
27            $params['storage']
28        );
29        $result = [
30            'html' => $questionFormatter->format(),
31            'questions' => array_map( static function ( JsonSerializable $question ) {
32                return $question->jsonSerialize();
33            }, $questions ) ?: []
34        ];
35
36        $this->getResult()->addValue(
37            null,
38            $this->getModuleName(),
39            $result
40        );
41    }
42
43    /**
44     * @inheritDoc
45     */
46    public function getAllowedParams() {
47        return [
48            'storage' => [
49                ParamValidator::PARAM_REQUIRED => true,
50                ParamValidator::PARAM_TYPE => [ Mentorship::QUESTION_PREF, HelpdeskQuestionPoster::QUESTION_PREF ]
51            ],
52        ];
53    }
54}