Passed
Push — master ( ee136d...e867c3 )
by f
21:14 queued 06:17
created

Cab::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

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