Total Complexity | 9 |
Total Lines | 74 |
Duplicated Lines | 0 % |
Coverage | 71.43% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
7 | class IpDbReader extends Reader |
||
8 | { |
||
9 | /** |
||
10 | * @var false|resource |
||
11 | */ |
||
12 | private $file; |
||
13 | |||
14 | /** |
||
15 | * @var string|null |
||
16 | */ |
||
17 | private $database; |
||
18 | |||
19 | /** |
||
20 | * Reader constructor. |
||
21 | * |
||
22 | * @param $database |
||
23 | * |
||
24 | * @throws \Exception |
||
25 | */ |
||
26 | 4 | public function __construct($database) |
|
27 | { |
||
28 | 4 | if (!is_readable($database)) { |
|
29 | $message = "The IP Database file \"{$database}\" does not exist or is not readable."; |
||
30 | |||
31 | throw new DbFileException($message); |
||
32 | } |
||
33 | |||
34 | 4 | $this->file = @fopen($database, 'rb'); |
|
35 | 4 | if (!is_resource($this->file)) { |
|
36 | throw new DbFileException("IP Database File opening \"{$database}\"."); |
||
37 | } |
||
38 | |||
39 | 4 | $this->database = $database; |
|
40 | |||
41 | 4 | $this->init(); |
|
42 | 4 | } |
|
43 | |||
44 | /** |
||
45 | * @throws \Exception |
||
46 | * |
||
47 | * @return PHPReader |
||
48 | */ |
||
49 | public function getPHPReader() |
||
50 | { |
||
51 | return new PHPReader($this->read(0, $this->fileSize)); |
||
|
|||
52 | } |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | 4 | protected function computeFileSize() |
|
58 | { |
||
59 | 4 | return @filesize($this->database); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | 4 | protected function read($offset, $length) |
|
66 | { |
||
67 | 4 | if (0 !== fseek($this->file, $offset)) { |
|
68 | return false; |
||
69 | } |
||
70 | |||
71 | 4 | return fread($this->file, $length); |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 4 | public function close() |
|
81 | } |
||
82 | 4 | } |
|
83 | } |
||
84 |