Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
CentralAuthTokenProvider
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 getToken
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace FileImporter\Remote\MediaWiki;
4
5use ApiMain;
6use ApiUsageException;
7use FauxRequest;
8use RequestContext;
9use RuntimeException;
10use User;
11
12/**
13 * @license GPL-2.0-or-later
14 */
15class CentralAuthTokenProvider {
16
17    /**
18     * @param User $user
19     *
20     * @return string
21     * @throws ApiUsageException e.g. when CentralAuth is not available locally
22     * @throws RuntimeException when there is an unexpected API result
23     */
24    public function getToken( User $user ) {
25        $context = new RequestContext;
26        $context->setRequest( new FauxRequest( [ 'action' => 'centralauthtoken' ] ) );
27        $context->setUser( $user );
28
29        $api = new ApiMain( $context );
30
31        $api->execute();
32        $token = $api->getResult()->getResultData( [ 'centralauthtoken', 'centralauthtoken' ] );
33        if ( !$token ) {
34            // This should be unreachable, because execute() takes care of all error handling
35            throw new RuntimeException( 'Unexpected return value from the centralauthtoken API' );
36        }
37
38        return $token;
39    }
40
41}