Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
11.76% |
2 / 17 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
UserPreferenceRequirement | |
11.76% |
2 / 17 |
|
25.00% |
1 / 4 |
30.73 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isPreferenceEnabled | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
isMet | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * http://www.gnu.org/copyleft/gpl.html |
18 | * |
19 | * @file |
20 | */ |
21 | |
22 | namespace MediaWiki\Skins\Vector\FeatureManagement\Requirements; |
23 | |
24 | use MediaWiki\Request\WebRequest; |
25 | use MediaWiki\Skins\Vector\FeatureManagement\Requirement; |
26 | use MediaWiki\Title\Title; |
27 | use MediaWiki\User\Options\UserOptionsLookup; |
28 | use MediaWiki\User\UserIdentity; |
29 | |
30 | /** |
31 | * @package MediaWiki\Skins\Vector\FeatureManagement\Requirements |
32 | */ |
33 | final class UserPreferenceRequirement implements Requirement { |
34 | |
35 | private UserIdentity $user; |
36 | |
37 | private UserOptionsLookup $userOptionsLookup; |
38 | |
39 | private string $optionName; |
40 | |
41 | private string $requirementName; |
42 | |
43 | private ?Title $title; |
44 | |
45 | private OverrideableRequirementHelper $helper; |
46 | |
47 | /** |
48 | * This constructor accepts all dependencies needed to determine whether |
49 | * the overridable config is enabled for the current user and request. |
50 | * |
51 | * @param UserIdentity $user |
52 | * @param UserOptionsLookup $userOptionsLookup |
53 | * @param string $optionName The name of the user preference. |
54 | * @param string $requirementName The name of the requirement presented to FeatureManager. |
55 | * @param WebRequest $request |
56 | * @param Title|null $title |
57 | */ |
58 | public function __construct( |
59 | UserIdentity $user, |
60 | UserOptionsLookup $userOptionsLookup, |
61 | string $optionName, |
62 | string $requirementName, |
63 | WebRequest $request, |
64 | Title $title = null |
65 | ) { |
66 | $this->user = $user; |
67 | $this->userOptionsLookup = $userOptionsLookup; |
68 | $this->optionName = $optionName; |
69 | $this->requirementName = $requirementName; |
70 | $this->title = $title; |
71 | $this->helper = new OverrideableRequirementHelper( $request, $requirementName ); |
72 | } |
73 | |
74 | /** |
75 | * @inheritDoc |
76 | */ |
77 | public function getName(): string { |
78 | return $this->requirementName; |
79 | } |
80 | |
81 | /** |
82 | * Checks whether the user preference is enabled or not. Returns true if |
83 | * enabled AND title is not null. |
84 | * |
85 | * @internal |
86 | * |
87 | * @return bool |
88 | */ |
89 | public function isPreferenceEnabled(): bool { |
90 | $user = $this->user; |
91 | $userOptionsLookup = $this->userOptionsLookup; |
92 | $optionValue = $userOptionsLookup->getOption( |
93 | $user, |
94 | $this->optionName |
95 | ); |
96 | // Check for 0, '0' or 'disabled'. |
97 | // Any other value will be handled as enabled. |
98 | $isEnabled = $optionValue && $optionValue !== 'disabled'; |
99 | |
100 | return $this->title && $isEnabled; |
101 | } |
102 | |
103 | /** |
104 | * @inheritDoc |
105 | */ |
106 | public function isMet(): bool { |
107 | $override = $this->helper->isMet(); |
108 | return $override ?? $this->isPreferenceEnabled(); |
109 | } |
110 | } |