Passed
Push — 4.x ( c37faa...bd875a )
by Doug
12:53
created

Repository::clearCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 11
rs 9.9666
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord\EPSG;
10
11
use SQLite3;
12
13
/**
14
 * @internal
15
 */
16
class Repository
17
{
18
    /** @var SQLite3 */
19
    private static $connection;
20
21
    /** @var array */
22
    private static $unitsOfMeasureData = [];
23
24
    /** @var array */
25
    private static $primeMeridianData = [];
26
27
    /** @var array */
28
    private static $ellipsoidData = [];
29
30
    /** @var array */
31
    private static $datumData = [];
32
33
    /** @var array */
34
    private static $datumEnsembleData = [];
35
36
    /** @var array */
37
    private static $coordinateSystemData = [];
38
39
    /** @var array */
40
    private static $coordinateSystemAxisData = [];
41
42
    /** @var array */
43
    private static $coordinateReferenceSystemData = [];
44
45
    private function getConnection(): SQLite3
46
    {
47
        if (!static::$connection instanceof SQLite3) {
0 ignored issues
show
Bug introduced by
Since $connection is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $connection to at least protected.
Loading history...
introduced by
static::connection is always a sub-type of SQLite3.
Loading history...
48
            static::$connection = new SQLite3(__DIR__ . '/../../resources/epsg/epsg.sqlite', SQLITE3_OPEN_READONLY);
49 638
            static::$connection->enableExceptions(true);
50
        }
51 638
52
        return static::$connection;
53
    }
54
55
    public function getEllipsoids(): array
56
    {
57
        if (!static::$ellipsoidData) {
0 ignored issues
show
Bug introduced by
Since $ellipsoidData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $ellipsoidData to at least protected.
Loading history...
Bug Best Practice introduced by
The expression static::ellipsoidData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
58
            $connection = $this->getConnection();
59
            $sql = '
60
            SELECT
61
                el.ellipsoid_code,
62
                el.ellipsoid_name,
63
                el.semi_major_axis,
64
                el.semi_minor_axis,
65
                el.inv_flattening,
66
                el.uom_code,
67
                el.deprecated
68
            FROM epsg_ellipsoid el
69
        ';
70
71
            $result = $connection->query($sql);
72
73
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
74
                // some ellipsoids are defined via inverse flattening and the DB doesn't store the calculated data...
75
                if (!$row['semi_minor_axis']) {
76
                    $row['semi_minor_axis'] = $row['semi_major_axis'] - ($row['semi_major_axis'] / $row['inv_flattening']);
77
                }
78
79
                static::$ellipsoidData[$row['ellipsoid_code']] = $row;
80 638
            }
81
        }
82
83 796
        return static::$ellipsoidData;
84
    }
85 796
86
    public function getDatums(): array
87
    {
88
        if (!static::$datumData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::datumData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug introduced by
Since $datumData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $datumData to at least protected.
Loading history...
89
            $connection = $this->getConnection();
90
            $sql = "
91
            SELECT
92
                d.datum_code,
93
                d.datum_name,
94
                d.datum_type,
95
                d.ellipsoid_code,
96
                d.prime_meridian_code,
97
                d.conventional_rs_code,
98
                d.frame_reference_epoch,
99
                d.deprecated
100
            FROM epsg_datum d
101
            WHERE d.datum_type != 'engineering'
102
        ";
103
104
            $result = $connection->query($sql);
105
106
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
107
                static::$datumData[$row['datum_code']] = $row;
108
            }
109
        }
110
111
        return static::$datumData;
112 796
    }
113
114
    public function getDatumEnsembles(): array
115 760
    {
116
        if (!static::$datumEnsembleData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::datumEnsembleData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug introduced by
Since $datumEnsembleData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $datumEnsembleData to at least protected.
Loading history...
117 760
            $connection = $this->getConnection();
118
            $sql = '
119
            SELECT
120
                d.datum_ensemble_code,
121
                d.datum_code,
122
                d.datum_sequence
123
            FROM epsg_datumensemblemember d
124
            ORDER BY d.datum_sequence
125
            ';
126
127
            $result = $connection->query($sql);
128
129
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
130
                if (isset(static::$datumEnsembleData[$row['datum_ensemble_code']])) {
131
                    static::$datumEnsembleData[$row['datum_ensemble_code']][] = $row['datum_code'];
132
                } else {
133
                    static::$datumEnsembleData[$row['datum_ensemble_code']] = [$row['datum_code']];
134
                }
135
            }
136
        }
