Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ButtonAuthenticationRequest
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
4 / 4
17
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getUniqueId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFieldInfo
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getRequestByName
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 __set_state
n/a
0 / 0
n/a
0 / 0
10
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Auth
20 */
21
22namespace MediaWiki\Auth;
23
24use MediaWiki\Language\RawMessage;
25use MediaWiki\Message\Message;
26
27/**
28 * This is an authentication request that just implements a simple button.
29 * @stable to extend
30 * @ingroup Auth
31 * @since 1.27
32 */
33class ButtonAuthenticationRequest extends AuthenticationRequest {
34    /** @var string */
35    protected $name;
36
37    /** @var Message */
38    protected $label;
39
40    /** @var Message */
41    protected $help;
42
43    /**
44     * @stable to call
45     * @param string $name Button name
46     * @param Message $label Button label
47     * @param Message $help Button help
48     * @param bool $required The button is required for authentication to proceed.
49     */
50    public function __construct( $name, Message $label, Message $help, $required = false ) {
51        $this->name = $name;
52        $this->label = $label;
53        $this->help = $help;
54        $this->required = $required ? self::REQUIRED : self::OPTIONAL;
55    }
56
57    /**
58     * @inheritDoc
59     * @stable to override
60     */
61    public function getUniqueId() {
62        return parent::getUniqueId() . ':' . $this->name;
63    }
64
65    /**
66     * @inheritDoc
67     * @stable to override
68     */
69    public function getFieldInfo() {
70        return [
71            $this->name => [
72                'type' => 'button',
73                'label' => $this->label,
74                'help' => $this->help,
75            ]
76        ];
77    }
78
79    /**
80     * Fetch a ButtonAuthenticationRequest or subclass by name
81     * @param AuthenticationRequest[] $reqs Requests to search
82     * @param string $name Name to look for
83     * @return ButtonAuthenticationRequest|null Returns null if there is not
84     *  exactly one matching request.
85     */
86    public static function getRequestByName( array $reqs, $name ) {
87        $requests = array_filter( $reqs, static function ( $req ) use ( $name ) {
88            return $req instanceof ButtonAuthenticationRequest && $req->name === $name;
89        } );
90        // @phan-suppress-next-line PhanTypeMismatchReturnSuperType False positive
91        return count( $requests ) === 1 ? reset( $requests ) : null;
92    }
93
94    /**
95     * @codeCoverageIgnore
96     * @stable to override
97     * @param array $data
98     * @return AuthenticationRequest|static
99     */
100    public static function __set_state( $data ) {
101        if ( !isset( $data['label'] ) ) {
102            $data['label'] = new RawMessage( '$1', $data['name'] );
103        } elseif ( is_string( $data['label'] ) ) {
104            $data['label'] = new Message( $data['label'] );
105        } elseif ( is_array( $data['label'] ) && $data['label'] ) {
106            // @phan-suppress-next-line PhanParamTooFewUnpack Should infer non-emptiness from above
107            $data['label'] = Message::newFromKey( ...$data['label'] );
108        }
109        if ( !isset( $data['help'] ) ) {
110            $data['help'] = new RawMessage( '$1', $data['name'] );
111        } elseif ( is_string( $data['help'] ) ) {
112            $data['help'] = new Message( $data['help'] );
113        } elseif ( is_array( $data['help'] ) && $data['help'] ) {
114            // @phan-suppress-next-line PhanParamTooFewUnpack Should infer non-emptiness from above
115            $data['help'] = Message::newFromKey( ...$data['help'] );
116        }
117        $ret = new static( $data['name'], $data['label'], $data['help'] );
118        foreach ( $data as $k => $v ) {
119            $ret->$k = $v;
120        }
121        return $ret;
122    }
123}