Completed
Pull Request — master (#46)
by Sam
03:20
created

UserCreator::create()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.7717

Importance

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