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