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

ZipCompressor::uncompress()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.7085

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 4
cts 7
cp 0.5714
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3.7085
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