FileReader   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 42
ccs 21
cts 23
cp 0.913
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 3 1
A isOpen() 0 3 1
A close() 0 6 2
A readLine() 0 10 4
A open() 0 7 2
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