Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
34.62% |
18 / 52 |
|
25.00% |
2 / 8 |
CRAP | |
0.00% |
0 / 1 |
OATHAuthModuleRegistry | |
34.62% |
18 / 52 |
|
25.00% |
2 / 8 |
97.78 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
moduleExists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getModuleByKey | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
getAllModules | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getModuleId | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getModuleIds | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
4 | |||
getModuleIdsFromDatabase | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
getModules | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
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 | */ |
20 | |
21 | namespace MediaWiki\Extension\OATHAuth; |
22 | |
23 | use InvalidArgumentException; |
24 | use Wikimedia\ObjectFactory\ObjectFactory; |
25 | use Wikimedia\Rdbms\IConnectionProvider; |
26 | |
27 | class OATHAuthModuleRegistry { |
28 | |
29 | private IConnectionProvider $dbProvider; |
30 | private ObjectFactory $objectFactory; |
31 | |
32 | /** @var array */ |
33 | private array $modules; |
34 | |
35 | /** @var array|null */ |
36 | private ?array $moduleIds = null; |
37 | |
38 | public function __construct( |
39 | IConnectionProvider $dbProvider, |
40 | ObjectFactory $objectFactory, |
41 | array $modules |
42 | ) { |
43 | $this->dbProvider = $dbProvider; |
44 | $this->objectFactory = $objectFactory; |
45 | $this->modules = $modules; |
46 | } |
47 | |
48 | public function moduleExists( string $moduleKey ): bool { |
49 | return isset( $this->getModules()[$moduleKey] ); |
50 | } |
51 | |
52 | public function getModuleByKey( string $key ): IModule { |
53 | if ( !isset( $this->getModules()[$key] ) ) { |
54 | throw new InvalidArgumentException( "No such two-factor module $key" ); |
55 | } |
56 | |
57 | $data = $this->getModules()[$key]; |
58 | if ( is_string( $data ) ) { |
59 | $module = call_user_func_array( $this->getModules()[$key], [] ); |
60 | } else { |
61 | $module = $this->objectFactory->createObject( |
62 | $data, |
63 | [ 'assertClass' => IModule::class ] |
64 | ); |
65 | } |
66 | |
67 | return $module; |
68 | } |
69 | |
70 | /** |
71 | * Get all modules registered on the wiki |
72 | * |
73 | * @return IModule[] |
74 | */ |
75 | public function getAllModules(): array { |
76 | $modules = []; |
77 | foreach ( $this->getModules() as $key => $callback ) { |
78 | $modules[$key] = $this->getModuleByKey( $key ); |
79 | } |
80 | return $modules; |
81 | } |
82 | |
83 | /** |
84 | * Returns the numerical ID for the module with the specified key. |
85 | * |
86 | * @param string $key |
87 | * @return int |
88 | */ |
89 | public function getModuleId( string $key ): int { |
90 | $ids = $this->getModuleIds(); |
91 | if ( isset( $ids[$key] ) ) { |
92 | return $ids[$key]; |
93 | } |
94 | |
95 | throw new InvalidArgumentException( "Module $key does not seem to exist" ); |
96 | } |
97 | |
98 | /** |
99 | * @return array |
100 | */ |
101 | public function getModuleIds(): array { |
102 | if ( $this->moduleIds === null ) { |
103 | $this->moduleIds = $this->getModuleIdsFromDatabase( false ); |
104 | } |
105 | |
106 | $missing = array_diff( |
107 | array_keys( $this->getModules() ), |
108 | array_keys( $this->moduleIds ) |
109 | ); |
110 | |
111 | if ( $missing ) { |
112 | $insert = $this->dbProvider |
113 | ->getPrimaryDatabase( 'virtual-oathauth' ) |
114 | ->newInsertQueryBuilder() |
115 | ->insertInto( 'oathauth_types' ) |
116 | ->caller( __METHOD__ ); |
117 | |
118 | foreach ( $missing as $name ) { |
119 | $insert->row( [ 'oat_name' => $name ] ); |
120 | } |
121 | |
122 | $insert->execute(); |
123 | $this->moduleIds = $this->getModuleIdsFromDatabase( true ); |
124 | } |
125 | |
126 | return $this->moduleIds; |
127 | } |
128 | |
129 | private function getModuleIdsFromDatabase( bool $fromPrimary ): array { |
130 | $ids = []; |
131 | |
132 | if ( $fromPrimary ) { |
133 | $dbr = $this->dbProvider->getPrimaryDatabase( 'virtual-oathauth' ); |
134 | } else { |
135 | $dbr = $this->dbProvider->getReplicaDatabase( 'virtual-oathauth' ); |
136 | } |
137 | |
138 | $rows = $dbr->newSelectQueryBuilder() |
139 | ->select( [ 'oat_id', 'oat_name' ] ) |
140 | ->from( 'oathauth_types' ) |
141 | ->caller( __METHOD__ ) |
142 | ->fetchResultSet(); |
143 | |
144 | foreach ( $rows as $row ) { |
145 | $ids[$row->oat_name] = (int)$row->oat_id; |
146 | } |
147 | |
148 | return $ids; |
149 | } |
150 | |
151 | private function getModules(): array { |
152 | return $this->modules; |
153 | } |
154 | } |