Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MentorDashboardModuleRegistry
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
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
12
 getModules
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getWiring
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace GrowthExperiments\MentorDashboard;
4
5use GrowthExperiments\DashboardModule\IDashboardModule;
6use GrowthExperiments\GrowthExperimentsServices;
7use GrowthExperiments\MentorDashboard\Modules\MenteeOverview;
8use GrowthExperiments\MentorDashboard\Modules\MentorTools;
9use GrowthExperiments\MentorDashboard\Modules\PersonalizedPraise;
10use GrowthExperiments\MentorDashboard\Modules\Resources;
11use MediaWiki\Context\IContextSource;
12use MediaWiki\MediaWikiServices;
13
14class MentorDashboardModuleRegistry {
15
16    private MediaWikiServices $services;
17
18    /**
19     * @var callable[] id => module factory function
20     */
21    private ?array $wiring = null;
22
23    /** @var IDashboardModule[] */
24    private array $modules;
25
26    /**
27     * @param MediaWikiServices $services
28     */
29    public function __construct(
30        MediaWikiServices $services
31    ) {
32        $this->services = $services;
33    }
34
35    /**
36     * @param string $moduleId
37     * @param IContextSource $context
38     * @return IDashboardModule
39     */
40    public function get( string $moduleId, IContextSource $context ): IDashboardModule {
41        if ( isset( $this->modules[$moduleId] ) ) {
42            return $this->modules[$moduleId];
43        }
44
45        if ( $this->wiring === null ) {
46            $this->wiring = self::getWiring();
47        }
48
49        $this->modules[$moduleId] = $this->wiring[$moduleId]( $this->services, $context );
50        return $this->modules[$moduleId];
51    }
52
53    /**
54     * @return array
55     */
56    public static function getModules(): array {
57        return array_keys( self::getWiring() );
58    }
59
60    /**
61     * @return callable[]
62     */
63    private static function getWiring() {
64        return [
65            'mentee-overview' => static function (
66                MediaWikiServices $services,
67                IContextSource $context
68            ): IDashboardModule {
69                return new MenteeOverview(
70                    'mentee-overview',
71                    $context
72                );
73            },
74            'resources' => static function (
75                MediaWikiServices $services,
76                IContextSource $context
77            ): IDashboardModule {
78                $geServices = GrowthExperimentsServices::wrap( $services );
79                return new Resources(
80                    'resources',
81                    $context,
82                    $services->getTitleParser(),
83                    $services->getLinkRenderer(),
84                    $geServices->getMentorProvider()
85                );
86            },
87            'mentor-tools' => static function (
88                MediaWikiServices $services,
89                IContextSource $context
90            ): IDashboardModule {
91                $geServices = GrowthExperimentsServices::wrap( $services );
92                return new MentorTools(
93                    'mentor-tools',
94                    $context,
95                    $geServices->getMentorProvider(),
96                    $geServices->getMentorStatusManager()
97                );
98            },
99            'personalized-praise' => static function (
100                MediaWikiServices $services,
101                IContextSource $context
102            ) {
103                $geServices = GrowthExperimentsServices::wrap( $services );
104                return new PersonalizedPraise(
105                    'personalized-praise',
106                    $context,
107                    $geServices->getPraiseworthyMenteeSuggester(),
108                    $geServices->getPersonalizedPraiseSettings(),
109                    $services->getGenderCache()
110                );
111            },
112        ];
113    }
114}