Conditions | 5 |
Paths | 5 |
Total Lines | 33 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
11 | public function handler(string $filename, ?int $level = null): string |
||
12 | { |
||
13 | $filenameCompress = $filename.'.'.self::EXTENSION_COMPRESS; |
||
14 | |||
15 | $fd = fopen($filename, 'r'); |
||
16 | |||
17 | if ($fd === false) { |
||
18 | throw new Exception("file {$filename} not can read.", 100); |
||
19 | } |
||
20 | |||
21 | $level = $level ?? ''; |
||
22 | |||
23 | $gz = gzopen($filenameCompress, "wb{$level}"); |
||
24 | |||
25 | if ($gz === false) { |
||
26 | fclose($fd); |
||
27 | |||
28 | throw new Exception("file {$filenameCompress} not can open.", 101); |
||
29 | } |
||
30 | |||
31 | while (!feof($fd)) { |
||
32 | $data = fread($fd, 1024 * 512); |
||
33 | |||
34 | $data = $data === false ? '' : $data; |
||
35 | |||
36 | gzwrite($gz, $data); |
||
37 | } |
||
38 | |||
39 | gzclose($gz); |
||
40 | fclose($fd); |
||
41 | unlink($filename); |
||
42 | |||
43 | return $filenameCompress; |
||
44 | } |
||
46 |