Completed
Pull Request — master (#77)
by
unknown
01:28
created

Bigfile::iterateText()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
// inspirated by:https://dzone.com/articles/processing-large-files-using-php
4
5
namespace Spatie\DbSnapshots;
6
7
use \Exception;
8
use \SplFileObject;
9
10
class Bigfile
11
{
12
    protected $file;
13
14
    public function __construct($filename, $mode = 'r', $compressed = false)
15
    {
16
        if (! file_exists($filename)) {
17
            throw new Exception('File not found');
18
        }
19
        $this->file = new SplFileObject(($compressed ? 'compress.zlib://'.$filename : $filename), $mode);
20
    }
21
22
    public function iterateText()
23
    {
24
        $count = 0;
25
26
        while (! $this->file->eof()) {
27
            yield $this->file->fgets();
28
            $count++;
29
        }
30
31
        return $count;
32
    }
33
}
34