Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
57.14% |
4 / 7 |
CRAP | |
68.42% |
26 / 38 |
FileReader | |
0.00% |
0 / 1 |
|
57.14% |
4 / 7 |
26.10 | |
68.42% |
26 / 38 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
rows | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
randomCapping | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
|||
each | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
generator | |
0.00% |
0 / 1 |
8.19 | |
85.71% |
12 / 14 |
|||
passCapping | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 7 |
|||
resetRows | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\ToolsLaravel\FileReader; |
4 | |
5 | use Closure; |
6 | |
7 | class FileReader |
8 | { |
9 | protected $filename; |
10 | |
11 | protected $size; |
12 | |
13 | protected $nbLines; |
14 | |
15 | protected $rows = []; |
16 | |
17 | protected $capTo; |
18 | |
19 | protected $totalToCap; |
20 | |
21 | /** |
22 | * CsvReader constructor. |
23 | * |
24 | * @param $filename |
25 | * @param int $size |
26 | */ |
27 | public function __construct($filename, $size = 4096) |
28 | { |
29 | $this->filename = $filename; |
30 | $this->size = $size; |
31 | } |
32 | |
33 | /** |
34 | * @param null $nbLines |
35 | * @return array |
36 | */ |
37 | public function rows($nbLines = null) |
38 | { |
39 | $this->nbLines = $nbLines; |
40 | |
41 | $this->resetRows(); |
42 | foreach ($this->generator() as $row) { |
43 | $this->rows[] = trim($row); |
44 | } |
45 | |
46 | return $this->rows; |
47 | } |
48 | |
49 | /** |
50 | * Undocumented function |
51 | * |
52 | * @param [type] $max |
53 | * @param [type] $total |
54 | * @return self |
55 | */ |
56 | public function randomCapping($max, $total) |
57 | { |
58 | $this->capTo = $max; |
59 | $this->totalToCap = $total; |
60 | |
61 | return $this; |
62 | } |
63 | |
64 | /** |
65 | * Undocumented function |
66 | * |
67 | * @param Closure $func |
68 | * @param [type] $max |
69 | * @return void |
70 | */ |
71 | public function each(Closure $func, $max = null, $params = null) |
72 | { |
73 | $this->nbLines = $max; |
74 | |
75 | foreach ($this->generator() as $row) { |
76 | $func(str_replace(PHP_EOL, null, $row)); |
77 | } |
78 | } |
79 | |
80 | /** |
81 | * @return \Generator |
82 | */ |
83 | protected function generator() |
84 | { |
85 | $handle = fopen($this->filename, 'r'); |
86 | |
87 | $row = 0; |
88 | while (!feof($handle)) { |
89 | |
90 | if ($this->nbLines && $row == $this->nbLines || $this->capTo !== null && $this->capTo === 0) { |
91 | break; |
92 | } |
93 | |
94 | $line = fgets($handle, $this->size); |
95 | |
96 | if ($this->capTo !== null) { |
97 | if ($this->passCapping($row)) { |
98 | yield $line; |
99 | } |
100 | } else { |
101 | yield $line; |
102 | } |
103 | |
104 | $row++; |
105 | } |
106 | |
107 | fclose($handle); |
108 | } |
109 | |
110 | /** |
111 | * |
112 | */ |
113 | protected function passCapping($currentRowIndex) |
114 | { |
115 | $left = $this->totalToCap - $currentRowIndex; |
116 | $percent = $this->capTo * 100 / $left; |
117 | $rand = mt_rand(1, 100); |
118 | |
119 | if ($rand <= $percent) { |
120 | $this->capTo--; |
121 | return true; |
122 | } |
123 | return false; |
124 | } |
125 | |
126 | /** |
127 | * |
128 | */ |
129 | protected function resetRows() |
130 | { |
131 | $this->rows = []; |
132 | } |
133 | } |