Completed
Push — master ( 6f516e...bda4be )
by Peter
01:19 queued 11s
created

Bzip2Compressor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 0
loc 57
ccs 23
cts 25
cp 0.92
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B compress() 0 21 6
A uncompress() 0 20 5
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 Bzip2Compressor 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.'.bz2';
23 2
        $fh = @fopen($source, 'rb');
24 2
        $bz = @bzopen($target, 'w');
25
26 2
        if (false === $fh || false === $bz) {
27 1
            return false;
28
        }
29
30 1
        while (!feof($fh)) {
31 1
            if (false === bzwrite($bz, fread($fh, 1024))) {
32
                return false;
33
            }
34 1
        }
35
36 1
        fclose($fh);
37 1
        bzclose($bz);
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
        $bz = @bzopen($source, 'r');
51 2
        $fh = @fopen($target, 'wb');
52
53 2
        if (false === $fh || false === $bz) {
54 1
            return false;
55
        }
56
57 1
        while (!feof($bz)) {
58 1
            if (false === fwrite($fh, bzread($bz, 1024))) {
59
                return false;
60
            }
61 1
        }
62
63 1
        fclose($fh);
64 1
        bzclose($bz);
65
66 1
        return true;
67
    }
68
}
69