Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
TemplateBasedTaskType
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
4 / 4
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getTemplates
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toJsonArray
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 newFromJsonArray
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace GrowthExperiments\NewcomerTasks\TaskType;
4
5use MediaWiki\Json\JsonUnserializer;
6use MediaWiki\Linker\LinkTarget;
7use MediaWiki\Title\TitleValue;
8
9class TemplateBasedTaskType extends TaskType {
10
11    /**
12     * List of templates any one of which identifies a wiki page as a candidate for this task type.
13     * @var LinkTarget[]
14     */
15    private $templates;
16
17    /**
18     * @param string $id Task type ID, e.g. 'copyedit'.
19     * @param string $difficulty One of the DIFFICULTY_* constants.
20     * @param array $extraData See TaskType::__construct()
21     * @param LinkTarget[] $templates
22     * @param LinkTarget[] $excludedTemplates
23     * @param LinkTarget[] $excludedCategories
24     */
25    public function __construct(
26        $id,
27        $difficulty,
28        array $extraData,
29        array $templates,
30        array $excludedTemplates = [],
31        array $excludedCategories = []
32    ) {
33        parent::__construct( $id, $difficulty, $extraData, $excludedTemplates, $excludedCategories );
34        $this->templates = $templates;
35    }
36
37    /**
38     * @return LinkTarget[]
39     */
40    public function getTemplates(): array {
41        return $this->templates;
42    }
43
44    /** @inheritDoc */
45    public function toJsonArray(): array {
46        return array_merge( parent::toJsonArray(), [
47            'templates' => array_map( static function ( LinkTarget $template ) {
48                return [ $template->getNamespace(), $template->getDBkey() ];
49            }, $this->getTemplates() )
50        ] );
51    }
52
53    /** @inheritDoc */
54    public static function newFromJsonArray( JsonUnserializer $unserializer, array $json ) {
55        $templates = array_map( static function ( array $template ) {
56            return new TitleValue( $template[0], $template[1] );
57        }, $json['templates'] );
58
59        $taskType = new TemplateBasedTaskType(
60            $json['id'],
61            $json['difficulty'],
62            $json['extraData'],
63            $templates,
64            self::getExcludedTemplatesTitleValues( $json ),
65            self::getExcludedCategoriesTitleValues( $json )
66        );
67        $taskType->setHandlerId( $json['handlerId'] );
68        return $taskType;
69    }
70
71}