Passed
Push — master ( e9611b...a3681d )
by f
13:07
created

NelexaZip   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 162
rs 10
wmc 20

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getSupportedFormats() 0 4 1
A setComment() 0 3 1
A extractFiles() 0 2 1
A getDescription() 0 3 1
A getFileData() 0 11 1
A getFileNames() 0 3 1
A addFileFromString() 0 3 1
A getComment() 0 3 1
A getArchiveInformation() 0 14 3
A getFileStream() 0 3 1
A getFileContent() 0 3 1
A checkFormatSupport() 0 8 1
A isInstalled() 0 3 1
A extractArchive() 0 2 1
A __construct() 0 6 2
A isFileExists() 0 3 1
A getInstallationInstruction() 0 3 1
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
    /**
26
     * @inheritDoc
27
     * @throws \PhpZip\Exception\ZipException
28
     */
29
    public function __construct($archiveFileName, $format, $password = null)
30
    {
31
        $this->zip = new ZipFile();
32
        $this->zip->openFile($archiveFileName);
33
        if ($password !== null) {
34
            $this->zip->setReadPassword($password);
35
        }
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function getArchiveInformation()
42
    {
43
        $this->files = [];
44
        $information = new ArchiveInformation();
45
46
        foreach ($this->zip->getAllInfo() as $info) {
47
            if ($info->isFolder())
48
                continue;
49
50
            $this->files[] = $information->files[] = str_replace('\\', '/', $info->getName());
51
            $information->compressedFilesSize += $info->getCompressedSize();
52
            $information->uncompressedFilesSize += $info->getSize();
53
        }
54
        return $information;
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public function getFileNames()
61
    {
62
        return $this->files;
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function isFileExists($fileName)
69
    {
70
        return $this->zip->hasEntry($fileName);
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function getFileData($fileName)
77
    {
78
        $info = $this->zip->getEntryInfo($fileName);
79
        return new ArchiveEntry(
80
            $fileName,
81
            $info->getCompressedSize(),
82
            $info->getSize(),
83
            $info->getMtime(),
84
            null,
85
            $info->getComment(),
86
            $info->getCrc()
87
        );
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93
    public function getFileContent($fileName)
94
    {
95
        return $this->zip->getEntryContents($fileName);
96
    }
97
98
    /**
99
     * @inheritDoc
100
     * @throws NonExistentArchiveFileException
101
     */
102
    public function getFileStream($fileName)
103
    {
104
        return static::wrapStringInStream($this->getFileContent($fileName));
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110
    public function extractFiles($outputFolder, array $files)
111
    {
112
        // TODO: Implement extractFiles() method.
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118
    public function extractArchive($outputFolder)
119
    {
120
        // TODO: Implement extractArchive() method.
121
    }
122
123
    /**
124
     * @inheritDoc
125
     * @throws \PhpZip\Exception\ZipException
126
     */
127
    public function addFileFromString($inArchiveName, $content)
128
    {
129
        return $this->zip->addFromString($inArchiveName, $content);
130
    }
131
132
    public function getComment()
133
    {
134
        return $this->zip->getArchiveComment();
135
    }
136
137
    public function setComment($comment)
138
    {
139
        return $this->zip->setArchiveComment($comment);
140
    }
141
142
    public static function getSupportedFormats()
143
    {
144
        return [
145
            Formats::ZIP,
146
        ];
147
    }
148
149
    public static function checkFormatSupport($format)
150
    {
151
        return [
152
            BasicDriver::OPEN,
153
            BasicDriver::OPEN_ENCRYPTED,
154
            BasicDriver::EXTRACT_CONTENT,
155
            BasicDriver::APPEND,
156
            BasicDriver::DELETE,
157
        ];
158
    }
159
160
    public static function getDescription()
161
    {
162
        return 'nelexa/zip driver';
163
    }
164
165
    public static function isInstalled()
166
    {
167
        return class_exists('\\PhpZip\\ZipFile');
168
    }
169
170
    public static function getInstallationInstruction()
171
    {
172
        return 'install library [nelexa/zip]: `composer require nelexa/zip`';
173
    }
174
}