Completed
Push — 0.1.x ( 4972a0...752a54 )
by f
01:47
created

SevenZip   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Test Coverage

Coverage 34.52%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 76
c 2
b 0
f 0
dl 0
loc 229
ccs 29
cts 84
cp 0.3452
rs 9.92
wmc 31

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getFileContent() 0 4 1
A canCreateArchive() 0 3 1
A getFileResource() 0 8 1
A createArchive() 0 14 4
A addFiles() 0 15 4
A getArchiveInformation() 0 11 3
A deleteFiles() 0 11 3
A extractFiles() 0 13 3
A isFileExists() 0 3 1
A canAddFiles() 0 4 2
A canDeleteFiles() 0 3 1
A getFileNames() 0 6 2
A extractArchive() 0 8 2
A getFileData() 0 5 1
1
<?php
2
namespace wapmorgan\UnifiedArchive\Formats;
3
4
use Exception;
5
use wapmorgan\UnifiedArchive\ArchiveEntry;
6
use wapmorgan\UnifiedArchive\ArchiveInformation;
7
8
class SevenZip extends BasicFormat
9
{
10
    /** @var Archive7z */
11
    protected $sevenZip;
12
13
    /**
14
     * BasicFormat constructor.
15
     *
16
     * @param string $archiveFileName
17
     *
18
     * @throws \Exception
19
     */
20 3
    public function __construct($archiveFileName)
21
    {
22
        try {
23 3
            $this->sevenZip = new Archive7z($archiveFileName, null, null);
24
        } catch (\Archive7z\Exception $e) {
0 ignored issues
show
Bug introduced by
The type Archive7z\Exception 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...
25
            throw new Exception('Could not open 7Zip archive: '.$e->getMessage(), $e->getCode(), $e);
26
        }
27 3
    }
28
29
    /**
30
     * @return ArchiveInformation
31
     */
32 3
    public function getArchiveInformation()
33
    {
34 3
        $information = new ArchiveInformation();
35 3
        foreach ($this->sevenZip->getEntries() as $entry) {
36 3
            $information->files[] = method_exists($entry, 'getUnixPath')
37 3
                ? $entry->getUnixPath()
38
                : str_replace('\\', '/', $entry->getPath());
39 3
            $information->compressedFilesSize += (int)$entry->getPackedSize();
40 3
            $information->uncompressedFilesSize += (int)$entry->getSize();
41
        }
42 3
        return $information;
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function getFileNames()
49
    {
50
        $files = [];
51
        foreach ($this->sevenZip->getEntries() as $entry)
52
            $files[] = $entry->getPath();
53
        return $files;
54
    }
55
56
    /**
57
     * @param string $fileName
58
     *
59
     * @return bool
60
     */
61
    public function isFileExists($fileName)
62
    {
63
        return $this->sevenZip->getEntry($fileName) !== null;
64
    }
65
66
    /**
67
     * @param string $fileName
68
     *
69
     * @return ArchiveEntry|false
70
     */
71 1
    public function getFileData($fileName)
72
    {
73 1
        $entry = $this->sevenZip->getEntry($fileName);
74 1
        return new ArchiveEntry($fileName, $entry->getPackedSize(), $entry->getSize(),
75 1
            strtotime($entry->getModified()));
76
    }
77
78
    /**
79
     * @param string $fileName
80
     *
81
     * @return string|false
82
     */
83 1
    public function getFileContent($fileName)
84
    {
85 1
        $entry = $this->sevenZip->getEntry($fileName);
86 1
        return $entry->getContent();
87
    }
88
89
    /**
90
     * @param string $fileName
91
     *
92
     * @return bool|resource|string
93
     */
94 1
    public function getFileResource($fileName)
95
    {
96 1
        $resource = fopen('php://temp', 'r+');
97 1
        $entry = $this->sevenZip->getEntry($fileName);
98
99 1
        fwrite($resource, $entry->getContent());
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
        fwrite(/** @scrutinizer ignore-type */ $resource, $entry->getContent());
Loading history...
100 1
        rewind($resource);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $handle of rewind() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
        rewind(/** @scrutinizer ignore-type */ $resource);
Loading history...
101 1
        return $resource;
102
    }
103
104
    /**
105
     * @param string $outputFolder
106
     * @param array  $files
107
     *
108
     * @return false|int
109
     * @throws \Archive7z\Exception
110
     * @throws \Exception
111
     */
112
    public function extractFiles($outputFolder, array $files)
113
    {
114
        $this->sevenZip->setOutputDirectory($outputFolder);
115
        $count = 0;
116
        try {
117
118
            foreach ($files as $file) {
119
                $this->sevenZip->extractEntry($file);
120
                $count++;
121
            }
122
            return $count;
123
        } catch (Exception $e) {
124
            throw new Exception('Could not extract archive: '.$e->getMessage(), $e->getCode(), $e);
125
        }
126
    }
127
128
    /**
129
     * @param string $outputFolder
130
     *
131
     * @return false|bool
132
     * @throws \Exception
133
     */
134
    public function extractArchive($outputFolder)
135
    {
136
        $this->sevenZip->setOutputDirectory($outputFolder);
137
        try {
138
            $this->sevenZip->extract();
139
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the return type mandated by wapmorgan\UnifiedArchive...ormat::extractArchive() of false|integer.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
140
        } catch (Exception $e) {
141
            throw new Exception('Could not extract archive: '.$e->getMessage(), $e->getCode(), $e);
142
        }
143
    }
144
145
    /**
146
     * @param array $files
147
     *
148
     * @return false|int
149
     * @throws \Exception
150
     */
151
    public function deleteFiles(array $files)
152
    {
153
        $count = 0;
154
        try {
155
            foreach ($files as $file) {
156
                $this->sevenZip->delEntry($file);
157
                $count++;
158
            }
159
            return $count;
160
        } catch (Exception $e) {
161
            throw new Exception('Could not modify archive: '.$e->getMessage(), $e->getCode(), $e);
162
        }
163
    }
164
165
    /**
166
     * @param array $files
167
     *
168
     * @return false|int
169
     * @throws \Exception
170
     */
171
    public function addFiles(array $files)
172
    {
173
        $added_files = 0;
174
        try {
175
            foreach ($files as $localName => $filename) {
176
                if (!is_null($filename)) {
177
                    $this->sevenZip->addEntry($filename);
178
                    $this->sevenZip->renameEntry($filename, $localName);
179
                    $added_files++;
180
                }
181
            }
182
        } catch (Exception $e) {
183
            throw new Exception('Could not modify archive: '.$e->getMessage(), $e->getCode(), $e);
184
        }
185
        return $added_files;
186
    }
187
188
    /**
189
     * @param array  $files
190
     * @param string $archiveFileName
191
     *
192
     * @return false|int
193
     * @throws \Archive7z\Exception
194
     * @throws \Exception
195
     */
196
    public static function createArchive(array $files, $archiveFileName) {
197
        $seven_zip = new Archive7z($archiveFileName);
198
        try {
199
            foreach ($files as $localName => $filename) {
200
                if ($filename !== null) {
201
                    $seven_zip->addEntry($filename, true);
202
                    $seven_zip->renameEntry($filename, $localName);
203
                }
204
            }
205
            unset($seven_zip);
206
        } catch (Exception $e) {
207
            throw new Exception('Could not create archive: '.$e->getMessage(), $e->getCode(), $e);
208
        }
209
        return count($files);
210
    }
211
212
    /**
213
     * @return bool
214
     * @throws \Archive7z\Exception
215
     */
216
    public static function canCreateArchive()
217
    {
218
        return static::canAddFiles();
219
    }
220
221
    /**
222
     * @return bool
223
     * @throws \Archive7z\Exception
224
     */
225 1
    public static function canAddFiles()
226
    {
227 1
        $version = Archive7z::getBinaryVersion();
228 1
        return $version !== false && version_compare('9.30', $version, '<=');
229
    }
230
231
    /**
232
     * @return bool
233
     */
234 1
    public static function canDeleteFiles()
235
    {
236 1
        return true;
237
    }
238
}