Skip to content

Commit 5dad3c3

Browse files
committed
Split throwLoginUsageException method
1 parent a06afa4 commit 5dad3c3

File tree

1 file changed

+41
-55
lines changed

1 file changed

+41
-55
lines changed

src/MediawikiApi.php

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -303,16 +303,7 @@ public function isLoggedin() {
303303
*/
304304
public function login( ApiUser $apiUser ) {
305305
$this->logger->log( LogLevel::DEBUG, 'Logging in' );
306-
307-
$credentials = array(
308-
'lgname' => $apiUser->getUsername(),
309-
'lgpassword' => $apiUser->getPassword(),
310-
);
311-
312-
if( !is_null( $apiUser->getDomain() ) ) {
313-
$credentials['lgdomain'] = $apiUser->getDomain();
314-
}
315-
306+
$credentials = $this->getLoginParams( $apiUser );
316307
$result = $this->postRequest( new SimpleRequest( 'login', $credentials ) );
317308
if ( $result['login']['result'] == "NeedToken" ) {
318309
$result = $this->postRequest( new SimpleRequest( 'login', array_merge( array( 'lgtoken' => $result['login']['token'] ), $credentials) ) );
@@ -327,68 +318,63 @@ public function login( ApiUser $apiUser ) {
327318
return false;
328319
}
329320

321+
/**
322+
* @param ApiUser $apiUser
323+
*
324+
* @return string[]
325+
*/
326+
private function getLoginParams( ApiUser $apiUser ) {
327+
$params = array(
328+
'lgname' => $apiUser->getUsername(),
329+
'lgpassword' => $apiUser->getPassword(),
330+
);
331+
332+
if( !is_null( $apiUser->getDomain() ) ) {
333+
$params['lgdomain'] = $apiUser->getDomain();
334+
}
335+
return $params;
336+
}
337+
330338
/**
331339
* @param array $result
332340
*
333341
* @throws UsageException
334342
*/
335343
private function throwLoginUsageException( $result ) {
336344
$loginResult = $result['login']['result'];
345+
346+
throw new UsageException(
347+
'login-' . $loginResult,
348+
$this->getLoginExceptionMessage( $loginResult ),
349+
$result
350+
);
351+
}
352+
353+
/**
354+
* @param string $loginResult
355+
*
356+
* @return string
357+
*/
358+
private function getLoginExceptionMessage( $loginResult ) {
337359
switch( $loginResult ) {
338360
case 'Illegal';
339-
throw new UsageException(
340-
'login-' . $loginResult,
341-
'You provided an illegal username',
342-
$result
343-
);
361+
return 'You provided an illegal username';
344362
case 'NotExists';
345-
throw new UsageException(
346-
'login-' . $loginResult,
347-
'The username you provided doesn\'t exist',
348-
$result
349-
);
363+
return 'The username you provided doesn\'t exist';
350364
case 'WrongPass';
351-
throw new UsageException(
352-
'login-' . $loginResult,
353-
'The password you provided is incorrect',
354-
$result
355-
);
365+
return 'The password you provided is incorrect';
356366
case 'WrongPluginPass';
357-
throw new UsageException(
358-
'login-' . $loginResult,
359-
'An authentication plugin rather than MediaWiki itself rejected the password',
360-
$result
361-
);
367+
return 'An authentication plugin rather than MediaWiki itself rejected the password';
362368
case 'CreateBlocked';
363-
throw new UsageException(
364-
'login-' . $loginResult,
365-
'The wiki tried to automatically create a new account for you, but your IP address has been blocked from account creation',
366-
$result
367-
);
369+
return 'The wiki tried to automatically create a new account for you, but your IP address has been blocked from account creation';
368370
case 'Throttled';
369-
throw new UsageException(
370-
'login-' . $loginResult,
371-
'You\'ve logged in too many times in a short time.',
372-
$result
373-
);
371+
return 'You\'ve logged in too many times in a short time.';
374372
case 'Blocked';
375-
throw new UsageException(
376-
'login-' . $loginResult,
377-
'User is blocked',
378-
$result
379-
);
373+
return 'User is blocked';
380374
case 'NeedToken';
381-
throw new UsageException(
382-
'login-' . $loginResult,
383-
'Either you did not provide the login token or the sessionid cookie.',
384-
$result
385-
);
375+
return 'Either you did not provide the login token or the sessionid cookie.';
386376
default:
387-
throw new UsageException(
388-
'login-' . $loginResult,
389-
$loginResult,
390-
$result
391-
);
377+
return $loginResult;
392378
}
393379
}
394380

0 commit comments

Comments
 (0)