Completed
Branch 0.1.x (de528d)
by f
03:18 queued 01:28
created

Iso::getFileContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 10
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 */
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 Size of block in ISO. Used to find real position of file in ISO */
23
    protected $blockSize;
24
25
    /**
26
     * BasicFormat constructor.
27
     *
28
     * @param string $archiveFileName
29
     */
30 3
    public function __construct($archiveFileName)
31
    {
32 3
        $this->open($archiveFileName);
33 3
    }
34
35
    /**
36
     * Iso format destructor
37
     */
38 3
    public function __destruct()
39
    {
40 3
        $this->iso->close();
41 3
    }
42
43
    /**
44
     * @param $archiveFileName
45
     */
46 3
    protected function open($archiveFileName)
47
    {
48
        // load php-iso-files
49 3
        $this->iso = new \CISOFile;
50 3
        $this->iso->open($archiveFileName);
51 3
        $this->iso->ISOInit();
52
53
        /** @var \CVolumeDescriptor $usedDesc */
54 3
        $usedDesc = $this->iso->GetDescriptor(SUPPLEMENTARY_VOLUME_DESC);
55 3
        if (!$usedDesc)
0 ignored issues
show
introduced by
$usedDesc is of type CVolumeDescriptor, thus it always evaluated to true.
Loading history...
56
            $usedDesc = $this->iso->GetDescriptor(PRIMARY_VOLUME_DESC);
57 3
        $this->blockSize = $usedDesc->iBlockSize;
58 3
        $directories = $usedDesc->LoadMPathTable($this->iso);
59
        // iterate over all directories
60
        /** @var \CPathTableRecord $Directory */
61 3
        foreach ($directories as $Directory) {
62 3
            $directory = $Directory->GetFullPath($directories);
63 3
            $directory = trim($directory, '/');
64 3
            if ($directory != '') {
65 3
                $directory .= '/';
66
//                $this->files[$Directory->Location] = $directory;
67
            }
68
//            $this->isoCatalogsStructure[$Directory->Location]
69
//                = $directory;
70
71
            /** @var \CFileDirDescriptors[] $files */
72 3
            $files = $Directory->LoadExtents($this->iso,
73 3
                $usedDesc->iBlockSize, true);
74 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...
75
                /** @var \CFileDirDescriptors $file */
76 3
                foreach ($files as $file) {
77 3
                    if (in_array($file->strd_FileId, ['.', '..']) || $file->IsDirectory())
78 3
                        continue;
79 3
                    $this->files[$file->Location] = $directory.$file->strd_FileId;
80 3
                    $this->filesSize += $file->DataLen;
81
82 3
                    $this->filesData[$directory . $file->strd_FileId] =
83
                        [
84 3
                            'size' => $file->DataLen,
85
                            'mtime' =>
86 3
                                strtotime((string)$file->isoRecDate),
87
                        ];
88
                }
89
            }
90
            // break;
91
        }
92 3
    }
93
94
    /**
95
     * @return ArchiveInformation
96
     */
97 3
    public function getArchiveInformation()
98
    {
99 3
        $information = new ArchiveInformation();
100 3
        $information->files = array_values($this->files);
101 3
        $information->compressedFilesSize = $information->uncompressedFilesSize = $this->filesSize;
102 3
        return $information;
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public function getFileNames()
109
    {
110
        return array_values($this->files);
111
    }
112
113
    /**
114
     * @param string $fileName
115
     *
116
     * @return bool
117
     */
118
    public function isFileExists($fileName)
119
    {
120
        return array_key_exists($fileName, $this->filesData);
121
    }
122
123
    /**
124
     * @param string $fileName
125
     *
126
     * @return ArchiveEntry|false
127
     */
128 1
    public function getFileData($fileName)
129
    {
130 1
        if (!isset($this->filesData[$fileName]))
131
            return false;
132
133 1
        return new ArchiveEntry($fileName, $this->filesData[$fileName]['size'],
134 1
            $this->filesData[$fileName]['size'], $this->filesData[$fileName]['mtime'],false);
135
    }
136
137
    /**
138
     * @param string $fileName
139
     *
140
     * @return string|false
141
     */
142 1
    public function getFileContent($fileName)
143
    {
144 1
        $Location = array_search($fileName, $this->files, true);
145 1
        if (!isset($this->filesData[$fileName])) return false;
146 1
        $data = $this->filesData[$fileName];
147 1
        $Location_Real = $Location * $this->blockSize;
148 1
        if ($this->iso->Seek($Location_Real, SEEK_SET) === false)
149
            return false;
150
151 1
        return $this->iso->Read($data['size']);
152
    }
153
154
    /**
155
     * @param string $fileName
156
     *
157
     * @return bool|resource|string
158
     */
159 1
    public function getFileResource($fileName)
160
    {
161 1
        $Location = array_search($fileName, $this->files, true);
162 1
        if (!isset($this->filesData[$fileName])) return false;
163 1
        $data = $this->filesData[$fileName];
164 1
        $Location_Real = $Location * $this->blockSize;
165 1
        if ($this->iso->Seek($Location_Real, SEEK_SET) === false)
166
            return false;
167
168 1
        $resource = fopen('php://temp', 'r+');
169 1
        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

169
        fwrite(/** @scrutinizer ignore-type */ $resource, $this->iso->Read($data['size']));
Loading history...
Bug introduced by
It seems like $this->iso->Read($data['size']) can also be of type false; however, parameter $string of fwrite() does only seem to accept string, 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

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

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