Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
25 / 25 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
SqlModelLookup | |
100.00% |
25 / 25 |
|
100.00% |
5 / 5 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getModelId | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getModelVersion | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getModels | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getModelData | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
4 |
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 3 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 |
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
15 | */ |
16 | |
17 | namespace ORES\Storage; |
18 | |
19 | use InvalidArgumentException; |
20 | use Wikimedia\Rdbms\IConnectionProvider; |
21 | |
22 | class SqlModelLookup implements ModelLookup { |
23 | |
24 | private IConnectionProvider $dbProvider; |
25 | |
26 | /** @var array|null */ |
27 | private $modelData = null; |
28 | |
29 | public function __construct( IConnectionProvider $dbProvider ) { |
30 | $this->dbProvider = $dbProvider; |
31 | } |
32 | |
33 | /** |
34 | * @see ModelLookup::getModelId() |
35 | * @param string $model |
36 | * |
37 | * @throws InvalidArgumentException |
38 | * @return int |
39 | */ |
40 | public function getModelId( $model ) { |
41 | $modelData = $this->getModelData(); |
42 | if ( !array_key_exists( $model, $modelData ) ) { |
43 | throw new InvalidArgumentException( "No model available for [{$model}]" ); |
44 | } |
45 | |
46 | return $modelData[$model]['id']; |
47 | } |
48 | |
49 | /** |
50 | * @see ModelLookup::getModelVersion() |
51 | * @param string $model |
52 | * |
53 | * @throws InvalidArgumentException |
54 | * @return string |
55 | */ |
56 | public function getModelVersion( $model ) { |
57 | $modelData = $this->getModelData(); |
58 | if ( !array_key_exists( $model, $modelData ) ) { |
59 | throw new InvalidArgumentException( "No model available for [{$model}]" ); |
60 | } |
61 | |
62 | return $modelData[$model]['version']; |
63 | } |
64 | |
65 | /** |
66 | * @see ModelLookup::getModels() |
67 | * |
68 | * @return array[] |
69 | */ |
70 | public function getModels() { |
71 | return $this->getModelData(); |
72 | } |
73 | |
74 | /** |
75 | * @return array[] |
76 | */ |
77 | private function getModelData() { |
78 | // Don't cache when result is empty (T184938) |
79 | if ( $this->modelData === null || $this->modelData === [] ) { |
80 | $this->modelData = []; |
81 | $result = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder() |
82 | ->select( [ 'oresm_id', 'oresm_name', 'oresm_version' ] ) |
83 | ->from( 'ores_model' ) |
84 | ->where( [ 'oresm_is_current' => 1 ] ) |
85 | ->orderBy( 'oresm_name' ) |
86 | ->caller( __METHOD__ ) |
87 | ->fetchResultSet(); |
88 | foreach ( $result as $row ) { |
89 | $this->modelData[$row->oresm_name] = [ |
90 | 'id' => (int)$row->oresm_id, |
91 | 'version' => $row->oresm_version |
92 | ]; |
93 | } |
94 | } |
95 | |
96 | return $this->modelData; |
97 | } |
98 | |
99 | } |