Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
18 / 18 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
FeaturesManager | |
100.00% |
18 / 18 |
|
100.00% |
7 / 7 |
12 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
useHookToRegisterExtensionOrSkinFeatures | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
registerFeature | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getAvailableForMode | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getFeature | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
isFeatureAvailableForCurrentUser | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
getMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MobileFrontend\Features; |
4 | |
5 | use MediaWiki\MediaWikiServices; |
6 | use MobileFrontend\Hooks\HookRunner; |
7 | |
8 | /** |
9 | * A facade for UserModes and MobileFrontend Features system. |
10 | * |
11 | * FeaturesManager takes care about all available user modes and features |
12 | * It handles relations between mode and features (one to many relation) |
13 | * and allows an easy acecss to single mode/feature so there is no need |
14 | * to retrieve objects from the MediaWikiServices. |
15 | * |
16 | * @package MobileFrontend\Features |
17 | */ |
18 | class FeaturesManager { |
19 | |
20 | /** |
21 | * A collection of available features |
22 | * |
23 | * @var array<IFeature> |
24 | */ |
25 | private $features = []; |
26 | |
27 | /** |
28 | * @var UserModes |
29 | */ |
30 | private $userModes; |
31 | |
32 | /** |
33 | * @param UserModes $userModes |
34 | */ |
35 | public function __construct( UserModes $userModes ) { |
36 | $this->userModes = $userModes; |
37 | } |
38 | |
39 | /** |
40 | * Allow other extensions to register features |
41 | */ |
42 | public function useHookToRegisterExtensionOrSkinFeatures() { |
43 | $hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ); |
44 | $hookRunner->onMobileFrontendFeaturesRegistration( $this ); |
45 | } |
46 | |
47 | /** |
48 | * Register a new MobileFronted feature |
49 | * @param IFeature $feature Feature to register |
50 | * @throws \RuntimeException |
51 | */ |
52 | public function registerFeature( IFeature $feature ) { |
53 | if ( array_key_exists( $feature->getId(), $this->features ) ) { |
54 | throw new \RuntimeException( 'Feature ' . $feature->getId() . ' is already defined.' ); |
55 | } |
56 | $this->features[ $feature->getId() ] = $feature; |
57 | } |
58 | |
59 | /** |
60 | * List all features that are available in given mode. |
61 | * This function do not check if user enabled given mode. |
62 | * |
63 | * @param IUserMode $mode User Mode |
64 | * @return array<IFeature> |
65 | */ |
66 | public function getAvailableForMode( IUserMode $mode ) { |
67 | return array_filter( $this->features, static function ( IFeature $feature ) use ( $mode ) { |
68 | return $feature->isAvailable( $mode ); |
69 | } ); |
70 | } |
71 | |
72 | /** |
73 | * Get feature |
74 | * @param string $id Feature id |
75 | * @return IFeature |
76 | */ |
77 | public function getFeature( $id ) { |
78 | if ( !array_key_exists( $id, $this->features ) ) { |
79 | throw new \RuntimeException( 'Feature ' . $id . ' is not defined.' ); |
80 | } |
81 | return $this->features[ $id ]; |
82 | } |
83 | |
84 | /** |
85 | * Check if given feature is available for current user |
86 | * |
87 | * @param string $featureId Feature identifier |
88 | * @return bool |
89 | */ |
90 | public function isFeatureAvailableForCurrentUser( $featureId ) { |
91 | $feature = $this->getFeature( $featureId ); |
92 | |
93 | /** @var IUserMode $userMode */ |
94 | foreach ( $this->userModes as $userMode ) { |
95 | if ( $userMode->isEnabled() && $feature->isAvailable( $userMode ) ) { |
96 | return true; |
97 | } |
98 | } |
99 | return false; |
100 | } |
101 | |
102 | /** |
103 | * Retrieve the current mode |
104 | * |
105 | * @param string $modeIdentifier |
106 | * @return IUserMode |
107 | */ |
108 | public function getMode( $modeIdentifier ) { |
109 | return $this->userModes->getMode( $modeIdentifier ); |
110 | } |
111 | } |