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

Cab::isFileExists()   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
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace wapmorgan\UnifiedArchive\Formats;
3
4
use CabArchive;
0 ignored issues
show
Bug introduced by
The type CabArchive 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...
5
use Exception;
6
use wapmorgan\UnifiedArchive\ArchiveEntry;
7
use wapmorgan\UnifiedArchive\ArchiveInformation;
8
use wapmorgan\UnifiedArchive\UnsupportedOperationException;
9
10
class Cab extends BasicFormat
11
{
12
    /** @var CabArchive */
13
    protected $cab;
14
15
    /**
16
     * BasicFormat constructor.
17
     *
18
     * @param string $archiveFileName
19
     * @throws Exception
20
     */
21
    public function __construct($archiveFileName)
22
    {
23
        $this->open($archiveFileName);
24
    }
25
26
    /**
27
     * Iso format destructor
28
     */
29
    public function __destruct()
30
    {
31
        unset($this->cab);
32
    }
33
34
    /**
35
     * @param $archiveFileName
36
     * @throws Exception
37
     */
38
    protected function open($archiveFileName)
39
    {
40
        try {
41
            $this->cab = new CabArchive($archiveFileName);
42
        } catch (Exception $e) {
43
            throw new Exception('Could not open Cab archive: '.$e->getMessage(), $e->getCode(), $e);
44
        }
45
    }
46
47
    /**
48
     * @return ArchiveInformation
49
     */
50
    public function getArchiveInformation()
51
    {
52
        $information = new ArchiveInformation();
53
        foreach ($this->cab->getFileNames() as $file) {
54
            $information->files[] = $file;
55
            $file_info = $this->cab->getFileData($file);
56
            $information->uncompressedFilesSize += $file_info->size;
57
            $information->compressedFilesSize += $file_info->packedSize;
58
        }
59
        return $information;
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getFileNames()
66
    {
67
        return $this->cab->getFileNames();
68
    }
69
70
    /**
71
     * @param string $fileName
72
     *
73
     * @return bool
74
     */
75
    public function isFileExists($fileName)
76
    {
77
        return in_array($fileName, $this->cab->getFileNames(), true);
78
    }
79
80
    /**
81
     * @param string $fileName
82
     *
83
     * @return ArchiveEntry|false
84
     */
85
    public function getFileData($fileName)
86
    {
87
        $data = $this->cab->getFileData($fileName);
88
89
        return new ArchiveEntry($fileName, $data->packedSize, $data->size, $data->unixtime, $data->is_compressed);
90
    }
91
92
    /**
93
     * @param string $fileName
94
     *
95
     * @return string|false
96
     * @throws Exception
97
     */
98
    public function getFileContent($fileName)
99
    {
100
        return $this->cab->getFileContent($fileName);
101
    }
102
103
    /**
104
     * @param string $fileName
105
     *
106
     * @return bool|resource|string
107
     * @throws Exception
108
     */
109
    public function getFileResource($fileName)
110
    {
111
        $resource = fopen('php://temp', 'r+');
112
        fwrite($resource, $this->cab->getFileContent($fileName));
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

112
        fwrite(/** @scrutinizer ignore-type */ $resource, $this->cab->getFileContent($fileName));
Loading history...
113
        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

113
        rewind(/** @scrutinizer ignore-type */ $resource);
Loading history...
114
        return $resource;
115
    }
116
117
    /**
118
     * @param string $outputFolder
119
     * @param array $files
120
     *
121
     * @return false|resource
122
     * @throws UnsupportedOperationException
123
     * @throws Exception
124
     */
125
    public function extractFiles($outputFolder, array $files)
126
    {
127
        return $this->cab->extract($outputFolder, $files);
128
    }
129
130
    /**
131
     * @param string $outputFolder
132
     *
133
     * @return false|resource
134
     * @throws UnsupportedOperationException
135
     * @throws Exception
136
     */
137
    public function extractArchive($outputFolder)
138
    {
139
        return $this->cab->extract($outputFolder);
140
    }
141
142
    /**
143
     * @param array $files
144
     *
145
     * @return false|int
146
     * @throws UnsupportedOperationException
147
     */
148
    public function deleteFiles(array $files)
149
    {
150
        throw new UnsupportedOperationException();
151
    }
152
153
    /**
154
     * @param array $files
155
     *
156
     * @return false|int
157
     * @throws UnsupportedOperationException
158
     */
159
    public function addFiles(array $files)
160
    {
161
        throw new UnsupportedOperationException();
162
    }
163
164
    /**
165
     * @param array $files
166
     * @param string $archiveFileName
167
     *
168
     * @return false|int
169
     * @throws UnsupportedOperationException
170
     */
171
    public static function createArchive(array $files, $archiveFileName){
172
        throw new UnsupportedOperationException();
173
    }
174
}