Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
ReaderFactory | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
get | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
getReader | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\IPInfo\InfoRetriever; |
4 | |
5 | use Exception; |
6 | use GeoIp2\Database\Reader; |
7 | |
8 | class ReaderFactory { |
9 | |
10 | /** @var Reader[] Map of filename to the Reader object */ |
11 | private $readers = []; |
12 | |
13 | /** |
14 | * @param string $path |
15 | * @param string $filename |
16 | * @return Reader|null null if the file path or file is invalid |
17 | */ |
18 | public function get( string $path, string $filename ): ?Reader { |
19 | if ( isset( $this->readers[$filename] ) ) { |
20 | return $this->readers[$filename]; |
21 | } |
22 | try { |
23 | $reader = $this->getReader( $path . $filename ); |
24 | } catch ( Exception $e ) { |
25 | return null; |
26 | } |
27 | |
28 | $this->readers[$filename] = $reader; |
29 | |
30 | return $this->readers[$filename]; |
31 | } |
32 | |
33 | /** |
34 | * @param string $filename |
35 | * @return Reader |
36 | * @throws Exception |
37 | */ |
38 | protected function getReader( string $filename ): Reader { |
39 | return new Reader( $filename ); |
40 | } |
41 | } |