Completed
Push — master ( 62d0c8...1d7e23 )
by Peter
06:36
created

ZipCompressor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 59
ccs 12
cts 18
cp 0.6667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A compress() 0 16 4
A uncompress() 0 14 3
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 ZipCompressor implements CompressorInterface
13
{
14
    /**
15
     * @var \ZipArchive
16
     */
17
    protected $zip;
18
19
    /**
20
     * @param \ZipArchive $zip
21
     */
22 1
    public function __construct(\ZipArchive $zip)
23
    {
24 1
        $this->zip = $zip;
25 1
    }
26
27
    /**
28
     * @param string $source
29
     * @param string $target
30
     *
31
     * @return bool
32
     */
33 1
    public function compress($source, $target = '')
34
    {
35 1
        $target = $target ?: $source.'.zip';
36
37 1
        if ($this->zip->open($target) === false) {
38
            return false;
39
        }
40
41 1
        if ($this->zip->addFile($source, basename($source)) === false) {
42
            $this->zip->close();
43
44
            return false;
45
        }
46
47 1
        return $this->zip->close();
48
    }
49
50
    /**
51
     * @param string $source
52
     * @param string $target
53
     *
54
     * @return bool
55
     */
56 1
    public function uncompress($source, $target)
57
    {
58 1
        if ($this->zip->open($source) === false) {
59
            return false;
60
        }
61
62 1
        if ($this->zip->extractTo(dirname($target), basename($target)) === false) {
63
            $this->zip->close();
64
65
            return false;
66
        }
67
68 1
        return $this->zip->close();
69
    }
70
}
71