Completed
Pull Request — master (#49)
by Sam
01:56
created

UserCreator::create()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 35
ccs 0
cts 25
cp 0
rs 5.3846
cc 8
eloc 21
nc 11
nop 3
crap 72
1
<?php
2
3
namespace Mediawiki\Api\Service;
4
5
use InvalidArgumentException;
6
use Mediawiki\Api\SimpleRequest;
7
8
/**
9
 * @access private
10
 *
11
 * @author Addshore
12
 */
13
class UserCreator extends Service {
14
15
	/**
16
	 * @param string $username
17
	 * @param string $password
18
	 * @param string|null $email
19
	 *
20
	 * @return bool
21
	 */
22
	public function create( $username, $password, $email = null ) {
23
		if ( !is_string( $username ) ) {
24
			throw new InvalidArgumentException( '$username should be a string' );
25
		}
26
		if ( !is_string( $password ) ) {
27
			throw new InvalidArgumentException( '$password should be a string' );
28
		}
29
		if ( !is_string( $email ) && !is_null( $email ) ) {
30
			throw new InvalidArgumentException( '$email should be a string or null' );
31
		}
32
33
		$params = [
34
			'name' => $username,
35
			'password' => $password,
36
		];
37
38
		if ( !is_null( $email ) ) {
39
			$params['email'] = $email;
40
		}
41
42
		$result = $this->api->postRequest( new SimpleRequest( 'createaccount', $params ) );
43
		if ( $result['createaccount']['result'] == 'NeedToken' ) {
44
			$result = $this->api->postRequest(
45
				new SimpleRequest(
46
					'createaccount',
47
					array_merge( [ 'token' => $result['createaccount']['token'] ], $params )
48
				)
49
			);
50
		}
51
		if ( $result['createaccount']['result'] === 'Success' ) {
52
			return true;
53
		}
54
55
		return false;
56
	}
57
58
}
59