Passed
Push — 1.1.x ( 3d3019...6d4a67 )
by f
02:55 queued 01:04
created

Iso::addFileFromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
namespace wapmorgan\UnifiedArchive\Drivers;
3
4
use wapmorgan\UnifiedArchive\ArchiveEntry;
5
use wapmorgan\UnifiedArchive\ArchiveInformation;
6
use wapmorgan\UnifiedArchive\Drivers\BasicDriver;
7
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException;
8
use wapmorgan\UnifiedArchive\Formats;
9
10
class Iso extends BasicDriver
11
{
12
    /** @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...
13
    protected $iso;
14
15
    /** @var array List of files */
16
    protected $files = [];
17
18
    /** @var array  */
19
    protected $filesData = [];
20
21
    /** @var int */
22
    protected $filesSize = 0;
23
24
    /** @var null|int Size of block in ISO. Used to find real position of file in ISO */
25
    protected $blockSize;
26
27
    /**
28
     * @return array
29
     */
30 1
    public static function getSupportedFormats()
31
    {
32
        return [
33 1
            Formats::ISO,
34
        ];
35
    }
36
37
    /**
38
     * @param $format
39
     * @return bool
40
     */
41 1
    public static function checkFormatSupport($format)
42
    {
43
        switch ($format) {
44 1
            case Formats::ISO:
45 1
                return class_exists('\CISOFile');
46
        }
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public static function getDescription()
53
    {
54
        return 'php-library';
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public static function getInstallationInstruction()
61
    {
62
        return !class_exists('\CISOFile')
63
            ? 'install library `phpclasses/php-iso-file`'
64
            : null;
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70 3
    public function __construct($archiveFileName, $format, $password = null)
71
    {
72 3
        $this->open($archiveFileName);
73 3
        if ($password !== null)
74
            throw new UnsupportedOperationException('Iso archive does not support password!');
75 3
    }
76
77
    /**
78
     * Iso format destructor
79
     */
80 3
    public function __destruct()
81
    {
82 3
        $this->iso->close();
83 3
    }
84
85
    /**
86
     * @param $archiveFileName
87
     */
88 3
    protected function open($archiveFileName)
89
    {
90
        // load php-iso-files
91 3
        $this->iso = new \CISOFile;
92 3
        $this->iso->open($archiveFileName);
93 3
        $this->iso->ISOInit();
94
95
        /** @var \CVolumeDescriptor $usedDesc */
96 3
        $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...
97 3
        if (!$usedDesc)
0 ignored issues
show
introduced by
$usedDesc is of type CVolumeDescriptor, thus it always evaluated to true.
Loading history...
98
            $usedDesc = $this->iso->GetDescriptor(PRIMARY_VOLUME_DESC);
0 ignored issues
show
Bug introduced by
The constant wapmorgan\UnifiedArchive...ers\PRIMARY_VOLUME_DESC was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
99 3
        $this->blockSize = $usedDesc->iBlockSize;
100 3
        $directories = $usedDesc->LoadMPathTable($this->iso);
101
        // iterate over all directories
102
        /** @var \CPathTableRecord $Directory */
103 3
        foreach ($directories as $Directory) {
104 3
            $directory = $Directory->GetFullPath($directories);
105 3
            $directory = trim($directory, '/');
106 3
            if ($directory != '') {
107 3
                $directory .= '/';
108
//                $this->files[$Directory->Location] = $directory;
109
            }
110
//            $this->isoCatalogsStructure[$Directory->Location]
111
//                = $directory;
112
113
            /** @var \CFileDirDescriptors[] $files */
114 3
            $files = $Directory->LoadExtents($this->iso,
115 3
                $usedDesc->iBlockSize, true);
116 3
            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...
117
                /** @var \CFileDirDescriptors $file */
118 3
                foreach ($files as $file) {
119 3
                    if (in_array($file->strd_FileId, ['.', '..']) || $file->IsDirectory())
120 3
                        continue;
121 3
                    $this->files[$file->Location] = $directory.$file->strd_FileId;
122 3
                    $this->filesSize += $file->DataLen;
123
124 3
                    $this->filesData[$directory . $file->strd_FileId] =
125
                        [
126 3
                            'size' => $file->DataLen,
127
                            'mtime' =>
128 3
                                strtotime((string)$file->isoRecDate),
129
                        ];
130
                }
131
            }
132
            // break;
133
        }
134 3
    }
135
136
    /**
137
     * @return ArchiveInformation
138
     */
139 3
    public function getArchiveInformation()
140
    {
141 3
        $information = new ArchiveInformation();
142 3
        $information->files = array_values($this->files);
143 3
        $information->compressedFilesSize = $information->uncompressedFilesSize = $this->filesSize;
144 3
        return $information;
145
    }
146
147
    /**
148
     * @return array
149
     */
150
    public function getFileNames()
151
    {
152
        return array_values($this->files);
153
    }
154
155
    /**
156
     * @param string $fileName
157
     *
158
     * @return bool
159
     */
160
    public function isFileExists($fileName)
161
    {
162
        return array_key_exists($fileName, $this->filesData);
163
    }
164
165
    /**
166
     * @param string $fileName
167
     *
168
     * @return ArchiveEntry|false
169
     */
170 1
    public function getFileData($fileName)
171
    {
172 1
        if (!isset($this->filesData[$fileName]))
173
            return false;
174
175 1
        return new ArchiveEntry($fileName, $this->filesData[$fileName]['size'],
176 1
            $this->filesData[$fileName]['size'], $this->filesData[$fileName]['mtime'],false);
177
    }
178
179
    /**
180
     * @param string $fileName
181
     *
182
     * @return string|false
183
     */
184 1
    public function getFileContent($fileName)
185
    {
186 1
        $data = $this->prepareForFileExtracting($fileName);
187 1
        return $this->iso->Read($data['size']);
188
    }
189
190
    /**
191
     * @param string $fileName
192
     *
193
     * @return bool|resource|string
194
     */
195 1
    public function getFileStream($fileName)
196
    {
197 1
        $data = $this->prepareForFileExtracting($fileName);
198 1
        return self::wrapStringInStream($this->iso->Read($data['size']));
199
    }
200
201
    /**
202
     * @param string $fileName
203
     * @return array
204
     */
205 1
    protected function prepareForFileExtracting($fileName)
206
    {
207 1
        $Location = array_search($fileName, $this->files, true);
208 1
        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...
209 1
        $data = $this->filesData[$fileName];
210 1
        $Location_Real = $Location * $this->blockSize;
211 1
        if ($this->iso->Seek($Location_Real, SEEK_SET) === false)
212
            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...
213 1
        return $data;
214
    }
215
216
    /**
217
     * @param string $outputFolder
218
     * @param array $files
219
     * @return void
220
     * @throws UnsupportedOperationException
221
     * @todo Implement extracting with reading & writing to FS
222
     */
223
    public function extractFiles($outputFolder, array $files)
224
    {
225
        throw new UnsupportedOperationException();
226
    }
227
228
    /**
229
     * @param string $outputFolder
230
     * @return void
231
     * @throws UnsupportedOperationException
232
     * @todo Implement extracting with reading & writing to FS
233
     */
234
    public function extractArchive($outputFolder)
235
    {
236
        throw new UnsupportedOperationException();
237
    }
238
239
    /**
240
     * @param $inArchiveName
241
     * @param $content
242
     * @return void
243
     * @throws UnsupportedOperationException
244
     */
245
    public function addFileFromString($inArchiveName, $content)
246
    {
247
        throw new UnsupportedOperationException();
248
    }
249
}