Passed
Push — master ( 125b29...554a92 )
by f
27:06 queued 12:02
created

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