UserGetter::newUserFromListUsersResult()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 23
cp 0
rs 9.552
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Mediawiki\Api\Service;
4
5
use Mediawiki\Api\SimpleRequest;
6
use Mediawiki\DataModel\User;
7
8
/**
9
 * @access private
10
 *
11
 * @author Addshore
12
 */
13
class UserGetter extends Service {
14
15
	/**
16
	 * @param string $username
17
	 *
18
	 * @return User
19
	 */
20
	public function getFromUsername( $username ) {
21
		$result = $this->api->getRequest(
22
			new SimpleRequest(
23
				'query', [
24
				'list' => 'users',
25
				'ususers' => $username,
26
				'usprop' => 'gender|emailable|registration|editcount|rights|implicitgroups|groups|blockinfo',
27
			]
28
			)
29
		);
30
31
		return $this->newUserFromListUsersResult( array_shift( $result['query']['users'] ) );
32
	}
33
34
	/**
35
	 * @param array $array
36
	 *
37
	 * @return User
38
	 */
39
	private function newUserFromListUsersResult( $array ) {
40
		if ( array_key_exists( 'userid', $array ) ) {
41
			return new User(
42
				$array['name'],
43
				$array['userid'],
44
				$array['editcount'],
45
				$array['registration'],
46
				[ 'groups' => $array['groups'], 'implicitgroups' => $array['implicitgroups'] ],
47
				$array['rights'],
48
				$array['gender']
49
			);
50
		} else {
51
			return new User(
52
				$array['name'],
53
				0,
54
				0,
55
				'',
56
				[ 'groups' => [], 'implicitgroups' => [] ],
57
				[],
58
				''
59
			);
60
		}
61
	}
62
63
}
64