Passed
Push — master ( bf29cf...6612ae )
by f
15:14
created

Cab::checkFormatSupport()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

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