Passed
Push — master ( ee136d...e867c3 )
by f
21:14 queued 06:17
created

Iso::deleteFiles()   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 1
dl 0
loc 3
rs 10
1
<?php
2
namespace wapmorgan\UnifiedArchive\Formats;
3
4
use wapmorgan\UnifiedArchive\ArchiveEntry;
5
use wapmorgan\UnifiedArchive\ArchiveInformation;
6
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException;
7
use wapmorgan\UnifiedArchive\Formats;
8
9
class Iso extends BasicDriver
10
{
11
    /** @var \CISOFile */
0 ignored issues
show
Bug introduced by
The type CISOFile 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...
12
    protected $iso;
13
14
    /** @var array List of files */
15
    protected $files = [];
16
17
    /** @var array  */
18
    protected $filesData = [];
19
20
    /** @var int */
21
    protected $filesSize = 0;
22
23
    /** @var null|int Size of block in ISO. Used to find real position of file in ISO */
24
    protected $blockSize;
25
26
    /**
27
     * @return array
28
     */
29
    public static function getSupportedFormats()
30
    {
31
        return [
32
            Formats::ISO,
33
        ];
34
    }
35
36
    /**
37
     * @param $format
38
     * @return bool
39
     */
40
    public static function checkFormatSupport($format)
41
    {
42
        switch ($format) {
43
            case Formats::ISO:
44
                return class_exists('\CISOFile');
45
        }
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    public static function getDescription()
52
    {
53
        return 'php-library';
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    public static function getInstallationInstruction()
60
    {
61
        return !class_exists('\CISOFile')
62
            ? 'install library `phpclasses/php-iso-file`'
63
            : null;
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function __construct($archiveFileName, $format, $password = null)
70
    {
71
        $this->open($archiveFileName);
72
        if ($password !== null)
73
            throw new UnsupportedOperationException('Iso archive does not support password!');
74
    }
75
76
    /**
77
     * Iso format destructor
78
     */
79
    public function __destruct()
80
    {
81
        $this->iso->close();
82
    }
83
84
    /**
85
     * @param $archiveFileName
86
     */
87
    protected function open($archiveFileName)
88
    {
89
        // load php-iso-files
90
        $this->iso = new \CISOFile;
91
        $this->iso->open($archiveFileName);
92
        $this->iso->ISOInit();
93
94
        /** @var \CVolumeDescriptor $usedDesc */
95
        $usedDesc = $this->iso->GetDescriptor(SUPPLEMENTARY_VOLUME_DESC);
0 ignored issues
show
Bug introduced by
The constant wapmorgan\UnifiedArchive...PPLEMENTARY_VOLUME_DESC was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
96
        if (!$usedDesc)
0 ignored issues
show
introduced by
$usedDesc is of type CVolumeDescriptor, thus it always evaluated to true.
Loading history...
97
            $usedDesc = $this->iso->GetDescriptor(PRIMARY_VOLUME_DESC);
0 ignored issues
show
Bug introduced by
The constant wapmorgan\UnifiedArchive...ats\PRIMARY_VOLUME_DESC was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
98
        $this->blockSize = $usedDesc->iBlockSize;
99
        $directories = $usedDesc->LoadMPathTable($this->iso);
100
        // iterate over all directories
101
        /** @var \CPathTableRecord $Directory */
102
        foreach ($directories as $Directory) {
103
            $directory = $Directory->GetFullPath($directories);
104
            $directory = trim($directory, '/');
105
            if ($directory != '') {
106
                $directory .= '/';
107
//                $this->files[$Directory->Location] = $directory;
108
            }
109
//            $this->isoCatalogsStructure[$Directory->Location]
110
//                = $directory;
111
112
            /** @var \CFileDirDescriptors[] $files */
113
            $files = $Directory->LoadExtents($this->iso,
114
                $usedDesc->iBlockSize, true);
115
            if ($files) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $files of type CFileDirDescriptors[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
116
                /** @var \CFileDirDescriptors $file */
117
                foreach ($files as $file) {
118
                    if (in_array($file->strd_FileId, ['.', '..']) || $file->IsDirectory())
119
                        continue;
120
                    $this->files[$file->Location] = $directory.$file->strd_FileId;
121
                    $this->filesSize += $file->DataLen;
122
123
                    $this->filesData[$directory . $file->strd_FileId] =
124
                        [
125
                            'size' => $file->DataLen,
126
                            'mtime' =>
127
                                strtotime((string)$file->isoRecDate),
128
                        ];
129
                }
130
            }
131
            // break;
132
        }
133
    }
134
135
    /**
136
     * @return ArchiveInformation
137
     */
138
    public function getArchiveInformation()
139
    {
140
        $information = new ArchiveInformation();
141
        $information->files = array_values($this->files);
142
        $information->compressedFilesSize = $information->uncompressedFilesSize = $this->filesSize;
143
        return $information;
144
    }
145
146
    /**
147
     * @return array
148
     */
149
    public function getFileNames()
150
    {
151
        return array_values($this->files);
152
    }
153
154
    /**
155
     * @param string $fileName
156
     *
157
     * @return bool
158
     */
159
    public function isFileExists($fileName)
160
    {
161
        return array_key_exists($fileName, $this->filesData);
162
    }
163
164
    /**
165
     * @param string $fileName
166
     *
167
     * @return ArchiveEntry|false
168
     */
169
    public function getFileData($fileName)
170
    {
171
        if (!isset($this->filesData[$fileName]))
172
            return false;
173
174
        return new ArchiveEntry($fileName, $this->filesData[$fileName]['size'],
175
            $this->filesData[$fileName]['size'], $this->filesData[$fileName]['mtime'],false);
176
    }
177
178
    /**
179
     * @param string $fileName
180
     *
181
     * @return string|false
182
     */
183
    public function getFileContent($fileName)
184
    {
185
        $data = $this->prepareForFileExtracting($fileName);
186
        return $this->iso->Read($data['size']);
187
    }
188
189
    /**
190
     * @param string $fileName
191
     *
192
     * @return bool|resource|string
193
     */
194
    public function getFileStream($fileName)
195
    {
196
        $data = $this->prepareForFileExtracting($fileName);
197
        return self::wrapStringInStream($this->iso->Read($data['size']));
198
    }
199
200
    /**
201
     * @param string $fileName
202
     * @return array
203
     */
204
    protected function prepareForFileExtracting($fileName)
205
    {
206
        $Location = array_search($fileName, $this->files, true);
207
        if (!isset($this->filesData[$fileName])) return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
208
        $data = $this->filesData[$fileName];
209
        $Location_Real = $Location * $this->blockSize;
210
        if ($this->iso->Seek($Location_Real, SEEK_SET) === false)
211
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
212
        return $data;
213
    }
214
215
    /**
216
     * @param string $outputFolder
217
     * @param array $files
218
     * @return void
219
     * @throws UnsupportedOperationException
220
     * @todo Implement extracting with reading & writing to FS
221
     */
222
    public function extractFiles($outputFolder, array $files)
223
    {
224
        throw new UnsupportedOperationException();
225
    }
226
227
    /**
228
     * @param string $outputFolder
229
     * @return void
230
     * @throws UnsupportedOperationException
231
     * @todo Implement extracting with reading & writing to FS
232
     */
233
    public function extractArchive($outputFolder)
234
    {
235
        throw new UnsupportedOperationException();
236
    }
237
238
    /**
239
     * @param $inArchiveName
240
     * @param $content
241
     * @return void
242
     * @throws UnsupportedOperationException
243
     */
244
    public function addFileFromString($inArchiveName, $content)
245
    {
246
        throw new UnsupportedOperationException();
247
    }
248
}