Passed
Push — master ( d62b89...abb8c8 )
by Michiel
06:34
created

UnzipTask   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 53
ccs 18
cts 22
cp 0.8182
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A extractArchive() 0 24 3
A listArchiveContent() 0 13 2
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
use Phing\Io\File;
21
use Phing\Project;
22
23
/**
24
 * Extracts one or several zip archives using ZipArchive class.
25
 *
26
 * @author  Joakim Bodin <[email protected]>
27
 * @author  George Miroshnikov <[email protected]>
28
 * @package phing.tasks.ext
29
 */
30
class UnzipTask extends ExtractBaseTask
31
{
32
    /**
33
     * Extract archive content into $this->todir directory
34
     *
35
     * @param  File Zip file to extract
36
     * @return boolean
37
     */
38 1
    protected function extractArchive(File $zipfile)
39
    {
40 1
        $this->log(
41 1
            "Extracting zip: " . $zipfile->__toString() . ' to ' . $this->todir->__toString(),
42 1
            Project::MSG_INFO
43
        );
44
45 1
        $zip = new ZipArchive();
46
47 1
        $result = $zip->open($zipfile->getAbsolutePath());
48 1
        if (!$result) {
49
            $this->log("Unable to open zipfile " . $zipfile->__toString(), Project::MSG_ERR);
50
51
            return false;
52
        }
53
54 1
        $result = $zip->extractTo($this->todir->getAbsolutePath());
55 1
        if (!$result) {
56
            $this->log("Unable to extract zipfile " . $zipfile->__toString(), Project::MSG_ERR);
57
58
            return false;
59
        }
60
61 1
        return true;
62
    }
63
64
    /**
65
     * List archive content
66
     *
67
     * @param  File Zip file to list content
68
     * @return array List of files inside $zipfile
69
     */
70 1
    protected function listArchiveContent(File $zipfile)
71
    {
72 1
        $zip = new ZipArchive();
73 1
        $zip->open($zipfile->getAbsolutePath());
74
75 1
        $content = [];
76 1
        for ($i = 0; $i < $zip->numFiles; $i++) {
77 1
            $content[] = [
78 1
                'filename' => $zip->getNameIndex($i)
79
            ];
80
        }
81
82 1
        return $content;
83
    }
84
}
85