Completed
Push — master ( 02a765...c0a6fd )
by Romain
16:38 queued 05:36
created

Archive::deleteTemp()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.2
cc 4
eloc 12
nc 4
nop 0
1
<?php
2
3
namespace Akeneo\Component\SpreadsheetParser\Xlsx;
4
5
/**
6
 * Represents an XLSX Archive
7
 *
8
 * @author    Antoine Guigan <[email protected]>
9
 * @copyright 2014 Akeneo SAS (http://www.akeneo.com)
10
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
11
 */
12
class Archive
13
{
14
    /**
15
     *
16
     * @var string
17
     */
18
    protected $tempPath;
19
20
    /**
21
     *
22
     * @var string
23
     */
24
    protected $archivePath;
25
26
    /**
27
     * @var \ZipArchive
28
     */
29
    private $zip;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param string $archivePath
35
     */
36
    public function __construct($archivePath)
37
    {
38
        $this->archivePath = $archivePath;
39
        $this->tempPath = tempnam(sys_get_temp_dir(), 'xls_parser_archive');
40
        unlink($this->tempPath);
41
    }
42
43
    /**
44
     * Extracts the specified file to a temp path, and return the temp path
45
     *
46
     * Files are only extracted once for the given archive
47
     *
48
     * @param string $filePath
49
     *
50
     * @return string
51
     */
52
    public function extract($filePath)
53
    {
54
        $tempPath = sprintf('%s/%s', $this->tempPath, $filePath);
55
        if (!file_exists($tempPath)) {
56
            $this->getArchive()->extractTo($this->tempPath, $filePath);
57
        }
58
59
        return $tempPath;
60
    }
61
62
    /**
63
     * Clears all extracted files
64
     */
65
    public function __destruct()
66
    {
67
        $this->deleteTemp();
68
        $this->closeArchive();
69
    }
70
71
    /**
72
     * Returns the archive
73
     *
74
     * @return \ZipArchive
75
     */
76
    protected function getArchive()
77
    {
78
        if (!$this->zip) {
79
            $this->zip = new \ZipArchive();
80
            if (true !== $errorCode = $this->zip->open($this->archivePath)) {
81
                $this->zip = null;
82
                throw new \RuntimeException('Error opening file: '.$this->getErrorMessage($errorCode));
83
            }
84
        }
85
86
        return $this->zip;
87
    }
88
89
    /**
90
     * Closes the archive
91
     */
92
    protected function closeArchive()
93
    {
94
        if ($this->zip) {
95
            $this->zip->close();
96
            $this->zip = null;
97
        }
98
    }
99
100
    /**
101
     * Deletes temporary files
102
     */
103
    protected function deleteTemp()
104
    {
105
        if (!file_exists($this->tempPath)) {
106
            return;
107
        }
108
109
        $files = new \RecursiveIteratorIterator(
110
            new \RecursiveDirectoryIterator($this->tempPath, \RecursiveDirectoryIterator::SKIP_DOTS),
111
            \RecursiveIteratorIterator::CHILD_FIRST
112
        );
113
        foreach ($files as $file) {
114
            if ($file->isDir()) {
115
                rmdir($file->getRealPath());
116
            } else {
117
                unlink($file->getRealPath());
118
            }
119
        }
120
        rmdir($this->tempPath);
121
    }
122
123
    private function getErrorMessage($errorCode)
124
    {
125
        switch ($errorCode) {
126
            case \ZipArchive::ER_MULTIDISK:
127
                return 'Multi-disk zip archives not supported';
128
129
            case \ZipArchive::ER_RENAME:
130
                return 'Renaming temporary file failed';
131
132
            case \ZipArchive::ER_CLOSE:
133
                return 'Closing zip archive failed';
134
135
            case \ZipArchive::ER_SEEK:
136
                return 'Seek error';
137
138
            case \ZipArchive::ER_READ:
139
                return 'Read error';
140
141
            case \ZipArchive::ER_WRITE:
142
                return 'Write error';
143
144
            case \ZipArchive::ER_CRC:
145
                return 'CRC error';
146
147
            case \ZipArchive::ER_ZIPCLOSED:
148
                return 'Containing zip archive was closed';
149
150
            case \ZipArchive::ER_NOENT:
151
                return 'No such file';
152
153
            case \ZipArchive::ER_EXISTS:
154
                return 'File already exists';
155
156
            case \ZipArchive::ER_OPEN:
157
                return 'Can\'t open file';
158
159
            case \ZipArchive::ER_TMPOPEN:
160
                return 'Failure to create temporary file';
161
162
            case \ZipArchive::ER_ZLIB:
163
                return 'Zlib error';
164
165
            case \ZipArchive::ER_MEMORY:
166
                return 'Malloc failure';
167
168
            case \ZipArchive::ER_CHANGED:
169
                return 'Entry has been changed';
170
171
            case \ZipArchive::ER_COMPNOTSUPP:
172
                return 'Compression method not supported';
173
174
            case \ZipArchive::ER_EOF:
175
                return 'Premature EOF';
176
177
            case \ZipArchive::ER_INVAL:
178
                return 'Invalid argument';
179
180
            case \ZipArchive::ER_NOZIP:
181
                return 'Not a zip archive';
182
183
            case \ZipArchive::ER_INTERNAL:
184
                return 'Internal error';
185
186
            case \ZipArchive::ER_INCONS:
187
                return 'Zip archive inconsistent';
188
189
            case \ZipArchive::ER_REMOVE:
190
                return 'Can\'t remove file';
191
192
            case \ZipArchive::ER_DELETED:
193
                return 'Entry has been deleted';
194
195
            default:
196
                return 'An unknown error has occurred('.intval($errorCode).')';
197
        }
198
    }
199
}
200