FileReader::isOpen()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\RfcLinc\Util;
6
7
class FileReader implements ReaderInterface
8
{
9
    /** @var resource|null */
10
    private $file;
11
12 2
    public function __destruct()
13
    {
14 2
        $this->close();
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
47
    {
48 2
        return is_resource($this->file);
49
    }
50
}
51