| Total Complexity | 10 |
| Total Lines | 42 |
| Duplicated Lines | 0 % |
| Coverage | 91.3% |
| Changes | 0 | ||
| 1 | <?php |
||
| 7 | class FileReader implements ReaderInterface |
||
| 8 | { |
||
| 9 | /** @var resource|null */ |
||
| 10 | private $file; |
||
| 11 | |||
| 12 | 2 | public function __destruct() |
|
| 15 | 2 | } |
|
| 16 | |||
| 17 | 4 | public function open(string $source) |
|
| 18 | { |
||
| 19 | 4 | $file = fopen($source, 'r'); |
|
| 20 | 4 | if (! is_resource($file)) { |
|
| 21 | throw new \RuntimeException('Cannot create a reader from the file'); |
||
| 22 | } |
||
| 23 | 4 | $this->file = $file; |
|
| 24 | 4 | } |
|
| 25 | |||
| 26 | 4 | public function readLine() |
|
| 27 | { |
||
| 28 | 4 | if (! is_resource($this->file)) { |
|
| 29 | 1 | throw new \RuntimeException('File is not open'); |
|
| 30 | } |
||
| 31 | 3 | if (feof($this->file)) { |
|
| 32 | return false; |
||
| 33 | } |
||
| 34 | 3 | $line = fgets($this->file); |
|
| 35 | 3 | return (false !== $line) ? rtrim($line, PHP_EOL) : false; |
|
| 36 | } |
||
| 37 | |||
| 38 | 5 | public function close() |
|
| 39 | { |
||
| 40 | 5 | if (is_resource($this->file)) { |
|
| 41 | 4 | fclose($this->file); |
|
| 42 | } |
||
| 43 | 5 | $this->file = null; |
|
| 44 | 5 | } |
|
| 45 | |||
| 46 | 2 | public function isOpen(): bool |
|
| 49 | } |
||
| 50 | } |
||
| 51 |