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

UserGetter::newUserFromListUsersResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
rs 9.552
cc 2
nc 2
nop 1
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