Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
25.93% covered (danger)
25.93%
7 / 27
10.00% covered (danger)
10.00%
1 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
StaticMentorManager
25.93% covered (danger)
25.93%
7 / 27
10.00% covered (danger)
10.00%
1 / 10
120.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getMentorForUserIfExists
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMentorForUser
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getMentorForUserSafe
40.00% covered (danger)
40.00%
2 / 5
0.00% covered (danger)
0.00%
0 / 1
4.94
 getEffectiveMentorForUser
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getEffectiveMentorForUserSafe
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 newMentorFromUserIdentity
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 getMentorshipStateForUser
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRandomAutoAssignedMentor
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setMentorshipStateForUser
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace GrowthExperiments\Mentorship;
4
5use GrowthExperiments\Mentorship\Store\MentorStore;
6use GrowthExperiments\WikiConfigException;
7use InvalidArgumentException;
8use MediaWiki\User\UserIdentity;
9
10/**
11 * MentorManager implementation for local testing and development.
12 * Uses a predefined user => mentor mapping.
13 */
14class StaticMentorManager extends MentorManager {
15
16    /** @var Mentor[] */
17    private array $mentors;
18
19    /** @var Mentor[] */
20    private array $backupMentors;
21
22    /**
23     * @param Mentor[] $mentors username => (primary) mentor
24     * @param Mentor[] $backupMentors username => backup mentor
25     */
26    public function __construct( array $mentors, array $backupMentors = [] ) {
27        $this->mentors = $mentors;
28        $this->backupMentors = $backupMentors;
29    }
30
31    /** @inheritDoc */
32    public function getMentorForUserIfExists(
33        UserIdentity $user,
34        string $role = MentorStore::ROLE_PRIMARY
35    ): ?Mentor {
36        return $this->getMentorForUserSafe( $user );
37    }
38
39    /** @inheritDoc */
40    public function getMentorForUser(
41        UserIdentity $user,
42        string $role = MentorStore::ROLE_PRIMARY
43    ): Mentor {
44        $mentor = $this->getMentorForUserSafe( $user );
45        if ( !$mentor ) {
46            throw new WikiConfigException( __METHOD__ . ': No mentor for {user}',
47                [ 'user' => $user->getName() ] );
48        }
49        return $mentor;
50    }
51
52    /** @inheritDoc */
53    public function getMentorForUserSafe(
54        UserIdentity $user,
55        string $role = MentorStore::ROLE_PRIMARY
56    ): ?Mentor {
57        if ( $role === MentorStore::ROLE_PRIMARY ) {
58            return $this->mentors[$user->getName()] ?? null;
59        } elseif ( $role === MentorStore::ROLE_BACKUP ) {
60            return $this->backupMentors[$user->getName()] ?? null;
61        } else {
62            throw new InvalidArgumentException( 'Invalid role' );
63        }
64    }
65
66    /** @inheritDoc */
67    public function getEffectiveMentorForUser( UserIdentity $menteeUser ): Mentor {
68        $mentor = $this->getEffectiveMentorForUserSafe( $menteeUser );
69        if ( !$mentor ) {
70            throw new WikiConfigException( __METHOD__ . ': No effective mentor for ' . $menteeUser->getName() );
71        }
72        return $mentor;
73    }
74
75    /** @inheritDoc */
76    public function getEffectiveMentorForUserSafe( UserIdentity $menteeUser ): ?Mentor {
77        return $this->getMentorForUserSafe( $menteeUser, MentorStore::ROLE_PRIMARY ) ??
78            $this->getMentorForUserSafe( $menteeUser, MentorStore::ROLE_BACKUP );
79    }
80
81    /** @inheritDoc */
82    public function newMentorFromUserIdentity(
83        UserIdentity $mentorUser,
84        ?UserIdentity $menteeUser = null
85    ): Mentor {
86        foreach ( $this->mentors as $mentee => $mentor ) {
87            if ( $mentor->getUserIdentity()->equals( $mentorUser ) ) {
88                return $mentor;
89            }
90        }
91        throw new InvalidArgumentException( 'Invalid mentor passed' );
92    }
93
94    /** @inheritDoc */
95    public function getMentorshipStateForUser( UserIdentity $user ): int {
96        return MentorManager::MENTORSHIP_ENABLED;
97    }
98
99    /** @inheritDoc */
100    public function getRandomAutoAssignedMentor(
101        UserIdentity $mentee,
102        array $excluded = []
103    ): UserIdentity {
104        $autoAssignedMentors = array_values( $this->mentors );
105        return $autoAssignedMentors[rand( 0, count( $autoAssignedMentors ) - 1 )]->getUserIdentity();
106    }
107
108    /** @inheritDoc */
109    public function setMentorshipStateForUser( UserIdentity $user, int $state ): void {
110    }
111}