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

Cab::__destruct()   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 0
crap 2
1
<?php
2
namespace wapmorgan\UnifiedArchive\Drivers;
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\Drivers\BasicDriver;
9
use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException;
10
use wapmorgan\UnifiedArchive\Exceptions\ArchiveModificationException;
11
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException;
12
use wapmorgan\UnifiedArchive\Formats;
13
14
class Cab extends BasicDriver
15
{
16
    /** @var CabArchive */
17
    protected $cab;
18
19
    /**
20
     * @return array
21
     */
22 1
    public static function getSupportedFormats()
23
    {
24
        return [
25 1
            Formats::CAB,
26
        ];
27
    }
28
29
    /**
30
     * @param $format
31
     * @return bool
32
     */
33
    public static function checkFormatSupport($format)
34
    {
35
        switch ($format) {
36
            case Formats::CAB:
37
                return class_exists('\CabArchive');
38
        }
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public static function getDescription()
45
    {
46
        return 'php-library';
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public static function getInstallationInstruction()
53
    {
54
        return !class_exists('\CabArchive')
55
            ? 'install library `wapmorgan/cab-archive`'
56
            : null;
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function __construct($archiveFileName, $format, $password = null)
63
    {
64
        if ($password !== null)
65
            throw new UnsupportedOperationException('Cab archive does not support password!');
66
        $this->open($archiveFileName);
67
    }
68
69
    /**
70
     * Iso format destructor
71
     */
72
    public function __destruct()
73
    {
74
        $this->cab = null;
75
    }
76
77
    /**
78
     * @param $archiveFileName
79
     * @throws Exception
80
     */
81
    protected function open($archiveFileName)
82
    {
83
        try {
84
            $this->cab = new CabArchive($archiveFileName);
85
        } catch (Exception $e) {
86
            throw new Exception('Could not open Cab archive: '.$e->getMessage(), $e->getCode(), $e);
87
        }
88
    }
89
90
    /**
91
     * @return ArchiveInformation
92
     */
93
    public function getArchiveInformation()
94
    {
95
        $information = new ArchiveInformation();
96
        foreach ($this->cab->getFileNames() as $file) {
97
            $information->files[] = $file;
98
            $file_info = $this->cab->getFileData($file);
99
            $information->uncompressedFilesSize += $file_info->size;
100
            $information->compressedFilesSize += $file_info->packedSize;
101
        }
102
        return $information;
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public function getFileNames()
109
    {
110
        return $this->cab->getFileNames();
111
    }
112
113
    /**
114
     * @param string $fileName
115
     *
116
     * @return bool
117
     */
118
    public function isFileExists($fileName)
119
    {
120
        return in_array($fileName, $this->cab->getFileNames(), true);
121
    }
122
123
    /**
124
     * @param string $fileName
125
     *
126
     * @return ArchiveEntry|false
127
     */
128
    public function getFileData($fileName)
129
    {
130
        $data = $this->cab->getFileData($fileName);
131
132
        return new ArchiveEntry($fileName, $data->packedSize, $data->size, $data->unixtime, $data->is_compressed);
133
    }
134
135
    /**
136
     * @param string $fileName
137
     *
138
     * @return string|false
139
     * @throws Exception
140
     */
141
    public function getFileContent($fileName)
142
    {
143
        return $this->cab->getFileContent($fileName);
144
    }
145
146
    /**
147
     * @param string $fileName
148
     *
149
     * @return bool|resource|string
150
     * @throws Exception
151
     */
152
    public function getFileStream($fileName)
153
    {
154
        return self::wrapStringInStream($this->cab->getFileContent($fileName));
155
    }
156
157
    /**
158
     * @param string $outputFolder
159
     * @param array $files
160
     * @return int Number of extracted files
161
     * @throws ArchiveExtractionException
162
     */
163
    public function extractFiles($outputFolder, array $files)
164
    {
165
        try {
166
            return $this->cab->extract($outputFolder, $files);
167
        } catch (Exception $e) {
168
            throw new ArchiveExtractionException($e->getMessage(),
169
                $e->getCode(),
170
                $e->getPrevious()
171
            );
172
        }
173
    }
174
175
    /**
176
     * @param string $outputFolder
177
     * @return int
178
     * @throws ArchiveExtractionException
179
     */
180
    public function extractArchive($outputFolder)
181
    {
182
        try {
183
            return $this->cab->extract($outputFolder);
184
        } catch (Exception $e) {
185
            throw new ArchiveExtractionException($e->getMessage(),
186
                $e->getCode(),
187
                $e->getPrevious()
188
            );
189
        }
190
    }
191
192
    /**
193
     * @param string $inArchiveName
194
     * @param string $content
195
     * @return bool|void
196
     * @throws UnsupportedOperationException
197
     */
198
    public function addFileFromString($inArchiveName, $content)
199
    {
200
        throw new UnsupportedOperationException();
201
    }
202
}