Passed
Push — master ( afad4e...ae0184 )
by f
40:32 queued 25:34
created

NelexaZip::createArchive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace wapmorgan\UnifiedArchive\Drivers;
4
5
use PhpZip\ZipFile;
0 ignored issues
show
Bug introduced by
The type PhpZip\ZipFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use wapmorgan\UnifiedArchive\ArchiveEntry;
7
use wapmorgan\UnifiedArchive\ArchiveInformation;
8
use wapmorgan\UnifiedArchive\Exceptions\NonExistentArchiveFileException;
9
use wapmorgan\UnifiedArchive\Formats;
10
11
class NelexaZip extends BasicDriver
12
{
13
    const TYPE = self::TYPE_PURE_PHP;
14
15
    /**
16
     * @var ZipFile
17
     */
18
    protected $zip;
19
20
    /**
21
     * @var array
22
     */
23
    protected $files;
24
25
    public static function getDescription()
26
    {
27
        return 'nelexa/zip driver';
28
    }
29
30
    public static function isInstalled()
31
    {
32
        return class_exists('\\PhpZip\\ZipFile');
33
    }
34
35
    public static function getInstallationInstruction()
36
    {
37
        return 'install library [nelexa/zip]: `composer require nelexa/zip`';
38
    }
39
40
    public static function getSupportedFormats()
41
    {
42
        return [
43
            Formats::ZIP,
44
        ];
45
    }
46
47
    public static function checkFormatSupport($format)
48
    {
49
        if (!static::isInstalled()) {
50
            return [];
51
        }
52
        return [
53
            BasicDriver::OPEN,
54
            BasicDriver::OPEN_ENCRYPTED,
55
            BasicDriver::GET_COMMENT,
56
            BasicDriver::SET_COMMENT,
57
            BasicDriver::EXTRACT_CONTENT,
58
            BasicDriver::APPEND,
59
            BasicDriver::DELETE,
60
        ];
61
    }
62
63
    public static function createArchive(
64
        array $files,
65
        $archiveFileName,
66
        $archiveFormat,
67
        $compressionLevel = self::COMPRESSION_AVERAGE,
68
        $password = null,
69
        $fileProgressCallable = null)
70
    {
71
72
    }
73
74
    /**
75
     * @inheritDoc
76
     * @throws \PhpZip\Exception\ZipException
77
     */
78
    public function __construct($archiveFileName, $format, $password = null)
79
    {
80
        parent::__construct($archiveFileName, $format);
81
        $this->zip = new ZipFile();
82
        $this->zip->openFile($archiveFileName);
83
        if ($password !== null) {
84
            $this->zip->setReadPassword($password);
85
        }
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91
    public function getArchiveInformation()
92
    {
93
        $this->files = [];
94
        $information = new ArchiveInformation();
95
96
        $files = method_exists($this->zip, 'getAllInfo')
97
            ? $this->zip->getAllInfo()
98
            : $this->zip->getEntries();
99
100
        foreach ($files as $info) {
101
            if (method_exists($info, 'isFolder') ? $info->isFolder() : $info->isDirectory())
102
                continue;
103
104
            $this->files[] = $information->files[] = str_replace('\\', '/', $info->getName());
105
            $information->compressedFilesSize += $info->getCompressedSize();
106
            $information->uncompressedFilesSize += method_exists($info, 'getSize') ? $info->getSize() : $info->getUncompressedSize();
107
        }
108
        return $information;
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    public function getFileNames()
115
    {
116
        return $this->files;
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    public function isFileExists($fileName)
123
    {
124
        return $this->zip->hasEntry($fileName);
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130
    public function getFileData($fileName)
131
    {
132
        $info = method_exists($this->zip, 'getEntryInfo')
133
            ? $this->zip->getEntryInfo($fileName)
134
            : $this->zip->getEntry($fileName);
135
136
        return new ArchiveEntry(
137
            $fileName,
138
            $info->getCompressedSize(),
139
            method_exists($info, 'getSize') ? $info->getSize() : $info->getUncompressedSize(),
140
            $info->getMtime(),
141
            null,
142
            $info->getComment(),
143
            $info->getCrc()
144
        );
145
    }
146
147
    /**
148
     * @inheritDoc
149
     */
150
    public function getFileContent($fileName)
151
    {
152
        return $this->zip->getEntryContents($fileName);
153
    }
154
155
    /**
156
     * @inheritDoc
157
     * @throws NonExistentArchiveFileException
158
     */
159
    public function getFileStream($fileName)
160
    {
161
        return static::wrapStringInStream($this->getFileContent($fileName));
162
    }
163
164
    /**
165
     * @inheritDoc
166
     */
167
    public function extractFiles($outputFolder, array $files)
168
    {
169
        $this->zip->extractTo($outputFolder, $files);
170
        return count($files);
171
    }
172
173
    /**
174
     * @inheritDoc
175
     */
176
    public function extractArchive($outputFolder)
177
    {
178
        $this->zip->extractTo($outputFolder);
179
        return count($this->files);
180
    }
181
182
    /**
183
     * @inheritDoc
184
     * @throws \PhpZip\Exception\ZipException
185
     */
186
    public function addFileFromString($inArchiveName, $content)
187
    {
188
        return $this->zip->addFromString($inArchiveName, $content);
189
    }
190
191
    public function getComment()
192
    {
193
        return $this->zip->getArchiveComment();
194
    }
195
196
    public function setComment($comment)
197
    {
198
        return $this->zip->setArchiveComment($comment);
199
    }
200
201
    public function deleteFiles(array $files)
202
    {
203
        $deleted = 0;
204
        foreach ($files as $file) {
205
            $this->zip->deleteFromName($file);
206
            $deleted++;
207
        }
208
        return $deleted;
209
    }
210
211
    public function addFiles(array $files)
212
    {
213
        foreach ($files as $inArchiveName => $fsFileName)
214
        {
215
            $this->zip->addFile($fsFileName, $inArchiveName);
216
        }
217
    }
218
}
219