Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ConfigValidatorFactory | |
0.00% |
0 / 23 |
|
0.00% |
0 / 5 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| titleEquals | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getSupportedConfigPages | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
| constructValidator | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
20 | |||
| newConfigValidator | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace AutoModerator\Config\Validation; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | use MediaWiki\Linker\LinkTarget; |
| 7 | use MediaWiki\Title\Title; |
| 8 | use MediaWiki\Title\TitleFactory; |
| 9 | |
| 10 | class ConfigValidatorFactory { |
| 11 | /** |
| 12 | * @var string[] |
| 13 | * |
| 14 | * Maps variable to validator class. |
| 15 | * |
| 16 | * @note When adding a mapping, add an entry to ConfigValidatorFactory::constructValidator |
| 17 | * as well. |
| 18 | */ |
| 19 | private const CONFIG_VALIDATOR_MAP = [ |
| 20 | 'AutoModeratorConfig.json' => AutoModeratorConfigValidation::class, |
| 21 | 'AutoModeratorMultilingualConfig.json' => AutoModeratorConfigValidation::class |
| 22 | ]; |
| 23 | |
| 24 | public function __construct( |
| 25 | private readonly TitleFactory $titleFactory, |
| 26 | ) { |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Code helper for comparing titles |
| 31 | * |
| 32 | * @param Title $configTitle |
| 33 | * @param string $otherConfigPage |
| 34 | * @return bool |
| 35 | */ |
| 36 | private function titleEquals( Title $configTitle, string $otherConfigPage ): bool { |
| 37 | $varTitle = $this->titleFactory |
| 38 | ->makeTitleSafe( NS_MEDIAWIKI, $otherConfigPage ); |
| 39 | return $varTitle !== null && $configTitle->equals( $varTitle ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Return list of supported config pages |
| 44 | * |
| 45 | * @return Title[] |
| 46 | */ |
| 47 | public function getSupportedConfigPages(): array { |
| 48 | return array_filter( |
| 49 | array_map( |
| 50 | function ( string $var ) { |
| 51 | return $this->titleFactory->makeTitleSafe( |
| 52 | NS_MEDIAWIKI, |
| 53 | $var |
| 54 | ); |
| 55 | }, |
| 56 | array_keys( self::CONFIG_VALIDATOR_MAP ) |
| 57 | ) |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Construct given validator |
| 63 | * |
| 64 | * @param string $class A ::class constant from one of the validators |
| 65 | * @return IConfigValidator |
| 66 | * @throws InvalidArgumentException when passed class is not supported; this should never |
| 67 | * happen in practice. |
| 68 | */ |
| 69 | private function constructValidator( string $class ): IConfigValidator { |
| 70 | switch ( $class ) { |
| 71 | case AutoModeratorConfigValidation::class: |
| 72 | return new AutoModeratorConfigValidation(); |
| 73 | case NoValidationValidator::class: |
| 74 | return new NoValidationValidator(); |
| 75 | default: |
| 76 | throw new InvalidArgumentException( 'Unsupported config class' ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Generate a validator for a config page |
| 82 | * |
| 83 | * @param LinkTarget $configPage |
| 84 | * @return IConfigValidator |
| 85 | * @throws InvalidArgumentException when passed config page is not recognized; this should |
| 86 | * never happen in practice. |
| 87 | */ |
| 88 | public function newConfigValidator( LinkTarget $configPage ): IConfigValidator { |
| 89 | $title = $this->titleFactory->newFromLinkTarget( $configPage ); |
| 90 | |
| 91 | foreach ( self::CONFIG_VALIDATOR_MAP as $var => $validatorClass ) { |
| 92 | if ( $this->titleEquals( $title, $var ) ) { |
| 93 | return $this->constructValidator( $validatorClass ); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | throw new InvalidArgumentException( 'Unsupported config page' ); |
| 98 | } |
| 99 | } |