Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
33.33% covered (danger)
33.33%
6 / 18
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
AddLinkSuggestionsHandler
33.33% covered (danger)
33.33%
6 / 18
0.00% covered (danger)
0.00%
0 / 4
21.52
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 run
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
4.25
 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 / 8
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace GrowthExperiments\Rest\Handler;
4
5use GrowthExperiments\ErrorException;
6use GrowthExperiments\NewcomerTasks\AddLink\LinkRecommendationHelper;
7use GrowthExperiments\Util;
8use MediaWiki\Context\RequestContext;
9use MediaWiki\Linker\LinkTarget;
10use MediaWiki\ParamValidator\TypeDef\TitleDef;
11use MediaWiki\Rest\HttpException;
12use MediaWiki\Rest\Response;
13use MediaWiki\Rest\SimpleHandler;
14use Wikimedia\ParamValidator\ParamValidator;
15
16/**
17 * Provide stored recommendations for a given page.
18 */
19class AddLinkSuggestionsHandler extends SimpleHandler {
20
21    /** @var LinkRecommendationHelper */
22    private $linkRecommendationHelper;
23
24    /**
25     * @param LinkRecommendationHelper $linkRecommendationHelper
26     */
27    public function __construct(
28        LinkRecommendationHelper $linkRecommendationHelper
29    ) {
30        $this->linkRecommendationHelper = $linkRecommendationHelper;
31    }
32
33    /**
34     * Entry point.
35     * @param LinkTarget $title
36     * @return Response|mixed A Response or a scalar passed to ResponseFactory::createFromReturnValue
37     * @throws HttpException
38     */
39    public function run( LinkTarget $title ) {
40        if ( !Util::areLinkRecommendationsEnabled( RequestContext::getMain() ) ) {
41            throw new HttpException( 'Disabled', 404 );
42        }
43        try {
44            $recommendation = $this->linkRecommendationHelper->getLinkRecommendation( $title );
45        } catch ( ErrorException $e ) {
46            throw new HttpException( $e->getErrorMessageInEnglish() );
47        }
48        if ( !$recommendation ) {
49            throw new HttpException( 'The recommendation has already been invalidated', 409 );
50        }
51        return [ 'recommendation' => $recommendation->toArray() ];
52    }
53
54    /** @inheritDoc */
55    public function needsWriteAccess() {
56        return false;
57    }
58
59    /** @inheritDoc */
60    public function getParamSettings() {
61        return [
62            'title' => [
63                self::PARAM_SOURCE => 'path',
64                ParamValidator::PARAM_TYPE => 'title',
65                ParamValidator::PARAM_REQUIRED => true,
66                TitleDef::PARAM_RETURN_OBJECT => true,
67            ],
68        ];
69    }
70
71}