mediawiki-skins-Vector/includes/FeatureManagement/Requirements/UserPreferenceRequirement.php
Doğu Abaris 348e6f6cc8 Refactor and clean up FeatureManagement classes
This commit introduces several changes to improve the structure and
readability of the FeatureManagement code in the Vector skin. Key
changes include:

- In ABRequirementTest.php, improved test method names for clarity.
- In ABRequirement.php, simplified property declarations and cleaned up
  code structure.
- In DynamicConfigRequirement.php, cleaned up property declarations.
- In LimitedWidthContentRequirement.php, cleaned up property
  declarations and added type hints.
- In LoggedInRequirement.php, cleaned up property declarations.
- In OverridableConfigRequirement.php, cleaned up property declarations.
- In OverrideableRequirementHelper.php, cleaned up property declarations.
- In SimpleRequirement.php, cleaned up property declarations.
- In UserPreferenceRequirement.php, cleaned up property declarations.

Each change aims to enhance the codebase's maintainability and
understandability. No functional changes were introduced.

Change-Id: I23283c5a6799b1f03a6a9b5832c34c7dbbcca527
2024-02-06 00:37:14 +00:00

111 lines
3 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Skins\Vector\FeatureManagement\Requirements;
use MediaWiki\Request\WebRequest;
use MediaWiki\Skins\Vector\FeatureManagement\Requirement;
use MediaWiki\Title\Title;
use MediaWiki\User\Options\UserOptionsLookup;
use MediaWiki\User\User;
/**
* @package MediaWiki\Skins\Vector\FeatureManagement\Requirements
*/
final class UserPreferenceRequirement implements Requirement {
private User $user;
private UserOptionsLookup $userOptionsLookup;
private string $optionName;
private string $requirementName;
private ?Title $title;
private OverrideableRequirementHelper $helper;
/**
* This constructor accepts all dependencies needed to determine whether
* the overridable config is enabled for the current user and request.
*
* @param User $user
* @param UserOptionsLookup $userOptionsLookup
* @param string $optionName The name of the user preference.
* @param string $requirementName The name of the requirement presented to FeatureManager.
* @param WebRequest $request
* @param Title|null $title
*/
public function __construct(
User $user,
UserOptionsLookup $userOptionsLookup,
string $optionName,
string $requirementName,
WebRequest $request,
Title $title = null
) {
$this->user = $user;
$this->userOptionsLookup = $userOptionsLookup;
$this->optionName = $optionName;
$this->requirementName = $requirementName;
$this->title = $title;
$this->helper = new OverrideableRequirementHelper( $request, $requirementName );
}
/**
* @inheritDoc
*/
public function getName(): string {
return $this->requirementName;
}
/**
* Checks whether the user preference is enabled or not. Returns true if
* enabled AND title is not null.
*
* @internal
*
* @return bool
*/
public function isPreferenceEnabled(): bool {
$user = $this->user;
$userOptionsLookup = $this->userOptionsLookup;
$optionValue = $userOptionsLookup->getOption(
$user,
$this->optionName
);
// Check for 0, '0' or 'disabled'.
// Any other value will be handled as enabled.
$isEnabled = $optionValue && $optionValue !== 'disabled';
return $this->title && $isEnabled;
}
/**
* @inheritDoc
*/
public function isMet(): bool {
$override = $this->helper->isMet();
return $override ?? $this->isPreferenceEnabled();
}
}