Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
InitModule
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 getDependencies
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 hasEnabledSurveys
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace QuickSurveys\ResourceLoader;
4
5use MediaWiki\MediaWikiServices;
6use MediaWiki\ResourceLoader as RL;
7
8/**
9 * AKA The ext.quicksurveys.init module.
10 *
11 * By default, this module is empty. When there's at least one enabled survey defined then it
12 * depends on the ext.quicksurveys.lib module and loads a script to show an available survey.
13 * When there a no enabled surveys defined the majority of the frontend codebase won't be
14 * fetched, parsed, and executed by the client.
15 *
16 * See T213459#4871107 for the original design of this approach.
17 */
18class InitModule extends RL\FileModule {
19
20    /** @inheritDoc */
21    public function __construct(
22        array $options = [],
23        string $localBasePath = null,
24        string $remoteBasePath = null
25    ) {
26        if ( $this->hasEnabledSurveys() ) {
27            $options = array_merge( $options, [
28                'packageFiles' => [
29                    'resources/ext.quicksurveys.init/init.js',
30                ],
31            ] );
32        }
33        parent::__construct( $options, $localBasePath, $remoteBasePath );
34    }
35
36    /** @inheritDoc */
37    public function getDependencies( RL\Context $context = null ) {
38        $result = parent::getDependencies( $context );
39
40        if ( $this->hasEnabledSurveys() ) {
41            $result = array_merge( $result, [ 'ext.quicksurveys.lib' ] );
42        }
43
44        return $result;
45    }
46
47    private function hasEnabledSurveys(): bool {
48        $enabledSurveys = MediaWikiServices::getInstance()->getService( 'QuickSurveys.EnabledSurveys' );
49        return count( $enabledSurveys ) > 0;
50    }
51}