ZipManager::setPath()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Comodojo\Zip;
4
5
use \Comodojo\Zip\Interfaces\ZipInterface;
6
use \Comodojo\Zip\Base\ManagerTools;
7
use \Comodojo\Foundation\Utils\UniqueId;
8
use \Comodojo\Exception\ZipException;
9
use \Countable;
10
use \Exception;
11
12
/**
13
 * Multiple Comodojo\Zip\Zip manager
14
 *
15
 * @package     Comodojo Spare Parts
16
 * @author      Marco Giovinazzi <[email protected]>
17
 * @license     MIT
18
 *
19
 * LICENSE:
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
 * THE SOFTWARE.
28
 */
29
30
class ZipManager implements Countable
31
{
32
33
    /**
34
     * Array of managed zip files
35
     *
36
     * @var array
37
     */
38
    private array $zip_archives = [];
39
40
    /**
41
     * Count the number of Zip objects registered to the manager
42
     *
43
     * @return int
44
     */
45 1
    public function count(): int
46
    {
47 1
        return count($this->zip_archives);
48
    }
49
50
    /**
51
     * Add a Zip object to manager and return its id
52
     *
53
     * @param Zip $zip
54
     *
55
     * @return string
56
     */
57 5
    public function addZip(ZipInterface $zip): string
58
    {
59 5
        $id = UniqueId::generate(32);
60 5
        $this->zip_archives[$id] = $zip;
61 5
        return $id;
62
    }
63
64
    /**
65
     * Remove a Zip object from manager
66
     *
67
     * @param Zip $zip
68
     *
69
     * @return bool
70
     * @throws ZipException
71
     */
72 1
    public function removeZip(ZipInterface $zip): bool
73
    {
74 1
        $archive_key = array_search($zip, $this->zip_archives, true);
75 1
        if ($archive_key === false) {
76
            throw new ZipException("Archive not found");
77
        }
78
79 1
        unset($this->zip_archives[$archive_key]);
80 1
        return true;
81
    }
82
83
    /**
84
     * Remove a Zip object from manager by Zip id
85
     *
86
     * @param string $id
87
     *
88
     * @return bool
89
     * @throws ZipException
90
     */
91 1
    public function removeZipById(string $id): bool
92
    {
93 1
        if (isset($this->zip_archives[$id]) === false) {
94
            throw new ZipException("Archive: $id not found");
95
        }
96 1
        unset($this->zip_archives[$id]);
97 1
        return true;
98
    }
99
100
    /**
101
     * Get a list of all registered Zips filenames as an array
102
     *
103
     * @return array
104
     */
105 1
    public function listZips(): array
106
    {
107 1
        return array_column(
108 1
            array_map(function ($key, $archive) {
109 1
                return ["key" => $key, "file" => $archive->getZipFile()];
110 1
            }, array_keys($this->zip_archives), $this->zip_archives),
111 1
            "file",
112 1
            "key"
113
        );
114
    }
115
116
    /**
117
     * Get a Zip object by Id
118
     *
119
     * @param string $id The zip id
120
     *
121
     * @return Zip
122
     * @throws ZipException
123
     */
124 2
    public function getZip(string $id): ZipInterface
125
    {
126 2
        if (array_key_exists($id, $this->zip_archives) === false) {
127 1
            throw new ZipException("Archive id $id not found");
128
        }
129 1
        return $this->zip_archives[$id];
130
    }
131
132
    /**
133
     * Set current base path (to add relative files to zip archive)
134
     * for all Zips
135
     *
136
     * @param string $path
137
     *
138
     * @return ZipManager
139
     * @throws ZipException
140
     */
141
    public function setPath(string $path): ZipManager
142
    {
143
        foreach ($this->zip_archives as $archive) {
144
            $archive->setPath($path);
145
        }
146
        return $this;
147
    }
148
149
    /**
150
     * Get a list of paths used by Zips
151
     *
152
     * @return  array
153
     */
154
    public function getPath(): array
155
    {
156
        return array_column(
157
            array_map(function ($key, $archive) {
158
                return ["key" => $key, "path" => $archive->getPath()];
159
            }, array_keys($this->zip_archives), $this->zip_archives),
160
            "path",
161
            "key"
162
        );
163
    }
164
165
    /**
166
     * Set default file mask for all Zips
167
     *
168
     * @param int $mask
169
     *
170
     * @return ZipManager
171
     * @throws ZipException
172
     */
173
    public function setMask(int $mask): ZipManager
174
    {
175
        foreach ($this->zip_archives as $archive) {
176
            $archive->setMask($mask);
177
        }
178
        return $this;
179
    }
180
181
    /**
182
     * Get a list of masks from Zips
183
     *
184
     * @return array
185
     */
186
    public function getMask(): array
187
    {
188
        return array_column(
189
            array_map(function ($key, $archive) {
190
                return ["key" => $key, "mask" => $archive->getMask()];
191
            }, array_keys($this->zip_archives), $this->zip_archives),
192
            "mask",
193
            "key"
194
        );
195
    }
196
197
    /**
198
     * Get a list of files in Zips
199
     *
200
     * @return array
201
     * @throws ZipException
202
     */
203 1
    public function listFiles(): array
204
    {
205 1
        return array_column(
206 1
            array_map(function ($key, $archive) {
207 1
                return ["key" => $key, "files" => $archive->listFiles()];
208 1
            }, array_keys($this->zip_archives), $this->zip_archives),
209 1
            "files",
210 1
            "key"
211
        );
212
    }
213
214
    /**
215
     * Extract Zips to common destination
216
     *
217
     * @param string $destination Destination path
218
     * @param bool $separate (optional) If true (default), files will be placed in different directories
219
     * @param mixed $files (optional) a filename or an array of filenames
220
     *
221
     * @return bool
222
     * @throws ZipException
223
     */
224 2
    public function extract(
225
        string $destination,
226
        bool $separate = true,
227
        $files = null
228
    ): bool {
229
230 2
        foreach ($this->zip_archives as $archive) {
231
232 2
            $local_path = substr($destination, -1) == '/' ? $destination : $destination . '/';
233 2
            $local_file = pathinfo($archive->getZipFile());
234 2
            $local_destination = $separate ? ($local_path . $local_file['filename']) : $destination;
235
236 2
            $archive->extract($local_destination, $files);
237
        }
238 2
        return true;
239
    }
240
241
    /**
242
     * Merge multiple Zips into one
243
     *
244
     * @param string $output_zip_file
245
     *  Destination zip
246
     * @param bool $separate (optional)
247
     *  If true (default), files will be placed in different directories
248
     *
249
     * @return bool
250
     * @throws ZipException
251
     */
252 1
    public function merge(string $output_zip_file, bool $separate = true): bool
253
    {
254 1
        $pathinfo = pathinfo($output_zip_file);
255 1
        $temporary_folder = $pathinfo['dirname'] . "/" . ManagerTools::getTemporaryFolder();
256
257 1
        $this->extract($temporary_folder, $separate);
258 1
        $zip = Zip::create($output_zip_file);
259 1
        $zip->add($temporary_folder, true)->close();
260 1
        ManagerTools::recursiveUnlink($temporary_folder);
261 1
        return true;
262
    }
263
264
    /**
265
     * Add a file to all registered Zips
266
     *
267
     * @param mixed $file_name_or_array
268
     *  The filename to add or an array of filenames
269
     * @param bool $flatten_root_folder
270
     *  (optional) If true, the Zip root folder will be flattened (default: false)
271
     *
272
     * @return ZipManager
273
     * @throws ZipException
274
     */
275 1
    public function add($file_name_or_array, bool $flatten_root_folder = false): ZipManager
276
    {
277 1
        foreach ($this->zip_archives as $archive) {
278 1
            $archive->add($file_name_or_array, $flatten_root_folder);
279
        }
280 1
        return $this;
281
    }
282
283
    /**
284
     * Delete a file from any registered Zip
285
     *
286
     * @param mixed $file_name_or_array
287
     *  The filename to add or an array of filenames
288
     *
289
     * @return ZipManager
290
     * @throws ZipException
291
     */
292
    public function delete($file_name_or_array): ZipManager
293
    {
294
        foreach ($this->zip_archives as $archive) {
295
            $archive->delete($file_name_or_array);
296
        }
297
        return $this;
298
    }
299
300
    /**
301
     * Close all Zips
302
     *
303
     * @return bool
304
     * @throws ZipException
305
     */
306 4
    public function close(): bool
307
    {
308 4
        foreach ($this->zip_archives as $archive) {
309 3
            $archive->close();
310
        }
311 4
        return true;
312
    }
313
}
314