Completed
Push — main ( 2daa48...b5d932 )
by
unknown
08:38
created

UserGetter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 45
c 0
b 0
f 0
wmc 3
lcom 0
cbo 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFromUsername() 0 13 1
A newUserFromListUsersResult() 0 23 2
1
<?php
2
3
namespace Addwiki\Mediawiki\Api\Service;
4
5
use Addwiki\Mediawiki\Api\Client\SimpleRequest;
6
use Addwiki\Mediawiki\DataModel\User;
7
8
/**
9
 * @access private
10
 *
11
 * @author Addshore
12
 */
13
class UserGetter extends Service {
14
15
	public function getFromUsername( string $username ): User {
16
		$result = $this->api->getRequest(
17
			new SimpleRequest(
18
				'query', [
19
				'list' => 'users',
20
				'ususers' => $username,
21
				'usprop' => 'gender|emailable|registration|editcount|rights|implicitgroups|groups|blockinfo',
22
			]
23
			)
24
		);
25
26
		return $this->newUserFromListUsersResult( array_shift( $result['query']['users'] ) );
27
	}
28
29
	/**
30
	 *
31
	 * @return User|void
32
	 */
33
	private function newUserFromListUsersResult( array $array ) {
34
		if ( array_key_exists( 'userid', $array ) ) {
35
			return new User(
36
				$array['name'],
37
				$array['userid'],
38
				$array['editcount'],
39
				$array['registration'],
40
				[ 'groups' => $array['groups'], 'implicitgroups' => $array['implicitgroups'] ],
41
				$array['rights'],
42
				$array['gender']
43
			);
44
		} else {
45
			return new User(
46
				$array['name'],
47
				0,
48
				0,
49
				'',
50
				[ 'groups' => [], 'implicitgroups' => [] ],
51
				[],
52
				''
53
			);
54
		}
55
	}
56
57
}
58