hughcube /
ipdb
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace HughCube\IpDb\Readers; |
||||
| 4 | |||||
| 5 | use HughCube\IpDb\Exceptions\DbFileException; |
||||
| 6 | |||||
| 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)); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 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)) { |
|||
|
0 ignored issues
–
show
It seems like
$this->file can also be of type boolean; however, parameter $handle of fseek() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 68 | return false; |
||||
| 69 | } |
||||
| 70 | |||||
| 71 | 4 | return fread($this->file, $length); |
|||
|
0 ignored issues
–
show
It seems like
$this->file can also be of type boolean; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 72 | } |
||||
| 73 | |||||
| 74 | /** |
||||
| 75 | * {@inheritdoc} |
||||
| 76 | */ |
||||
| 77 | 4 | public function close() |
|||
| 78 | { |
||||
| 79 | 4 | if (is_resource($this->file)) { |
|||
| 80 | 4 | fclose($this->file); |
|||
| 81 | } |
||||
| 82 | 4 | } |
|||
| 83 | } |
||||
| 84 |