Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
24 / 24 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
UserMode | |
100.00% |
24 / 24 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getModeIdentifier | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isEnabled | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
setEnabled | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
newForUser | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MobileFrontend\Amc; |
4 | |
5 | use MediaWiki\MediaWikiServices; |
6 | use MediaWiki\User\Options\UserOptionsManager; |
7 | use MediaWiki\User\UserIdentity; |
8 | use MobileFrontend\Features\IUserMode; |
9 | use MobileFrontend\Features\IUserSelectableMode; |
10 | use RuntimeException; |
11 | |
12 | class UserMode implements IUserMode, IUserSelectableMode { |
13 | |
14 | public const USER_OPTION_MODE_AMC = 'mf_amc_optin'; |
15 | |
16 | /** |
17 | * Value in the user options when AMC is enabled |
18 | */ |
19 | public const OPTION_ENABLED = '1'; |
20 | |
21 | /** |
22 | * Value in the user options when AMC is disabled (default state) |
23 | */ |
24 | public const OPTION_DISABLED = '0'; |
25 | |
26 | /** |
27 | * @var UserIdentity |
28 | */ |
29 | private $userIdentity; |
30 | |
31 | /** |
32 | * @var Manager |
33 | */ |
34 | private $amc; |
35 | |
36 | /** |
37 | * @var UserOptionsManager |
38 | */ |
39 | private $userOptionsManager; |
40 | |
41 | /** |
42 | * @param Manager $amcManager |
43 | * @param UserIdentity $userIdentity |
44 | * @param UserOptionsManager $userOptionsManager |
45 | * @throws RuntimeException When AMC mode is not available |
46 | */ |
47 | public function __construct( |
48 | Manager $amcManager, |
49 | UserIdentity $userIdentity, |
50 | UserOptionsManager $userOptionsManager |
51 | ) { |
52 | $this->amc = $amcManager; |
53 | $this->userIdentity = $userIdentity; |
54 | $this->userOptionsManager = $userOptionsManager; |
55 | } |
56 | |
57 | /** |
58 | * @return string |
59 | */ |
60 | public function getModeIdentifier() { |
61 | return $this->amc->getModeIdentifier(); |
62 | } |
63 | |
64 | /** |
65 | * Return information if the AMC mode is enabled by user |
66 | * @return bool |
67 | */ |
68 | public function isEnabled() { |
69 | $userOption = $this->userOptionsManager->getOption( |
70 | $this->userIdentity, |
71 | self::USER_OPTION_MODE_AMC, |
72 | self::OPTION_DISABLED |
73 | ); |
74 | return $this->amc->isAvailable() && |
75 | $userOption === self::OPTION_ENABLED; |
76 | } |
77 | |
78 | /** |
79 | * Set Advanced Mobile Contributions mode to enabled or disabled. |
80 | * |
81 | * WARNING: Does not persist the updated user preference to the database. |
82 | * The caller must handle this by calling User::saveSettings() after all |
83 | * preference updates associated with this web request are made. |
84 | * @param bool $isEnabled |
85 | * @throws RuntimeException when mode is disabled |
86 | */ |
87 | public function setEnabled( bool $isEnabled ) { |
88 | if ( !$this->amc->isAvailable() ) { |
89 | throw new RuntimeException( 'AMC Mode is not available' ); |
90 | } |
91 | $this->userOptionsManager->setOption( |
92 | $this->userIdentity, |
93 | self::USER_OPTION_MODE_AMC, |
94 | $isEnabled ? self::OPTION_ENABLED : self::OPTION_DISABLED |
95 | ); |
96 | } |
97 | |
98 | /** |
99 | * Create UserMode for given user |
100 | * NamedConstructor used by hooks system |
101 | * |
102 | * @param UserIdentity $userIdentity |
103 | * @return self |
104 | */ |
105 | public static function newForUser( UserIdentity $userIdentity ) { |
106 | $services = MediaWikiServices::getInstance(); |
107 | return new self( |
108 | $services->getService( 'MobileFrontend.AMC.Manager' ), |
109 | $userIdentity, |
110 | $services->getUserOptionsManager() |
111 | ); |
112 | } |
113 | |
114 | } |