Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
MetricsFactory
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 newMetric
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace GrowthExperiments\PeriodicMetrics;
4
5use GrowthExperiments\Mentorship\Provider\MentorProvider;
6use InvalidArgumentException;
7use MediaWiki\User\UserEditTracker;
8use MediaWiki\User\UserIdentityLookup;
9use Wikimedia\Rdbms\LoadBalancer;
10
11class MetricsFactory {
12
13    /** @var string[] */
14    public const METRICS = [
15        AutoAssignedMentorsMetric::class,
16        InactiveMentorsMetric::class,
17        NewcomersByMentorMetric::class,
18    ];
19
20    /** @var LoadBalancer */
21    private $loadBalancer;
22
23    /** @var UserEditTracker */
24    private $userEditTracker;
25
26    /** @var UserIdentityLookup */
27    private $userIdentityLookup;
28
29    /** @var MentorProvider */
30    private $mentorProvider;
31
32    /**
33     * @param LoadBalancer $loadBalancer
34     * @param UserEditTracker $userEditTracker
35     * @param UserIdentityLookup $userIdentityLookup
36     * @param MentorProvider $mentorProvider
37     */
38    public function __construct(
39        LoadBalancer $loadBalancer,
40        UserEditTracker $userEditTracker,
41        UserIdentityLookup $userIdentityLookup,
42        MentorProvider $mentorProvider
43    ) {
44        $this->loadBalancer = $loadBalancer;
45        $this->userEditTracker = $userEditTracker;
46        $this->userIdentityLookup = $userIdentityLookup;
47        $this->mentorProvider = $mentorProvider;
48    }
49
50    /**
51     * @param string $className
52     * @return IMetric
53     * @throws InvalidArgumentException if metric class name is not supported
54     */
55    public function newMetric( string $className ): IMetric {
56        switch ( $className ) {
57            case AutoAssignedMentorsMetric::class:
58                return new AutoAssignedMentorsMetric( $this->mentorProvider );
59            case InactiveMentorsMetric::class:
60                return new InactiveMentorsMetric(
61                    $this->userEditTracker,
62                    $this->userIdentityLookup,
63                    $this->mentorProvider
64                );
65            case NewcomersByMentorMetric::class:
66                return new NewcomersByMentorMetric(
67                    $this,
68                    $this->loadBalancer->getConnection( DB_REPLICA )
69                );
70            default:
71                throw new InvalidArgumentException( 'Unsupported metric class name' );
72        }
73    }
74}