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    ];
24
25    /**
26     * @param TitleFactory $titleFactory
27     */
28    public function __construct(
29        TitleFactory $titleFactory
30    ) {
31        $this->titleFactory = $titleFactory;
32    }
33
34    /**
35     * Code helper for comparing titles
36     *
37     * @param Title $configTitle
38     * @param string $otherConfigPage
39     * @return bool
40     */
41    private function titleEquals( Title $configTitle, string $otherConfigPage ): bool {
42        $varTitle = $this->titleFactory
43            ->makeTitleSafe( NS_MEDIAWIKI, $otherConfigPage );
44        return $varTitle !== null && $configTitle->equals( $varTitle );
45    }
46
47    /**
48     * Return list of supported config pages
49     *
50     * @return Title[]
51     */
52    public function getSupportedConfigPages(): array {
53        return array_filter(
54            array_map(
55                function ( string $var ) {
56                    return $this->titleFactory->makeTitleSafe(
57                        NS_MEDIAWIKI,
58                        $var
59                    );
60                },
61                array_keys( self::CONFIG_VALIDATOR_MAP )
62            )
63        );
64    }
65
66    /**
67     * Construct given validator
68     *
69     * @param string $class A ::class constant from one of the validators
70     * @return IConfigValidator
71     * @throws InvalidArgumentException when passed class is not supported; this should never
72     * happen in practice.
73     */
74    private function constructValidator( string $class ): IConfigValidator {
75        switch ( $class ) {
76            case AutoModeratorConfigValidation::class:
77                return new AutoModeratorConfigValidation();
78            case NoValidationValidator::class:
79                return new NoValidationValidator();
80            default:
81                throw new InvalidArgumentException( 'Unsupported config class' );
82        }
83    }
84
85    /**
86     * Generate a validator for a config page
87     *
88     * @param LinkTarget $configPage
89     * @return IConfigValidator
90     * @throws InvalidArgumentException when passed config page is not recognized; this should
91     * never happen in practice.
92     */
93    public function newConfigValidator( LinkTarget $configPage ): IConfigValidator {
94        $title = $this->titleFactory->newFromLinkTarget( $configPage );
95
96        foreach ( self::CONFIG_VALIDATOR_MAP as $var => $validatorClass ) {
97            if ( $this->titleEquals( $title, $var ) ) {
98                return $this->constructValidator( $validatorClass );
99            }
100        }
101
102        throw new InvalidArgumentException( 'Unsupported config page' );
103    }
104}