137
138
        return static::$datumEnsembleData;
139
    }
140
141
    public function getUnitsOfMeasure(): array
142 760
    {
143
        if (!static::$unitsOfMeasureData) {
0 ignored issues
show
Bug introduced by
Since $unitsOfMeasureData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $unitsOfMeasureData to at least protected.
Loading history...
Bug Best Practice introduced by
The expression static::unitsOfMeasureData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
144
            $connection = $this->getConnection();
145 598
            $sql = '
146
            SELECT
147 598
                m.uom_code,
148
                m.unit_of_meas_name,
149
                m.unit_of_meas_type,
150
                m.target_uom_code,
151
                m.factor_b,
152
                m.factor_c,
153
                m.deprecated
154
            FROM epsg_unitofmeasure m
155
            ';
156
157
            $result = $connection->query($sql);
158
159
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
160
                static::$unitsOfMeasureData[$row['uom_code']] = $row;
161
            }
162
        }
163
164
        return static::$unitsOfMeasureData;
165
    }
166
167
    public function getPrimeMeridians(): array
168
    {
169
        if (!static::$primeMeridianData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::primeMeridianData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug introduced by
Since $primeMeridianData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $primeMeridianData to at least protected.
Loading history...
170 598
            $connection = $this->getConnection();
171
            $sql = '
172
            SELECT
173
                p.prime_meridian_code,
174
                p.prime_meridian_name,
175
                p.greenwich_longitude,
176
                p.uom_code,
177
                p.deprecated
178
            FROM epsg_primemeridian p
179
            ';
180
181
            $result = $connection->query($sql);
182
183
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
184
                static::$primeMeridianData[$row['prime_meridian_code']] = $row;
185
            }
186
        }
187
188
        return static::$primeMeridianData;
189
    }
190
191
    public function getCoordinateSystems(): array
192
    {
193
        if (!static::$coordinateSystemData) {
0 ignored issues
show
Bug introduced by
Since $coordinateSystemData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $coordinateSystemData to at least protected.
Loading history...
Bug Best Practice introduced by
The expression static::coordinateSystemData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
194
            $connection = $this->getConnection();
195
            $sql = "
196
            SELECT
197
                cs.coord_sys_code,
198
                cs.coord_sys_name,
199
                cs.coord_sys_type,
200
                cs.dimension,
201
                cs.deprecated
202
            FROM epsg_coordinatesystem cs
203
            JOIN epsg_coordinatereferencesystem crs ON crs.coord_sys_code = cs.coord_sys_code AND crs.coord_ref_sys_kind NOT IN ('engineering', 'derived') AND crs.coord_ref_sys_name NOT LIKE '%example%'
204
            WHERE cs.coord_sys_type != 'ordinal'
205
            ";
206
207
            $result = $connection->query($sql);
208
209
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
210
                $row['axes'] = $this->getCoordinateSystemAxes()[$row['coord_sys_code']];
211
                static::$coordinateSystemData[$row['coord_sys_code']] = $row;
212
            }
213
        }
214
215
        return static::$coordinateSystemData;
216
    }
217
218
    public function getCoordinateReferenceSystems(): array
