Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 12 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
UserService | |
0.00% |
0 / 12 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getGlobalUserId | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getUsernameAndGender | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace ContentTranslation\Service; |
5 | |
6 | use InvalidArgumentException; |
7 | use MediaWiki\Cache\GenderCache; |
8 | use MediaWiki\User\CentralId\CentralIdLookup; |
9 | use MediaWiki\User\UserIdentity; |
10 | |
11 | /** |
12 | * Service that provides user-related functionality. |
13 | * |
14 | * @author Nik Gkountas |
15 | * @license GPL-2.0-or-later |
16 | * @since 2023.10 |
17 | */ |
18 | class UserService { |
19 | private CentralIdLookup $centralIdLookup; |
20 | private GenderCache $genderCache; |
21 | |
22 | public function __construct( CentralIdLookup $centralIdLookup, GenderCache $genderCache ) { |
23 | $this->centralIdLookup = $centralIdLookup; |
24 | $this->genderCache = $genderCache; |
25 | } |
26 | |
27 | public function getGlobalUserId( UserIdentity $user ): int { |
28 | $id = $this->centralIdLookup->centralIdFromLocalUser( $user, CentralIdLookup::AUDIENCE_RAW ); |
29 | if ( $id === 0 ) { |
30 | throw new InvalidArgumentException( 'User account is not global' ); |
31 | } |
32 | |
33 | return $id; |
34 | } |
35 | |
36 | /** |
37 | * @param int|null $globalUserId |
38 | * @return array {name: ?string, gender: ?string} |
39 | */ |
40 | public function getUsernameAndGender( ?int $globalUserId ): array { |
41 | $userIdentity = $this->centralIdLookup->localUserFromCentralId( $globalUserId ); |
42 | $name = $gender = null; |
43 | if ( $userIdentity ) { |
44 | $name = $userIdentity->getName(); |
45 | $gender = $this->genderCache->getGenderOf( $userIdentity, __METHOD__ ); |
46 | } |
47 | |
48 | return [ 'name' => $name, 'gender' => $gender ]; |
49 | } |
50 | |
51 | } |