Passed
Push — master ( 0d8a9b...48a836 )
by f
12:20
created

BasicDriver::addFileFromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
namespace wapmorgan\UnifiedArchive\Drivers\Basic;
3
4
use wapmorgan\UnifiedArchive\ArchiveEntry;
5
use wapmorgan\UnifiedArchive\ArchiveInformation;
6
use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException;
7
use wapmorgan\UnifiedArchive\Exceptions\ArchiveModificationException;
8
use wapmorgan\UnifiedArchive\Exceptions\NonExistentArchiveFileException;
9
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException;
10
use wapmorgan\UnifiedArchive\Formats;
11
12
abstract class BasicDriver
13
{
14
    const TYPE_EXTENSION = 1;
15
    const TYPE_UTILITIES = 2;
16
    const TYPE_PURE_PHP = 3;
17
18
    static $typeLabels = [
19
        BasicDriver::TYPE_EXTENSION => 'php extension',
20
        BasicDriver::TYPE_UTILITIES => 'utilities + php bridge',
21
        BasicDriver::TYPE_PURE_PHP => 'pure php',
22
    ];
23
24
    const COMPRESSION_NONE = 0;
25
    const COMPRESSION_WEAK = 1;
26
    const COMPRESSION_AVERAGE = 2;
27
    const COMPRESSION_STRONG = 3;
28
    const COMPRESSION_MAXIMUM = 4;
29
30
    const OPEN = 1;
31
    const OPEN_ENCRYPTED = 2;
32
    const OPEN_VOLUMED = 4;
33
34
    const GET_COMMENT = 64;
35
    const EXTRACT_CONTENT = 128;
36
    const STREAM_CONTENT = 256;
37
38
    const APPEND = 4096;
39
    const DELETE = 8192;
40
    const SET_COMMENT = 16384;
41
42
    const CREATE = 1048576;
43
    const CREATE_ENCRYPTED = 2097152;
44
    const CREATE_IN_STRING = 4194304;
45
46
    const TYPE = null;
47
48
    /**
49
     * @var string
50
     */
51
    protected $fileName;
52
53
    /**
54
     * @var string
55
     */
56
    protected $format;
57
58
    /**
59
     * @return string
60
     */
61
    abstract public static function getDescription();
62
63
    /**
64
     * @return bool
65
     */
66
    abstract public static function isInstalled();
67
68
    /**
69
     * @return string
70
     */
71
    abstract public static function getInstallationInstruction();
72
73
    /**
74
     * @return string[]
75
     */
76
    abstract public static function getSupportedFormats();
77
78
    /**
79
     * @param string $format
80
     * @return int[]
81
     */
82
    abstract public static function checkFormatSupport($format);
83
84
    /**
85
     * @param $ability
86
     * @return bool
87
     */
88
    public function checkAbility($ability)
89
    {
90
        return in_array($ability, static::checkFormatSupport($this->format), true);
91
    }
92
93
    /**
94
     * @param array $files
95
     * @param string $archiveFileName
96
     * @param string $archiveFormat
97
     * @param int $compressionLevel
98
     * @param null $password
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $password is correct as it would always require null to be passed?
Loading history...
99
     * @param $fileProgressCallable
100
     * @return int Number of archived files
101
     * @throws UnsupportedOperationException
102
     */
103
    public static function createArchive(
104
        array $files,
105
        $archiveFileName,
106
        $archiveFormat,
107
        $compressionLevel = self::COMPRESSION_AVERAGE,
108
        $password = null,
109
        $fileProgressCallable = null
110
    ) {
111
        throw new UnsupportedOperationException();
112
    }
113
114
    /**
115
     * @param array $files
116
     * @param string $archiveFormat
117
     * @param int $compressionLevel
118
     * @param null $password
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $password is correct as it would always require null to be passed?
Loading history...
119
     * @param $fileProgressCallable
120
     * @return string Content of archive
121
     * @throws UnsupportedOperationException
122
     */
123
    public static function createArchiveInString(
124
        array $files,
125
        $archiveFormat,
126
        $compressionLevel = self::COMPRESSION_AVERAGE,
127
        $password = null,
128
        $fileProgressCallable = null
129
    ) {
130
        $format_extension = Formats::getFormatExtension($archiveFormat);
131
        do {
132
            $temp_file = tempnam(sys_get_temp_dir(), 'temp_archive');
133
            unlink($temp_file);
134
            $archive_file =  $temp_file . '.' . $format_extension;
135
        } while (file_exists($archive_file));
136
        $created = static::createArchive($files, $archive_file, $archiveFormat, $compressionLevel, $password, $fileProgressCallable);
0 ignored issues
show
Unused Code introduced by
The assignment to $created is dead and can be removed.
Loading history...
137
        $string = file_get_contents($archive_file);
138
        unlink($archive_file);
139
        return $string;
140
    }
141
142
    /**
143
     * BasicDriver constructor.
144
     * @param string $format
145
     * @param string $archiveFileName
146
     * @param string|null $password Archive password for opening
147
     */
148
    public function __construct($archiveFileName, $format, $password = null)
149
    {
150
        $this->fileName = $archiveFileName;
151
        $this->format = $format;
152
    }
153
154
    /**
155
     * Returns summary about an archive.
156
     * Called after
157
     * - constructing
158
     * - addFiles()
159
     * - deleteFiles()
160
     * @return ArchiveInformation
161
     */
162
    abstract public function getArchiveInformation();
163
164
    /**
165
     * @return array
166
     */
167
    abstract public function getFileNames();
168
169
    /**
170
     * @param string $fileName
171
     * @return bool
172
     */
173
    abstract public function isFileExists($fileName);
174
175
    /**
176
     * @param string $fileName
177
     * @return ArchiveEntry|false
178
     */
179
    abstract public function getFileData($fileName);
180
181
    /**
182
     * @param string $fileName
183
     * @return string|false
184
     * @throws NonExistentArchiveFileException
185
     */
186
    abstract public function getFileContent($fileName);
187
188
    /**
189
     * @param string $fileName
190
     * @return resource
191
     */
192
    abstract public function getFileStream($fileName);
193
194
    /**
195
     * @param $string
196
     * @return resource
197
     */
198
    public static function wrapStringInStream($string)
199
    {
200
        $resource = fopen('php://temp', 'r+');
201
        fwrite($resource, $string);
202
        rewind($resource);
203
        return $resource;
204
    }
205
206
    /**
207
     * @param string $outputFolder
208
     * @param array  $files
209
     * @return int Number of extracted files
210
     * @throws ArchiveExtractionException
211
     */
212
    abstract public function extractFiles($outputFolder, array $files);
213
214
    /**
215
     * @param string $outputFolder
216
     * @return int Number of extracted files
217
     * @throws ArchiveExtractionException
218
     */
219
    abstract public function extractArchive($outputFolder);
220
221
    /**
222
     * @param array $files
223
     * @return false|int Number of deleted files
224
     * @throws UnsupportedOperationException
225
     * @throws ArchiveModificationException
226
     */
227
    public function deleteFiles(array $files)
228
    {
229
        throw new UnsupportedOperationException();
230
    }
231
232
    /**
233
     * @param array $files
234
     * @return int Number of added files
235
     * @throws UnsupportedOperationException
236
     * @throws ArchiveModificationException
237
     */
238
    public function addFiles(array $files)
239
    {
240
        throw new UnsupportedOperationException();
241
    }
242
243
    /**
244
     * @param string $inArchiveName
245
     * @param string $content
246
     * @return bool
247
     * @throws UnsupportedOperationException
248
     * @throws ArchiveModificationException
249
     */
250
    public function addFileFromString($inArchiveName, $content)
0 ignored issues
show
Unused Code introduced by
The parameter $content is not used and could be removed. ( Ignorable by Annotation )

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

250
    public function addFileFromString($inArchiveName, /** @scrutinizer ignore-unused */ $content)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $inArchiveName is not used and could be removed. ( Ignorable by Annotation )

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

250
    public function addFileFromString(/** @scrutinizer ignore-unused */ $inArchiveName, $content)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
251
    {
252
        throw new UnsupportedOperationException();
253
    }
254
255
    /**
256
     * @return string|null
257
     */
258
    public function getComment()
259
    {
260
        return null;
261
    }
262
263
    /**
264
     * @param string|null $comment
265
     * @return null
266
     * @throws UnsupportedOperationException
267
     */
268
    public function setComment($comment)
0 ignored issues
show
Unused Code introduced by
The parameter $comment is not used and could be removed. ( Ignorable by Annotation )

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

268
    public function setComment(/** @scrutinizer ignore-unused */ $comment)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
269
    {
270
        throw new UnsupportedOperationException();
271
    }
272
}
273