Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
TipsHandler
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 4
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
6
 needsWriteAccess
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getParamSettings
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace GrowthExperiments\Rest\Handler;
4
5use GrowthExperiments\GrowthExperimentsServices;
6use GrowthExperiments\HelpPanel\Tips\TipsAssembler;
7use GrowthExperiments\NewcomerTasks\ConfigurationLoader\ConfigurationLoader;
8use MediaWiki\Context\DerivativeContext;
9use MediaWiki\Context\RequestContext;
10use MediaWiki\MediaWikiServices;
11use MediaWiki\Rest\Response;
12use MediaWiki\Rest\SimpleHandler;
13use Wikimedia\ParamValidator\ParamValidator;
14
15/**
16 * Handle incoming requests to obtain tips for a skin, editor, task type id,
17 * and language. Returns a JSON response that can be placed into the suggested
18 * edits guidance screen in the help panel.
19 */
20class TipsHandler extends SimpleHandler {
21
22    private const MAX_CACHE_AGE_SECONDS = 3600;
23
24    /**
25     * @var TipsAssembler
26     */
27    private $tipsAssembler;
28    /**
29     * @var ConfigurationLoader
30     */
31    private $configurationLoader;
32
33    /**
34     * @param TipsAssembler $tipsAssembler
35     * @param ConfigurationLoader $configurationLoader
36     */
37    public function __construct(
38        TipsAssembler $tipsAssembler, ConfigurationLoader $configurationLoader
39    ) {
40        $this->tipsAssembler = $tipsAssembler;
41        $this->configurationLoader = $configurationLoader;
42    }
43
44    /**
45     * @param string $skin
46     * @param string $editor
47     * @param string $tasktypeid
48     * @param string $uselang
49     * @return Response
50     */
51    public function run( string $skin, string $editor, string $tasktypeid, string $uselang
52    ) {
53        $context = new DerivativeContext( RequestContext::getMain() );
54        if ( $uselang ) {
55            $context->setLanguage( $uselang );
56        }
57        // FIXME the context language should be set by the API framework
58        $this->tipsAssembler->setMessageLocalizer( $context );
59        GrowthExperimentsServices::wrap( MediaWikiServices::getInstance() )
60            ->getNewcomerTasksConfigurationValidator()->setMessageLocalizer( $context );
61        $taskTypes = $this->configurationLoader->getTaskTypes();
62        $response = $this->getResponseFactory()->createJson(
63            $this->tipsAssembler->getTips(
64                $skin,
65                $editor,
66                $taskTypes,
67                $tasktypeid,
68                $context->getLanguage()->getDir()
69            )
70        );
71        $response->setHeader( 'Cache-Control', 'public, max-age=' . self::MAX_CACHE_AGE_SECONDS );
72        return $response;
73    }
74
75    public function needsWriteAccess() {
76        return false;
77    }
78
79    /** @inheritDoc */
80    public function getParamSettings() {
81        return [
82            'skin' => [
83                self::PARAM_SOURCE => 'path',
84                ParamValidator::PARAM_REQUIRED => true,
85                ParamValidator::PARAM_TYPE => 'string',
86            ],
87            'editor' => [
88                self::PARAM_SOURCE => 'path',
89                ParamValidator::PARAM_REQUIRED => true,
90                ParamValidator::PARAM_TYPE => 'string',
91            ],
92            'tasktypeid' => [
93                self::PARAM_SOURCE => 'path',
94                ParamValidator::PARAM_REQUIRED => true,
95                ParamValidator::PARAM_TYPE => array_keys(
96                    $this->configurationLoader->getTaskTypes()
97                )
98            ],
99            'uselang' => [
100                self::PARAM_SOURCE => 'path',
101                ParamValidator::PARAM_REQUIRED => true,
102            ]
103        ];
104    }
105}