Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
43 / 43 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| UserRequirementsConditionCheckerFactory | |
100.00% |
43 / 43 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| getUserRequirementsConditionChecker | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| getCheckerWithCustomConditions | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| getDefaultEvaluators | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\User; |
| 8 | |
| 9 | use MediaWiki\Config\ServiceOptions; |
| 10 | use MediaWiki\Context\IContextSource; |
| 11 | use MediaWiki\HookContainer\HookContainer; |
| 12 | use MediaWiki\Permissions\GroupPermissionsLookup; |
| 13 | use MediaWiki\User\Registration\UserRegistrationLookup; |
| 14 | |
| 15 | /** |
| 16 | * @since 1.45 |
| 17 | */ |
| 18 | class UserRequirementsConditionCheckerFactory { |
| 19 | |
| 20 | /** @internal For use by ServiceWiring */ |
| 21 | public const CONSTRUCTOR_OPTIONS = [ |
| 22 | ...UserRequirementsConditionChecker::CONSTRUCTOR_OPTIONS, |
| 23 | ...UserRequirementsConditionEvaluator::CONSTRUCTOR_OPTIONS |
| 24 | ]; |
| 25 | |
| 26 | /** @var UserRequirementsConditionChecker[] */ |
| 27 | private array $instances = []; |
| 28 | |
| 29 | private ServiceOptions $checkerOptions; |
| 30 | private ServiceOptions $evaluatorOptions; |
| 31 | |
| 32 | public function __construct( |
| 33 | ServiceOptions $options, |
| 34 | private readonly GroupPermissionsLookup $groupPermissionsLookup, |
| 35 | private readonly HookContainer $hookContainer, |
| 36 | private readonly UserEditTracker $userEditTracker, |
| 37 | private readonly UserRegistrationLookup $userRegistrationLookup, |
| 38 | private readonly UserFactory $userFactory, |
| 39 | private readonly IContextSource $context, |
| 40 | private readonly UserRequirementsConditionValidator $userRequirementsConditionValidator, |
| 41 | ) { |
| 42 | $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 43 | $this->checkerOptions = new ServiceOptions( |
| 44 | UserRequirementsConditionChecker::CONSTRUCTOR_OPTIONS, |
| 45 | $options |
| 46 | ); |
| 47 | $this->evaluatorOptions = new ServiceOptions( |
| 48 | UserRequirementsConditionEvaluator::CONSTRUCTOR_OPTIONS, |
| 49 | $options |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param UserGroupManager $userGroupManager |
| 55 | * @param string|false $wikiId |
| 56 | * @return UserRequirementsConditionChecker |
| 57 | */ |
| 58 | public function getUserRequirementsConditionChecker( |
| 59 | UserGroupManager $userGroupManager, |
| 60 | $wikiId = UserIdentity::LOCAL |
| 61 | ): UserRequirementsConditionChecker { |
| 62 | $key = (string)$wikiId; |
| 63 | if ( !isset( $this->instances[$key] ) ) { |
| 64 | $this->instances[$key] = new UserRequirementsConditionChecker( |
| 65 | $this->checkerOptions, |
| 66 | $this->hookContainer, |
| 67 | $this->userFactory, |
| 68 | $this->context, |
| 69 | $this->userRequirementsConditionValidator, |
| 70 | $this->getDefaultEvaluators( $userGroupManager ) |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | return $this->instances[$key]; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Creates a condition checker with custom condition evaluators. |
| 79 | * It can be useful if caller needs to check a condition in a hypothetical situation, |
| 80 | * by simulating certain values the checker operates on. |
| 81 | * |
| 82 | * The custom evaluators passed to this method are invoked before any default ones, |
| 83 | * in the same order as provided. If no custom evaluator handles the condition, it will |
| 84 | * be processed as usual. |
| 85 | * |
| 86 | * @since 1.46 |
| 87 | */ |
| 88 | public function getCheckerWithCustomConditions( |
| 89 | UserGroupManager $userGroupManager, |
| 90 | array $customEvaluators |
| 91 | ): UserRequirementsConditionChecker { |
| 92 | $evaluators = array_merge( |
| 93 | $customEvaluators, |
| 94 | $this->getDefaultEvaluators( $userGroupManager ) |
| 95 | ); |
| 96 | return new UserRequirementsConditionChecker( |
| 97 | $this->checkerOptions, |
| 98 | $this->hookContainer, |
| 99 | $this->userFactory, |
| 100 | $this->context, |
| 101 | $this->userRequirementsConditionValidator, |
| 102 | $evaluators, |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | private function getDefaultEvaluators( UserGroupManager $userGroupManager ): array { |
| 107 | return [ |
| 108 | new UserRequirementsConditionEvaluator( |
| 109 | $this->evaluatorOptions, |
| 110 | $this->groupPermissionsLookup, |
| 111 | $this->userEditTracker, |
| 112 | $this->userRegistrationLookup, |
| 113 | $this->userFactory, |
| 114 | $this->context, |
| 115 | $userGroupManager |
| 116 | ) |
| 117 | ]; |
| 118 | } |
| 119 | } |