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