Passed
Pull Request — master (#131)
by
unknown
04:16
created

Zip::addMultipleFiles()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.0
13
 */
14
15
namespace Quantum\Libraries\Archive;
16
17
use ZipArchive;
18
19
/**
20
 * Class ArchiveInterface
21
 * @package Quantum\Libraries\Archive
22
 */
23
class Zip implements ArchiveInterface
24
{
25
    private $zipArchive;
26
27
    /**
28
     * Zip constructor
29
     */
30
    public function __construct()
31
    {
32
        $this->zipArchive = new ZipArchive();
33
    }
34
35
    public function __destruct()
36
    {
37
        if ($this->zipArchive->filename) {
38
            $this->zipArchive->close();
39
        }
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function addEmptyDir(string $archiveName, string $newDirectory): bool
46
    {
47
        if ($this->zipArchive->open($archiveName, ZipArchive::CREATE) === TRUE) {
48
            $newDirectory = rtrim($newDirectory, '/') . '/';
49
            if ($this->zipArchive->locateName($newDirectory) !== false) {
50
                return true;
51
            };
52
            return $this->zipArchive->addEmptyDir($newDirectory);
53
        } else {
54
            return false;
55
        }
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    public function addFile(string $archiveName, string $filePath, string $newFileName = ''): bool
62
    {
63
        if ($this->zipArchive->open($archiveName, ZipArchive::CREATE) === TRUE) {
64
            return $this->zipArchive->addFile($filePath, $newFileName);
65
        } else {
66
            return false;
67
        }
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function addFromString(string $archiveName, string $newFileName, string $newFileContent): bool
74
    {
75
        if ($this->zipArchive->open($archiveName, ZipArchive::CREATE) === TRUE) {
76
            return $this->zipArchive->addFromString($newFileName, $newFileContent);
77
        } else {
78
            return false;
79
        }
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85
    public function deleteUsingName(string $archiveName, string $fileOrDirName): bool
86
    {
87
        if (
88
            $this->zipArchive->open($archiveName) === TRUE
89
            && $this->zipArchive->locateName($fileOrDirName) !== false
90
        ) {
91
            return $this->zipArchive->deleteName($fileOrDirName);
92
        } else {
93
            return false;
94
        }
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100
    public function extractTo(string $archiveName, string $pathToExtract): bool
101
    {
102
        if ($this->zipArchive->open($archiveName) === TRUE) {
103
            return $this->zipArchive->extractTo($pathToExtract);
104
        } else {
105
            return false;
106
        }
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function count(): int
113
    {
114
        return $this->zipArchive->count();
115
    }
116
117
    /**
118
     * @inheritDoc
119
     */
120
    public function renameUsingName(string $archiveName, string $currentName, string $newName): bool
121
    {
122
        if ($this->zipArchive->open($archiveName) === TRUE) {
123
            return $this->zipArchive->renameName($currentName, $newName);
124
        } else {
125
            return false;
126
        }
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function addMultipleFiles(string $archiveName, array $fileNames): bool
133
    {
134
        if ($this->zipArchive->open($archiveName, ZipArchive::CREATE) === TRUE) {
135
            foreach ($fileNames as $fileNmae => $filePath) {
136
                if (!$this->zipArchive->addFile($filePath, $fileNmae)) {
137
                    return false;
138
                }
139
            }
140
            return true;
141
        } else {
142
            return false;
143
        }
144
    }
145
146
    /**
147
     * @inheritDoc
148
     */
149
    public function deleteMultipleFilesUsingName(string $archiveName, array $fileNames): bool
150
    {
151
        if ($this->zipArchive->open($archiveName) === TRUE) {
152
            foreach ($fileNames as $key => $fileOrDirName) {
153
                $this->zipArchive->deleteName($fileOrDirName);
154
            }
155
            return true;
156
        } else {
157
            return false;
158
        }
159
    }
160
}
161