Passed
Push — 1.1.x ( bc52f7...e8e75c )
by f
12:04
created

NelexaZip::getFileNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
    /**
64
     * @inheritDoc
65
     * @throws \PhpZip\Exception\ZipException
66
     */
67
    public function __construct($archiveFileName, $format, $password = null)
68
    {
69
        parent::__construct($archiveFileName, $format);
70
        $this->zip = new ZipFile();
71
        $this->zip->openFile($archiveFileName);
72
        if ($password !== null) {
73
            $this->zip->setReadPassword($password);
74
        }
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function getArchiveInformation()
81
    {
82
        $this->files = [];
83
        $information = new ArchiveInformation();
84
85
        $files = method_exists($this->zip, 'getAllInfo')
86
            ? $this->zip->getAllInfo()
87
            : $this->zip->getEntries();
88
89
        foreach ($files as $info) {
90
            if (method_exists($info, 'isFolder') ? $info->isFolder() : $info->isDirectory())
91
                continue;
92
93
            $this->files[] = $information->files[] = str_replace('\\', '/', $info->getName());
94
            $information->compressedFilesSize += $info->getCompressedSize();
95
            $information->uncompressedFilesSize += method_exists($info, 'getSize') ? $info->getSize() : $info->getUncompressedSize();
96
        }
97
        return $information;
98
    }
99
100
    /**
101
     * @inheritDoc
102
     */
103
    public function getFileNames()
104
    {
105
        return $this->files;
106
    }
107
108
    /**
109
     * @inheritDoc
110
     */
111
    public function isFileExists($fileName)
112
    {
113
        return $this->zip->hasEntry($fileName);
114
    }
115
116
    /**
117
     * @inheritDoc
118
     */
119
    public function getFileData($fileName)
120
    {
121
        $info = method_exists($this->zip, 'getEntryInfo')
122
            ? $this->zip->getEntryInfo($fileName)
123
            : $this->zip->getEntry($fileName);
124
125
        return new ArchiveEntry(
126
            $fileName,
127
            $info->getCompressedSize(),
128
            method_exists($info, 'getSize') ? $info->getSize() : $info->getUncompressedSize(),
129
            $info->getMtime(),
130
            null,
131
            $info->getComment(),
132
            $info->getCrc()
133
        );
134
    }
135
136
    /**
137
     * @inheritDoc
138
     */
139
    public function getFileContent($fileName)
140
    {
141
        return $this->zip->getEntryContents($fileName);
142
    }
143
144
    /**
145
     * @inheritDoc
146
     * @throws NonExistentArchiveFileException
147
     */
148
    public function getFileStream($fileName)
149
    {
150
        return static::wrapStringInStream($this->getFileContent($fileName));
151
    }
152
153
    /**
154
     * @inheritDoc
155
     */
156
    public function extractFiles($outputFolder, array $files)
157
    {
158
        $this->zip->extractTo($outputFolder, $files);
159
        return count($files);
160
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165
    public function extractArchive($outputFolder)
166
    {
167
        $this->zip->extractTo($outputFolder);
168
        return count($this->files);
169
    }
170
171
    /**
172
     * @inheritDoc
173
     * @throws \PhpZip\Exception\ZipException
174
     */
175
    public function addFileFromString($inArchiveName, $content)
176
    {
177
        return $this->zip->addFromString($inArchiveName, $content);
178
    }
179
180
    public function getComment()
181
    {
182
        return $this->zip->getArchiveComment();
183
    }
184
185
    public function setComment($comment)
186
    {
187
        return $this->zip->setArchiveComment($comment);
188
    }
189
190
    public function deleteFiles(array $files)
191
    {
192
        $deleted = 0;
193
        foreach ($files as $file) {
194
            $this->zip->deleteFromName($file);
195
            $deleted++;
196
        }
197
        return $deleted;
198
    }
199
200
    public function addFiles(array $files)
201
    {
202
        foreach ($files as $inArchiveName => $fsFileName)
203
        {
204
            $this->zip->addFile($fsFileName, $inArchiveName);
205
        }
206
    }
207
}
208