Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SuggestionsInfo
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 2
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getInfo
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2
3namespace GrowthExperiments\NewcomerTasks;
4
5use GrowthExperiments\NewcomerTasks\ConfigurationLoader\ConfigurationLoader;
6use GrowthExperiments\NewcomerTasks\Task\TaskSetFilters;
7use GrowthExperiments\NewcomerTasks\TaskSuggester\TaskSuggesterFactory;
8use GrowthExperiments\NewcomerTasks\TaskType\TaskTypeHandlerRegistry;
9use MediaWiki\Status\Status;
10use MediaWiki\User\UserIdentityValue;
11use StatusValue;
12
13class SuggestionsInfo implements NewcomerTasksInfo {
14    public const USER = 'SuggestionsInfo';
15
16    /**
17     * @var ConfigurationLoader
18     */
19    private $configurationLoader;
20    /**
21     * @var TaskTypeHandlerRegistry
22     */
23    private $taskTypeHandlerRegistry;
24    /**
25     * @var TaskSuggesterFactory
26     */
27    private $taskSuggesterFactory;
28
29    /**
30     * @param TaskSuggesterFactory $taskSuggesterFactory
31     * @param TaskTypeHandlerRegistry $taskTypeHandlerRegistry
32     * @param ConfigurationLoader $configurationLoader
33     */
34    public function __construct(
35        TaskSuggesterFactory $taskSuggesterFactory,
36        TaskTypeHandlerRegistry $taskTypeHandlerRegistry,
37        ConfigurationLoader $configurationLoader
38    ) {
39        $this->taskTypeHandlerRegistry = $taskTypeHandlerRegistry;
40        $this->configurationLoader = $configurationLoader;
41        $this->taskSuggesterFactory = $taskSuggesterFactory;
42    }
43
44    /** @inheritDoc */
45    public function getInfo( array $options = [] ): array {
46        $user = new UserIdentityValue( 0, 'SuggestionsInfo' );
47        $taskSuggester = $this->taskSuggesterFactory->create( $this->configurationLoader );
48        $taskTypes = $this->configurationLoader->loadTaskTypes();
49        $topics = $this->configurationLoader->loadTopics();
50        $data = [];
51        if ( $taskTypes instanceof StatusValue ) {
52            $data['error']['taskTypes'] = Status::wrap( $taskTypes )->getWikiText();
53        }
54        if ( $topics instanceof StatusValue ) {
55            $data['error']['topics'] = Status::wrap( $topics )->getWikiText();
56        }
57        if ( $taskTypes instanceof StatusValue || $topics instanceof StatusValue ) {
58            return $data;
59        }
60        $totalCount = 0;
61        foreach ( $taskTypes as $taskType ) {
62            $data['tasks'][$taskType->getId()]['totalCount'] = 0;
63            $data['tasks'][$taskType->getId()]['search'] = $this->taskTypeHandlerRegistry
64                ->getByTaskType( $taskType )
65                ->getSearchTerm( $taskType );
66            $result = $taskSuggester->suggest(
67                $user,
68                new TaskSetFilters(
69                    [ $taskType->getId() ],
70                    []
71                ),
72                0,
73                null,
74                [ 'useCache' => false ]
75            );
76            if ( $result instanceof StatusValue ) {
77                // Use -1 as a crude indicator of an error.
78                $data['tasks'][$taskType->getId()]['totalCount'] = -1;
79                continue;
80            }
81            $data['tasks'][$taskType->getId()]['totalCount'] += $result->getTotalCount();
82            $totalCount += $result->getTotalCount();
83        }
84        foreach ( $topics as $topic ) {
85            $data['topics'][$topic->getId()]['totalCount'] = 0;
86            foreach ( $taskTypes as $taskType ) {
87                $result = $taskSuggester->suggest(
88                    $user,
89                    new TaskSetFilters(
90                        [ $taskType->getId() ],
91                        [ $topic->getId() ]
92                    ),
93                    0,
94                    null,
95                    [ 'useCache' => false ]
96                );
97                if ( $result instanceof StatusValue ) {
98                    // Use -1 as a crude indicator of an error.
99                    $data['topics'][$topic->getId()]['tasks'][$taskType->getId()]['count'] = -1;
100                    continue;
101                }
102                $data['topics'][$topic->getId()]['tasks'][$taskType->getId()]['count'] =
103                    $result->getTotalCount();
104                $data['topics'][$topic->getId()]['totalCount'] += $result->getTotalCount();
105            }
106        }
107        $data['totalCount'] = $totalCount;
108        return $data;
109    }
110}