219
    {
220
        if (!static::$coordinateReferenceSystemData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::coordinateReferenceSystemData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug introduced by
Since $coordinateReferenceSystemData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $coordinateReferenceSystemData to at least protected.
Loading history...
221
            $connection = $this->getConnection();
222
            $sql = "
223
            SELECT
224
                crs.coord_ref_sys_code,
225
                crs.coord_ref_sys_kind,
226
                crs.coord_ref_sys_name,
227
                crs.coord_sys_code,
228
                crs.datum_code,
229
                crs.base_crs_code,
230
                crs.projection_conv_code,
231
                crs.cmpd_horizcrs_code,
232
                crs.cmpd_vertcrs_code,
233
                crs.deprecated
234
            FROM epsg_coordinatereferencesystem crs
235
            WHERE crs.coord_ref_sys_kind NOT IN ('engineering', 'derived') AND crs.coord_ref_sys_name NOT LIKE '%example%'
236
            AND (crs.cmpd_horizcrs_code IS NULL OR crs.cmpd_horizcrs_code NOT IN (SELECT coord_ref_sys_code FROM epsg_coordinatereferencesystem WHERE coord_ref_sys_kind = 'engineering'))
237
            AND (crs.cmpd_vertcrs_code IS NULL OR crs.cmpd_vertcrs_code NOT IN (SELECT coord_ref_sys_code FROM epsg_coordinatereferencesystem WHERE coord_ref_sys_kind = 'engineering'))
238
            ";
239
240
            $result = $connection->query($sql);
241
242
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
243
                static::$coordinateReferenceSystemData[$row['coord_ref_sys_code']] = $row;
244
            }
245
        }
246
247
        return static::$coordinateReferenceSystemData;
248
    }
249
250
    private function getCoordinateSystemAxes(): array
251
    {
252
        if (!static::$coordinateSystemAxisData) {
0 ignored issues
show
Bug introduced by
Since $coordinateSystemAxisData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $coordinateSystemAxisData to at least protected.
Loading history...
Bug Best Practice introduced by
The expression static::coordinateSystemAxisData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
253
            $connection = $this->getConnection();
254
            $sql = '
255
            SELECT
256
                a.coord_sys_code,
257
                a.coord_axis_orientation,
258
                a.coord_axis_abbreviation,
259
                an.coord_axis_name,
260
                a.uom_code,
261
                a.coord_axis_order
262
            FROM epsg_coordinateaxis a
263
            JOIN epsg_coordinateaxisname an on a.coord_axis_name_code = an.coord_axis_name_code
264
            ORDER BY a.coord_axis_order
265
            ';
266
267
            $result = $connection->query($sql);
268
269
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
270
                if (isset(static::$coordinateSystemAxisData[$row['coord_sys_code']])) {
271
                    static::$coordinateSystemAxisData[$row['coord_sys_code']][] = $row;
272
                } else {
273
                    static::$coordinateSystemAxisData[$row['coord_sys_code']] = [$row];
274
                }
275
            }
276
        }
277
278
        return static::$coordinateSystemAxisData;
279
    }
280
281
    /**
282
     * @codeCoverageIgnore
283
     */
284
    public function clearCache(): void
285
    {
286
        static::$connection = null;
0 ignored issues
show
Bug introduced by
Since $connection is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $connection to at least protected.
Loading history...
287
        static::$unitsOfMeasureData = [];
0 ignored issues
show
Bug introduced by
Since $unitsOfMeasureData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $unitsOfMeasureData to at least protected.
Loading history...
288
        static::$primeMeridianData = [];
0 ignored issues
show
Bug introduced by
Since $primeMeridianData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $primeMeridianData to at least protected.
Loading history...
289
        static::$ellipsoidData = [];
0 ignored issues
show
Bug introduced by
Since $ellipsoidData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $ellipsoidData to at least protected.
Loading history...
290
        static::$datumData = [];
0 ignored issues
show
Bug introduced by
Since $datumData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $datumData to at least protected.
Loading history...
291
        static::$datumEnsembleData = [];
0 ignored issues
show
Bug introduced by
Since $datumEnsembleData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $datumEnsembleData to at least protected.
Loading history...
292
        static::$coordinateSystemData = [];
0 ignored issues
show
Bug introduced by
Since $coordinateSystemData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $coordinateSystemData to at least protected.
Loading history...
293
        static::$coordinateSystemAxisData = [];
0 ignored issues
show
Bug introduced by
Since $coordinateSystemAxisData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $coordinateSystemAxisData to at least protected.
Loading history...
294
        static::$coordinateReferenceSystemData = [];
0 ignored issues
show
Bug introduced by
Since $coordinateReferenceSystemData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $coordinateReferenceSystemData to at least protected.
Loading history...
295
    }
296
}
297