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