ZipHandler::compress()   B
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7.0422

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 35
ccs 19
cts 21
cp 0.9048
rs 8.6666
cc 7
nc 10
nop 2
crap 7.0422
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpEpub;
6
7
use RecursiveDirectoryIterator;
8
use RecursiveIteratorIterator;
9
use ZipArchive;
10
11
class ZipHandler
12
{
13
    /**
14
     * Extracts a ZIP file to a specified directory.
15
     *
16
     * @param string $zipFilePath The path to the ZIP file.
17
     * @param string $destination The directory where the contents should be extracted.
18
     *
19
     * @throws Exception If the extraction fails.
20
     */
21 30
    public function extract(string $zipFilePath, string $destination): void
22
    {
23 30
        if (! file_exists($zipFilePath)) {
24 7
            throw new Exception("ZIP file does not exist: {$zipFilePath}");
25
        }
26
27 23
        $zip = new ZipArchive();
28 23
        if ($zip->open($zipFilePath) !== true) {
29 1
            throw new Exception("Failed to open ZIP file: {$zipFilePath}");
30
        }
31
32 22
        if (! $zip->extractTo($destination)) {
33
            $zip->close();
34
            throw new Exception("Failed to extract ZIP file to: {$destination}");
35
        }
36
37 22
        $zip->close();
38
    }
39
40
    /**
41
     * Compresses a directory into a ZIP file.
42
     *
43
     * @param string $source The directory to compress.
44
     * @param string $zipFilePath The path where the ZIP file should be created.
45
     *
46
     * @throws Exception If the compression fails.
47
     */
48 17
    public function compress(string $source, string $zipFilePath): void
49
    {
50 17
        $zip = new ZipArchive();
51 17
        if ($zip->open($zipFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
52
            throw new Exception("Failed to create ZIP file: {$zipFilePath}");
53
        }
54
55 17
        $realSource = realpath($source);
56 17
        if ($realSource === false) {
57 1
            throw new Exception("Invalid source directory: {$source}");
58
        }
59
60 16
        $files = new RecursiveIteratorIterator(
61 16
            new RecursiveDirectoryIterator($realSource, RecursiveDirectoryIterator::SKIP_DOTS),
62 16
            RecursiveIteratorIterator::SELF_FIRST
63 16
        );
64
65
        /** @var \SplFileInfo $file */
66 16
        foreach ($files as $file) {
67 16
            $filePath = $file->getRealPath();
68 16
            if ($filePath === false) {
69
                continue;
70
            }
71
72 16
            $relativePath = substr($filePath, strlen($realSource) + 1);
73
74 16
            if ($file->isDir()) {
75 14
                $zip->addEmptyDir($relativePath);
76
            } else {
77 16
                $zip->addFile($filePath, $relativePath);
78
            }
79
        }
80
81 16
        if (! $zip->close()) {
82 1
            throw new Exception("Failed to finalize ZIP file: {$zipFilePath}");
83
        }
84
    }
85
}
86