Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
12 / 12 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
MWOAuthException | |
100.00% |
12 / 12 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
getNormalizedMessage | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getMessageContext | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMessageObject | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\OAuth\Backend; |
4 | |
5 | use ILocalizedException; |
6 | use MediaWiki\Extension\OAuth\Lib\OAuthException; |
7 | use MediaWiki\Message\Message; |
8 | use Wikimedia\NormalizedException\INormalizedException; |
9 | |
10 | /** |
11 | * Exception class for human-readable OAuth errors. |
12 | */ |
13 | class MWOAuthException extends OAuthException implements INormalizedException, ILocalizedException { |
14 | /** @var string */ |
15 | protected $msg; |
16 | /** @var array */ |
17 | protected $params; |
18 | |
19 | /** |
20 | * Exception that may be shown to an end user. |
21 | * @param string $msg i18n message key for error text. |
22 | * @param array $params Error parameters. These double as i18n message parameters and PSR-3 |
23 | * log context. The array keys are used as log context keys; the position is used for i18n |
24 | * (the first array element is $1 etc). Array items with numeric keys are omitted from PSR-3 |
25 | * logging. |
26 | */ |
27 | public function __construct( $msg, $params = [] ) { |
28 | $this->msg = $msg; |
29 | $this->params = $params; |
30 | parent::__construct( |
31 | $this->getMessageObject()->inLanguage( 'en' )->useDatabase( false )->plain() |
32 | ); |
33 | } |
34 | |
35 | /** |
36 | * @inheritDoc |
37 | */ |
38 | public function getNormalizedMessage(): string { |
39 | $paramsWithPsr3Placeholders = array_map( static function ( $val, $key ) { |
40 | return is_numeric( $key ) ? $val : "{{$key}}"; |
41 | }, $this->params, array_keys( $this->params ) ); |
42 | return wfMessage( $this->msg, $paramsWithPsr3Placeholders )->inLanguage( 'en' ) |
43 | ->useDatabase( false )->plain(); |
44 | } |
45 | |
46 | /** |
47 | * @inheritDoc |
48 | */ |
49 | public function getMessageContext(): array { |
50 | return array_filter( $this->params, fn ( $key ) => !is_numeric( $key ), ARRAY_FILTER_USE_KEY ); |
51 | } |
52 | |
53 | /** |
54 | * @inheritDoc |
55 | */ |
56 | public function getMessageObject() { |
57 | return new Message( $this->msg, array_values( $this->params ) ); |
58 | } |
59 | |
60 | } |