Passed
Push — master ( e9611b...a3681d )
by f
13:07
created

Cab   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Test Coverage

Coverage 3.45%

Importance

Changes 0
Metric Value
eloc 52
c 0
b 0
f 0
dl 0
loc 207
rs 10
ccs 2
cts 58
cp 0.0345
wmc 30

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getFileStream() 0 3 1
A addFileFromString() 0 3 1
A open() 0 6 2
A extractFiles() 0 8 2
A isInstalled() 0 3 1
A getSupportedFormats() 0 4 1
B checkFormatSupport() 0 20 9
A getFileNames() 0 3 1
A getInstallationInstruction() 0 3 1
A __destruct() 0 3 1
A getArchiveInformation() 0 10 2
A getFileContent() 0 3 1
A isFileExists() 0 3 1
A getFileData() 0 5 1
A __construct() 0 5 2
A extractArchive() 0 8 2
A getDescription() 0 3 1
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
    const TYPE = self::TYPE_PURE_PHP;
17
18
    /** @var CabArchive */
19
    protected $cab;
20
21
    /**
22 1
     * @inheritDoc
23
     */
24
    public static function getDescription()
25 1
    {
26
        return 'php-library';
27
    }
28
29
    public static function isInstalled()
30
    {
31
        return class_exists('\CabArchive');
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37
    public static function getInstallationInstruction()
38
    {
39
        return 'install library [wapmorgan/cab-archive]: `composer require wapmorgan/cab-archive`';
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public static function getSupportedFormats()
46
    {
47
        return [
48
            Formats::CAB,
49
        ];
50
    }
51
52
    /**
53
     * @param $format
54
     * @return bool
55
     */
56
    public static function checkFormatSupport($format)
57
    {
58
        if (!class_exists('\CabArchive')) {
59
            return [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array() returns the type array which is incompatible with the documented return type boolean.
Loading history...
60
61
        }
62
63
        switch ($format) {
64
            case Formats::CAB:
65
                $abilities = [
66
                    BasicDriver::OPEN,
67
                ];
68
69
                $parts = explode('.', PHP_VERSION);
70
                // not supported on versions below 7.0.22, 7.1.8, 7.2.0
71
                if ($parts[0] > 7 || $parts[1] >= 2 || (($parts[1] == 1 && $parts[2] >= 8) || ($parts[1] == 0 && $parts[2] >= 22))) {
72
                    $abilities[] = BasicDriver::EXTRACT_CONTENT;
73
                }
74
75
                return $abilities;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $abilities returns the type array<integer,integer> which is incompatible with the documented return type boolean.
Loading history...
76
        }
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82
    public function __construct($archiveFileName, $format, $password = null)
83
    {
84
        if ($password !== null)
85
            throw new UnsupportedOperationException('Cab archive does not support password!');
86
        $this->open($archiveFileName);
87
    }
88
89
    /**
90
     * Iso format destructor
91
     */
92
    public function __destruct()
93
    {
94
        $this->cab = null;
95
    }
96
97
    /**
98
     * @param $archiveFileName
99
     * @throws Exception
100
     */
101
    protected function open($archiveFileName)
102
    {
103
        try {
104
            $this->cab = new CabArchive($archiveFileName);
105
        } catch (Exception $e) {
106
            throw new Exception('Could not open Cab archive: '.$e->getMessage(), $e->getCode(), $e);
107
        }
108
    }
109
110
    /**
111
     * @return ArchiveInformation
112
     */
113
    public function getArchiveInformation()
114
    {
115
        $information = new ArchiveInformation();
116
        foreach ($this->cab->getFileNames() as $file) {
117
            $information->files[] = $file;
118
            $file_info = $this->cab->getFileData($file);
119
            $information->uncompressedFilesSize += $file_info->size;
120
            $information->compressedFilesSize += $file_info->packedSize;
121
        }
122
        return $information;
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    public function getFileNames()
129
    {
130
        return $this->cab->getFileNames();
131
    }
132
133
    /**
134
     * @param string $fileName
135
     *
136
     * @return bool
137
     */
138
    public function isFileExists($fileName)
139
    {
140
        return in_array($fileName, $this->cab->getFileNames(), true);
141
    }
142
143
    /**
144
     * @param string $fileName
145
     *
146
     * @return ArchiveEntry|false
147
     */
148
    public function getFileData($fileName)
149
    {
150
        $data = $this->cab->getFileData($fileName);
151
152
        return new ArchiveEntry($fileName, $data->packedSize, $data->size, $data->unixtime, $data->is_compressed);
153
    }
154
155
    /**
156
     * @param string $fileName
157
     *
158
     * @return string|false
159
     * @throws Exception
160
     */
161
    public function getFileContent($fileName)
162
    {
163
        return $this->cab->getFileContent($fileName);
164
    }
165
166
    /**
167
     * @param string $fileName
168
     *
169
     * @return bool|resource|string
170
     * @throws Exception
171
     */
172
    public function getFileStream($fileName)
173
    {
174
        return self::wrapStringInStream($this->cab->getFileContent($fileName));
175
    }
176
177
    /**
178
     * @param string $outputFolder
179
     * @param array $files
180
     * @return int Number of extracted files
181
     * @throws ArchiveExtractionException
182
     */
183
    public function extractFiles($outputFolder, array $files)
184
    {
185
        try {
186
            return $this->cab->extract($outputFolder, $files);
187
        } catch (Exception $e) {
188
            throw new ArchiveExtractionException($e->getMessage(),
189
                $e->getCode(),
190
                $e->getPrevious()
191
            );
192
        }
193
    }
194
195
    /**
196
     * @param string $outputFolder
197
     * @return int
198
     * @throws ArchiveExtractionException
199
     */
200
    public function extractArchive($outputFolder)
201
    {
202
        try {
203
            return $this->cab->extract($outputFolder);
204
        } catch (Exception $e) {
205
            throw new ArchiveExtractionException($e->getMessage(),
206
                $e->getCode(),
207
                $e->getPrevious()
208
            );
209
        }
210
    }
211
212
    /**
213
     * @param string $inArchiveName
214
     * @param string $content
215
     * @return bool|void
216
     * @throws UnsupportedOperationException
217
     */
218
    public function addFileFromString($inArchiveName, $content)
219
    {
220
        throw new UnsupportedOperationException();
221
    }
222
}