Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
MediaModerationMetricsFactory
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 newMetric
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3namespace MediaWiki\Extension\MediaModeration\PeriodicMetrics;
4
5use InvalidArgumentException;
6use Wikimedia\Rdbms\IReadableDatabase;
7
8/**
9 * Allows the construction of IMetric classes.
10 * Copy, with some modification, from GrowthExperiments
11 * includes/PeriodicMetrics/MediaModerationMetricsFactory.php
12 */
13class MediaModerationMetricsFactory {
14
15    private IReadableDatabase $dbr;
16
17    /** @var string[] */
18    public const METRICS = [
19        TotalTableCountMetric::class,
20        ScannedImagesMetric::class,
21        UnscannedImagesMetric::class,
22        UnscannedImagesWithLastCheckedDefinedMetric::class,
23    ];
24
25    /**
26     * @param IReadableDatabase $dbr
27     */
28    public function __construct( IReadableDatabase $dbr ) {
29        $this->dbr = $dbr;
30    }
31
32    /**
33     * Returns an instance of the class that extends IMetric given
34     * in $className.
35     *
36     * @param string $className
37     * @return IMetric
38     * @throws InvalidArgumentException if metric class name is not supported
39     */
40    public function newMetric( string $className ): IMetric {
41        switch ( $className ) {
42            case UnscannedImagesMetric::class:
43                return new UnscannedImagesMetric( $this->dbr );
44            case ScannedImagesMetric::class:
45                return new ScannedImagesMetric( $this->dbr );
46            case TotalTableCountMetric::class:
47                return new TotalTableCountMetric( $this->dbr );
48            case UnscannedImagesWithLastCheckedDefinedMetric::class:
49                return new UnscannedImagesWithLastCheckedDefinedMetric( $this->dbr );
50            default:
51                throw new InvalidArgumentException( 'Unsupported metric class name' );
52        }
53    }
54}