Passed
Push — master ( 0d8a9b...48a836 )
by f
12:20
created

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