GzipCompressor::compress()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.0163

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 12
cts 13
cp 0.9231
rs 8.9617
c 0
b 0
f 0
cc 6
nc 8
nop 2
crap 6.0163
1
<?php
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Component\Compressor;
11
12
class GzipCompressor implements CompressorInterface
13
{
14
    /**
15
     * @param string $source
16
     * @param string $target
17
     *
18
     * @return bool
19
     */
20 2
    public function compress($source, $target = '')
21
    {
22 2
        $target = $target ?: $source.'.gz';
23 2
        $fh = @fopen($source, 'rb');
24 2
        $gz = @gzopen($target, 'wb9');
25
26 2
        if (false === $fh || false === $gz) {
27 1
            return false;
28
        }
29
30 1
        while (!feof($fh)) {
31 1
            if (false === gzwrite($gz, fread($fh, 1024))) {
32
                return false;
33
            }
34 1
        }
35
36 1
        fclose($fh);
37 1
        gzclose($gz);
38
39 1
        return true;
40
    }
41
42
    /**
43
     * @param string $source
44
     * @param string $target
45
     *
46
     * @return bool
47
     */
48 2
    public function uncompress($source, $target)
49
    {
50 2
        $gz = @gzopen($source, 'rb');
51 2
        $fh = @fopen($target, 'wb');
52
53 2
        if (false === $fh || false === $gz) {
54 1
            return false;
55
        }
56
57 1
        while (!feof($gz)) {
58 1
            if (false === fwrite($fh, gzread($gz, 1024))) {
59
                return false;
60
            }
61 1
        }
62
63 1
        fclose($fh);
64 1
        gzclose($gz);
65
66 1
        return true;
67
    }
68
}
69