Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
StaticTaskSuggesterFactory
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 create
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace GrowthExperiments\NewcomerTasks\TaskSuggester;
4
5use GrowthExperiments\NewcomerTasks\ConfigurationLoader\ConfigurationLoader;
6use MediaWiki\Title\TitleFactory;
7use StatusValue;
8
9/**
10 * Pseudo-factory for returning a pre-configured task suggester (not necessarily a
11 * StaticTaskSuggester) or error. Intended for testing and local frontend development.
12 *
13 * To use it, register a MediaWikiServices hook along the lines of
14 *
15 *     $wgHooks['MediaWikiServices'][] = function ( MediaWikiServices $services ) {
16 *         $services->redefineService( 'GrowthExperimentsTaskSuggesterFactory', function () use ( $services ) {
17 *             $taskType = new TaskType( 'copyedit', TaskType::DIFFICULTY_EASY );
18 *             return new StaticTaskSuggesterFactory( [
19 *                 new Task( $taskType, new TitleValue( NS_MAIN, 'Foo' ) ),
20 *                 new Task( $taskType, new TitleValue( NS_MAIN, 'Bar' ) ),
21 *             ], $services->getTitleFactory() );
22 *         } );
23 *     };
24 */
25class StaticTaskSuggesterFactory extends TaskSuggesterFactory {
26
27    /** @var TaskSuggester */
28    private $taskSuggester;
29
30    /**
31     * @param TaskSuggester|StatusValue|array $taskSuggester A TaskSuggester, an array of
32     *   suggestions to create a StaticTaskSuggester with, or an error to create an
33     *   ErrorForwardingTaskSuggester with.
34     * @param TitleFactory|null $titleFactory
35     */
36    public function __construct( $taskSuggester, ?TitleFactory $titleFactory = null ) {
37        if ( $taskSuggester instanceof TaskSuggester ) {
38            $this->taskSuggester = $taskSuggester;
39        } elseif ( $taskSuggester instanceof StatusValue ) {
40            $this->taskSuggester = $this->createError( $taskSuggester );
41        } else {
42            $this->taskSuggester = new StaticTaskSuggester( $taskSuggester, $titleFactory );
43        }
44    }
45
46    /** @inheritDoc */
47    public function create( ?ConfigurationLoader $customConfigurationLoader = null ) {
48        return $this->taskSuggester;
49    }
50
51}