| Total Complexity | 15 |
| Total Lines | 74 |
| Duplicated Lines | 0 % |
| Coverage | 90.63% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | class FileSource implements SourceInterface |
||
| 10 | { |
||
| 11 | /** @var string */ |
||
| 12 | protected $filename; |
||
| 13 | /** @var resource|null */ |
||
| 14 | protected $handle; |
||
| 15 | /** @var StreamSource|null */ |
||
| 16 | protected $streamSource; |
||
| 17 | |||
| 18 | 48 | public function __construct(string $filename) |
|
| 21 | 48 | } |
|
| 22 | |||
| 23 | 48 | public function __destruct() |
|
| 24 | { |
||
| 25 | 48 | if ($this->handle) { |
|
| 26 | try { |
||
| 27 | 47 | fclose($this->handle); |
|
| 28 | } catch (Throwable $e) { |
||
| 29 | // ignore |
||
| 30 | } |
||
| 31 | } |
||
| 32 | 48 | } |
|
| 33 | |||
| 34 | /** |
||
| 35 | * @return resource |
||
| 36 | * @throws SourceException |
||
| 37 | */ |
||
| 38 | 51 | protected function file() |
|
| 39 | { |
||
| 40 | 51 | if (!$this->handle) { |
|
| 41 | 51 | $exception = null; |
|
| 42 | try { |
||
| 43 | 51 | $handle = fopen($this->filename, 'r'); |
|
| 44 | 50 | if (!$handle) { |
|
|
|
|||
| 45 | $error = error_get_last(); |
||
| 46 | throw new \RuntimeException($error ? $error['message'] : 'fopen error'); |
||
| 47 | } |
||
| 48 | 50 | $this->handle = $handle; |
|
| 49 | 1 | } catch (Throwable $e) { |
|
| 50 | 1 | $this->handle = null; |
|
| 51 | 1 | $exception = $e; |
|
| 52 | } |
||
| 53 | 51 | if (!$this->handle) { |
|
| 54 | 1 | throw new SourceException("Could not open file '{$this->filename}'", 0, $exception); |
|
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | 50 | return $this->handle; |
|
| 59 | } |
||
| 60 | |||
| 61 | 51 | protected function stream() |
|
| 62 | { |
||
| 63 | 51 | if (!$this->streamSource) { |
|
| 64 | 51 | $this->streamSource = new StreamSource($this->file()); |
|
| 65 | } |
||
| 66 | |||
| 67 | 50 | return $this->streamSource; |
|
| 68 | } |
||
| 69 | |||
| 70 | 48 | public function isEof(): bool |
|
| 71 | { |
||
| 72 | 48 | return $this->stream()->isEof(); |
|
| 73 | } |
||
| 74 | |||
| 75 | 51 | public function read(int $bytes): string |
|
| 78 | } |
||
| 79 | |||
| 80 | 47 | public function rewind(): void |
|
| 83 | 47 | } |
|
| 84 | } |
||
| 85 |