Passed
Push — 0.1.x ( 81ff64...72f342 )
by f
01:32
created

Iso   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 67
dl 0
loc 218
rs 10
c 0
b 0
f 0
wmc 25

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getArchiveInformation() 0 6 1
A deleteFiles() 0 3 1
A isFileExists() 0 3 1
A getFileData() 0 7 2
A extractArchive() 0 3 1
A __destruct() 0 3 1
A createArchive() 0 2 1
B open() 0 39 7
A getFileContent() 0 10 3
A getFileNames() 0 3 1
A extractFiles() 0 3 1
A getFileResource() 0 13 3
A addFiles() 0 3 1
A __construct() 0 3 1
1
<?php
2
namespace wapmorgan\UnifiedArchive\Formats;
3
4
use wapmorgan\UnifiedArchive\ArchiveEntry;
5
use wapmorgan\UnifiedArchive\ArchiveInformation;
6
use wapmorgan\UnifiedArchive\UnsupportedOperationException;
7
8
class Iso extends BasicFormat
9
{
10
    /** @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...
11
    protected $iso;
12
13
    /** @var array List of files */
14
    protected $files = [];
15
16
    /** @var array  */
17
    protected $filesData = [];
18
19
    /** @var int */
20
    protected $filesSize = 0;
21
22
    /** @var null|int */
23
    protected $blockSize;
24
25
    /**
26
     * BasicFormat constructor.
27
     *
28
     * @param string $archiveFileName
29
     */
30
    public function __construct($archiveFileName)
31
    {
32
        $this->open($archiveFileName);
33
    }
34
35
    /**
36
     * Iso format destructor
37
     */
38
    public function __destruct()
39
    {
40
        $this->iso->close();
41
    }
42
43
    /**
44
     * @param $archiveFileName
45
     */
46
    protected function open($archiveFileName)
47
    {
48
        // load php-iso-files
49
        $this->iso = new \CISOFile;
50
        $this->iso->open($archiveFileName);
51
        $this->iso->ISOInit();
52
53
        $usedDesc =
54
            $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...
55
        if (!$usedDesc)
56
            $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...
57
        $this->blockSize = $usedDesc->iBlockSize;
58
59
        $directories = $usedDesc->LoadMPathTable($this->iso);
60
        foreach ($directories as $Directory) {
61
            $directory = $Directory->GetFullPath($directories, false);
62
            $directory = trim($directory, '/');
63
            if ($directory != '') {
64
                $directory .= '/';
65
//                $this->files[$Directory->Location] = $directory;
66
            }
67
//            $this->isoCatalogsStructure[$Directory->Location]
68
//                = $directory;
69
70
            $files = $Directory->LoadExtents($this->iso,
71
                $usedDesc->iBlockSize, true);
72
            if ($files) {
73
                foreach ($files as $file) {
74
                    if (in_array($file->strd_FileId, ['.', '..']))
75
                        continue;
76
                    $this->files[$file->Location]
77
                        = $directory . $file->strd_FileId;
78
                    $this->filesSize += $file->DataLen;
79
80
                    $this->filesData[$directory . $file->strd_FileId] =
81
                        [
82
                            'size' => $file->DataLen,
83
                            'mtime' =>
84
                                strtotime((string)$file->isoRecDate),
85
                        ];
86
                }
87
            }
88
            // break;
89
        }
90
    }
91
92
    /**
93
     * @return ArchiveInformation
94
     */
95
    public function getArchiveInformation()
96
    {
97
        $information = new ArchiveInformation();
98
        $information->files = array_values($this->files);
99
        $information->compressedFilesSize = $information->uncompressedFilesSize = $this->filesSize;
100
        return $information;
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function getFileNames()
107
    {
108
        return array_values($this->files);
109
    }
110
111
    /**
112
     * @param string $fileName
113
     *
114
     * @return bool
115
     */
116
    public function isFileExists($fileName)
117
    {
118
        return array_key_exists($fileName, $this->filesData);
119
    }
120
121
    /**
122
     * @param string $fileName
123
     *
124
     * @return ArchiveEntry|false
125
     */
126
    public function getFileData($fileName)
127
    {
128
        if (!isset($this->filesData[$fileName]))
129
            return false;
130
131
        return new ArchiveEntry($fileName, $this->filesData[$fileName]['size'],
132
            $this->filesData[$fileName]['size'], $this->filesData[$fileName]['mtime'],false);
133
    }
134
135
    /**
136
     * @param string $fileName
137
     *
138
     * @return string|false
139
     */
140
    public function getFileContent($fileName)
141
    {
142
        $Location = array_search($fileName, $this->files, true);
143
        if (!isset($this->filesData[$fileName])) return false;
144
        $data = $this->filesData[$fileName];
145
        $Location_Real = $Location * $this->blockSize;
146
        if ($this->iso->Seek($Location_Real, SEEK_SET) === false)
147
            return false;
148
149
        return $this->iso->Read($data['size']);
150
    }
151
152
    /**
153
     * @param string $fileName
154
     *
155
     * @return bool|resource|string
156
     */
157
    public function getFileResource($fileName)
158
    {
159
        $Location = array_search($fileName, $this->files, true);
160
        if (!isset($this->filesData[$fileName])) return false;
161
        $data = $this->filesData[$fileName];
162
        $Location_Real = $Location * $this->blockSize;
163
        if ($this->iso->Seek($Location_Real, SEEK_SET) === false)
164
            return false;
165
166
        $resource = fopen('php://temp', 'r+');
167
        fwrite($resource, $this->iso->Read($data['size']));
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

167
        fwrite(/** @scrutinizer ignore-type */ $resource, $this->iso->Read($data['size']));
Loading history...
168
        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

168
        rewind(/** @scrutinizer ignore-type */ $resource);
Loading history...
169
        return $resource;
170
    }
171
172
    /**
173
     * @param string $outputFolder
174
     * @param array $files
175
     *
176
     * @return false|resource
177
     * @throws UnsupportedOperationException
178
     */
179
    public function extractFiles($outputFolder, array $files)
180
    {
181
        throw new UnsupportedOperationException();
182
    }
183
184
    /**
185
     * @param string $outputFolder
186
     *
187
     * @return false|resource
188
     * @throws UnsupportedOperationException
189
     */
190
    public function extractArchive($outputFolder)
191
    {
192
        throw new UnsupportedOperationException();
193
    }
194
195
    /**
196
     * @param array $files
197
     *
198
     * @return false|int
199
     * @throws UnsupportedOperationException
200
     */
201
    public function deleteFiles(array $files)
202
    {
203
        throw new UnsupportedOperationException();
204
    }
205
206
    /**
207
     * @param array $files
208
     *
209
     * @return false|int
210
     * @throws UnsupportedOperationException
211
     */
212
    public function addFiles(array $files)
213
    {
214
        throw new UnsupportedOperationException();
215
    }
216
217
    /**
218
     * @param array $files
219
     * @param string $archiveFileName
220
     *
221
     * @return false|int
222
     * @throws UnsupportedOperationException
223
     */
224
    public static function createArchive(array $files, $archiveFileName){
225
        throw new UnsupportedOperationException();
226
    }
227
}