Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| UserMode | |
100.00% |
22 / 22 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
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 | public function __construct( |
| 27 | private readonly Manager $amc, |
| 28 | private readonly UserIdentity $userIdentity, |
| 29 | private readonly UserOptionsManager $userOptionsManager, |
| 30 | ) { |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @return string |
| 35 | */ |
| 36 | public function getModeIdentifier() { |
| 37 | return $this->amc->getModeIdentifier(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Return information if the AMC mode is enabled by user |
| 42 | * @return bool |
| 43 | */ |
| 44 | public function isEnabled() { |
| 45 | $userOption = $this->userOptionsManager->getOption( |
| 46 | $this->userIdentity, |
| 47 | self::USER_OPTION_MODE_AMC, |
| 48 | self::OPTION_DISABLED |
| 49 | ); |
| 50 | return $this->amc->isAvailable() && |
| 51 | $userOption === self::OPTION_ENABLED; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Set Advanced Mobile Contributions mode to enabled or disabled. |
| 56 | * |
| 57 | * WARNING: Does not persist the updated user preference to the database. |
| 58 | * The caller must handle this by calling User::saveSettings() after all |
| 59 | * preference updates associated with this web request are made. |
| 60 | * @param bool $isEnabled |
| 61 | * @throws RuntimeException when mode is disabled |
| 62 | */ |
| 63 | public function setEnabled( bool $isEnabled ) { |
| 64 | if ( !$this->amc->isAvailable() ) { |
| 65 | throw new RuntimeException( 'AMC Mode is not available' ); |
| 66 | } |
| 67 | $this->userOptionsManager->setOption( |
| 68 | $this->userIdentity, |
| 69 | self::USER_OPTION_MODE_AMC, |
| 70 | $isEnabled ? self::OPTION_ENABLED : self::OPTION_DISABLED |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Create UserMode for given user |
| 76 | * NamedConstructor used by hooks system |
| 77 | * |
| 78 | * @param UserIdentity $userIdentity |
| 79 | * @return self |
| 80 | */ |
| 81 | public static function newForUser( UserIdentity $userIdentity ) { |
| 82 | $services = MediaWikiServices::getInstance(); |
| 83 | return new self( |
| 84 | $services->getService( 'MobileFrontend.AMC.Manager' ), |
| 85 | $userIdentity, |
| 86 | $services->getUserOptionsManager() |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | } |