Passed
Push — master ( 125b29...554a92 )
by f
27:06 queued 12:02
created

NelexaZip::deleteFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
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
        foreach ($this->zip->getAllInfo() as $info) {
86
            if ($info->isFolder())
87
                continue;
88
89
            $this->files[] = $information->files[] = str_replace('\\', '/', $info->getName());
90
            $information->compressedFilesSize += $info->getCompressedSize();
91
            $information->uncompressedFilesSize += $info->getSize();
92
        }
93
        return $information;
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function getFileNames()
100
    {
101
        return $this->files;
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function isFileExists($fileName)
108
    {
109
        return $this->zip->hasEntry($fileName);
110
    }
111
112
    /**
113
     * @inheritDoc
114
     */
115
    public function getFileData($fileName)
116
    {
117
        $info = $this->zip->getEntryInfo($fileName);
118
        return new ArchiveEntry(
119
            $fileName,
120
            $info->getCompressedSize(),
121
            $info->getSize(),
122
            $info->getMtime(),
123
            null,
124
            $info->getComment(),
125
            $info->getCrc()
126
        );
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function getFileContent($fileName)
133
    {
134
        return $this->zip->getEntryContents($fileName);
135
    }
136
137
    /**
138
     * @inheritDoc
139
     * @throws NonExistentArchiveFileException
140
     */
141
    public function getFileStream($fileName)
142
    {
143
        return static::wrapStringInStream($this->getFileContent($fileName));
144
    }
145
146
    /**
147
     * @inheritDoc
148
     */
149
    public function extractFiles($outputFolder, array $files)
150
    {
151
        $this->zip->extractTo($outputFolder, $files);
152
        return count($files);
153
    }
154
155
    /**
156
     * @inheritDoc
157
     */
158
    public function extractArchive($outputFolder)
159
    {
160
        $this->zip->extractTo($outputFolder);
161
        return count($this->files);
162
    }
163
164
    /**
165
     * @inheritDoc
166
     * @throws \PhpZip\Exception\ZipException
167
     */
168
    public function addFileFromString($inArchiveName, $content)
169
    {
170
        return $this->zip->addFromString($inArchiveName, $content);
171
    }
172
173
    public function getComment()
174
    {
175
        return $this->zip->getArchiveComment();
176
    }
177
178
    public function setComment($comment)
179
    {
180
        return $this->zip->setArchiveComment($comment);
181
    }
182
183
    public function deleteFiles(array $files)
184
    {
185
        $deleted = 0;
186
        foreach ($files as $file) {
187
            $this->zip->deleteFromName($file);
188
            $deleted++;
189
        }
190
        return $deleted;
191
    }
192
193
    public function addFiles(array $files)
194
    {
195
        foreach ($files as $inArchiveName => $fsFileName)
196
        {
197
            $this->zip->addFile($fsFileName, $inArchiveName);
198
        }
199
    }
200
}