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

UserCreator::create()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
c 0
b 0
f 0
rs 8.1315
cc 8
nc 13
nop 3
1
<?php
2
3
namespace Addwiki\Mediawiki\Api\Service;
4
5
use Addwiki\Mediawiki\Api\Client\SimpleRequest;
6
use Addwiki\Mediawiki\Api\Client\UsageException;
7
use InvalidArgumentException;
8
9
/**
10
 * @access private
11
 *
12
 * @author Addshore
13
 */
14
class UserCreator extends Service {
15
16
	/**
17
	 *
18
	 * @return bool|void
19
	 */
20
	public function create( string $username, string $password, ?string $email = null ) {
21
		if ( !is_string( $username ) ) {
22
			throw new InvalidArgumentException( '$username should be a string' );
23
		}
24
		if ( !is_string( $password ) ) {
25
			throw new InvalidArgumentException( '$password should be a string' );
26
		}
27
		if ( !is_string( $email ) && $email !== null ) {
28
			throw new InvalidArgumentException( '$email should be a string or null' );
29
		}
30
31
		$params = [
32
			'createreturnurl' => $this->api->getApiUrl(),
33
			'createtoken' => $this->api->getToken( 'createaccount' ),
34
			'username' => $username,
35
			'password' => $password,
36
			'retype' => $password,
37
		];
38
39
		if ( $email !== null ) {
40
			$params['email'] = $email;
41
		}
42
43
		try {
44
			$result = $this->api->postRequest( new SimpleRequest( 'createaccount', $params ) );
45
			return $result['createaccount']['status'] === 'PASS';
46
		} catch ( UsageException $usageException ) {
0 ignored issues
show
Bug introduced by
The class Addwiki\Mediawiki\Api\Client\UsageException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
47
			// If the above request failed, try again in the old way.
48
			if ( $usageException->getApiCode() === 'noname' ) {
49
				return $this->createPreOneTwentySeven( $params );
50
			}
51
			throw $usageException;
52
		}
53
	}
54
55
	/**
56
	 * Create a user in the pre 1.27 manner.
57
	 * @link https://www.mediawiki.org/wiki/API:Account_creation/pre-1.27
58
	 *
59
	 *
60
	 */
61
	protected function createPreOneTwentySeven( array $params ): bool {
62
		$newParams = [
63
			'name' => $params['username'],
64
			'password' => $params['password'],
65
		];
66
		if ( array_key_exists( 'email', $params ) ) {
67
			$newParams['email'] = $params['email'];
68
		}
69
		// First get the token.
70
		$tokenRequest = new SimpleRequest( 'createaccount', $newParams );
71
		$result = $this->api->postRequest( $tokenRequest );
72
		if ( $result['createaccount']['result'] == 'NeedToken' ) {
73
			// Then send the token to create the account.
74
			$newParams['token'] = $result['createaccount']['token'];
75
			$request = new SimpleRequest( 'createaccount', $newParams );
76
			$result = $this->api->postRequest( $request );
77
		}
78
		return ( $result['createaccount']['result'] === 'Success' );
79
	}
80
81
}
82