Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
StaticLinkRecommendationProvider
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 2
12
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
 get
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace GrowthExperiments\NewcomerTasks\AddLink;
4
5use GrowthExperiments\NewcomerTasks\TaskType\LinkRecommendationTaskType;
6use GrowthExperiments\NewcomerTasks\TaskType\TaskType;
7use MediaWiki\Linker\LinkTarget;
8use OutOfBoundsException;
9use StatusValue;
10use Wikimedia\Assert\Assert;
11
12class StaticLinkRecommendationProvider implements LinkRecommendationProvider {
13
14    /** @var (LinkRecommendation|StatusValue)[] */
15    private $recommendations;
16
17    /** @var LinkRecommendation|StatusValue|null */
18    private $default;
19
20    /**
21     * @param (LinkRecommendation|StatusValue)[] $recommendations Title => recommendation
22     *   where title is in a <namespace number>:<dbkey> format.
23     * @param LinkRecommendation|StatusValue|null $default Default recommendation to use for titles
24     *   not present in $recommendations. When unset, will throw an error for such titles.
25     */
26    public function __construct( array $recommendations, $default = null ) {
27        $this->recommendations = $recommendations;
28        $this->default = $default;
29    }
30
31    /** @inheritDoc */
32    public function get( LinkTarget $title, TaskType $taskType ) {
33        Assert::parameterType( LinkRecommendationTaskType::class, $taskType, '$taskType' );
34        $target = $title->getNamespace() . ':' . $title->getDBkey();
35        $ret = $this->recommendations[$target] ?? $this->default;
36        if ( $ret === null ) {
37            throw new OutOfBoundsException( "No recommendation for $target" );
38        }
39        return $ret;
40    }
41
42}