Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
12 / 13
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
StaticImageRecommendationProvider
92.31% covered (success)
92.31%
12 / 13
66.67% covered (warning)
66.67%
2 / 3
6.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 get
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 normalize
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace GrowthExperiments\NewcomerTasks\AddImage;
4
5use GrowthExperiments\NewcomerTasks\TaskType\ImageRecommendationBaseTaskType;
6use GrowthExperiments\NewcomerTasks\TaskType\TaskType;
7use MediaWiki\Linker\LinkTarget;
8use OutOfBoundsException;
9use StatusValue;
10use Wikimedia\Assert\Assert;
11
12/**
13 * Simple provider with hardcoded responses for development setups.
14 */
15class StaticImageRecommendationProvider implements ImageRecommendationProvider {
16
17    /** @var (ImageRecommendation|StatusValue)[] */
18    private $recommendations;
19
20    /** @var ImageRecommendation|StatusValue|null */
21    private $default;
22
23    /**
24     * @param (ImageRecommendation|StatusValue|array)[] $recommendations Title => recommendation
25     *   where title is in a `<namespace number>:<dbkey>` format. The recommendation can be an
26     *   ImageRecommendation object, or serialized (loadable with ImageRecommendation::fromArray).
27     * @param ImageRecommendation|StatusValue|array $default Default recommendation to use for titles
28     *   not present in $recommendations. When unset, will throw an error for such titles.
29     */
30    public function __construct( array $recommendations, $default ) {
31        $this->recommendations = array_map( [ $this, 'normalize' ], $recommendations );
32        $this->default = $default ? $this->normalize( $default ) : null;
33    }
34
35    /** @inheritDoc */
36    public function get( LinkTarget $title, TaskType $taskType ) {
37        Assert::parameterType( ImageRecommendationBaseTaskType::class, $taskType, '$taskType' );
38        $target = $title->getNamespace() . ':' . $title->getDBkey();
39        $ret = $this->recommendations[$target] ?? $this->default;
40        if ( $ret === null ) {
41            throw new OutOfBoundsException( "No recommendation for $target" );
42        }
43        return $ret;
44    }
45
46    /**
47     * @param ImageRecommendation|StatusValue|array $recommendation
48     * @return ImageRecommendation|StatusValue
49     */
50    private function normalize( $recommendation ) {
51        Assert::parameterType( [ ImageRecommendation::class, StatusValue::class, 'array' ],
52            $recommendation, '$recommendation' );
53        return is_array( $recommendation )
54            ? ImageRecommendation::fromArray( $recommendation )
55            : $recommendation;
56    }
57
58}