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

Rar::isInstalled()   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 0
cp 0
crap 2
1
<?php
2
namespace wapmorgan\UnifiedArchive\Drivers;
3
4
use Exception;
5
use wapmorgan\UnifiedArchive\ArchiveEntry;
6
use wapmorgan\UnifiedArchive\ArchiveInformation;
7
use wapmorgan\UnifiedArchive\Drivers\BasicDriver;
8
use wapmorgan\UnifiedArchive\Exceptions\ArchiveModificationException;
9
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException;
10
use wapmorgan\UnifiedArchive\Formats;
11
12
class Rar extends BasicDriver
13
{
14
    const NONE_RAR_COMPRESSION = 48;
15
    const TYPE = self::TYPE_EXTENSION;
16
17
    /** @var \RarArchive */
18
    protected $rar;
19
20
    /**
21
     * @inheritDoc
22 1
     */
23
    public static function getDescription()
24
    {
25 1
        return 'adapter for ext-rar' . (self::isInstalled() ? ' (' . phpversion('rar') . ')' : null);
26
    }
27
28
    public static function isInstalled()
29
    {
30
        return extension_loaded('rar');
31
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36
    public static function getInstallationInstruction()
37
    {
38
        return 'install [rar] extension.' . "\n" . 'Can be installed with pecl: `pecl install rar`';
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public static function getSupportedFormats()
45
    {
46
        return [
47
            Formats::RAR,
48
        ];
49
    }
50
51
    /**
52
     * @param $format
53
     * @return array
54
     */
55
    public static function checkFormatSupport($format)
56
    {
57
        if (!extension_loaded('rar')) {
58
            return [];
59
        }
60
        switch ($format) {
61
            case Formats::RAR:
62
                return [
63
                    BasicDriver::OPEN,
64
                    BasicDriver::OPEN_ENCRYPTED,
65
                    BasicDriver::EXTRACT_CONTENT,
66
                    BasicDriver::STREAM_CONTENT,
67
                ];
68
        }
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public static function canStream($format)
0 ignored issues
show
Unused Code introduced by
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

74
    public static function canStream(/** @scrutinizer ignore-unused */ $format)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        return true;
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82
    public function __construct($archiveFileName, $format, $password = null)
83
    {
84
        \RarException::setUsingExceptions(true);
85
        $this->open($archiveFileName, $password);
86
    }
87
88
    /**
89
     * @param $archiveFileName
90
     * @param $password
91
     * @throws Exception
92
     */
93
    protected function open($archiveFileName, $password)
94
    {
95
        $this->rar = \RarArchive::open($archiveFileName, $password);
96
        if ($this->rar === false) {
97
            throw new Exception('Could not open Rar archive');
98
        }
99
    }
100
101
    /**
102
     * Rar format destructor
103
     */
104
    public function __destruct()
105
    {
106
        $this->rar->close();
107
    }
108
109
    /**
110
     * @return ArchiveInformation
111
     */
112
    public function getArchiveInformation()
113
    {
114
        $information = new ArchiveInformation();
115
        foreach ($this->rar->getEntries() as $i => $entry) {
116
            if ($entry->isDirectory()) continue;
117
            $information->files[] = $entry->getName();
118
            $information->compressedFilesSize += $entry->getPackedSize();
119
            $information->uncompressedFilesSize += $entry->getUnpackedSize();
120
        }
121
        return $information;
122
    }
123
124
    /**
125
     * @return string|null
126
     */
127
    public function getComment()
128
    {
129
        return $this->rar->getComment();
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function getFileNames()
136
    {
137
        $files = [];
138
        foreach ($this->rar->getEntries() as $i => $entry) {
139
            if ($entry->isDirectory()) continue;
140
            $files[] = $entry->getName();
141
        }
142
        return $files;
143
    }
144
145
    /**
146
     * @param string $fileName
147
     *
148
     * @return bool
149
     */
150
    public function isFileExists($fileName)
151
    {
152
        return $this->rar->getEntry($fileName) !== false;
153
    }
154
155
    /**
156
     * @param string $fileName
157
     *
158
     * @return ArchiveEntry|false
159
     */
160
    public function getFileData($fileName)
161
    {
162
        $entry = $this->rar->getEntry($fileName);
163
        return new ArchiveEntry($fileName, $entry->getPackedSize(), $entry->getUnpackedSize(),
164
            strtotime($entry->getFileTime()), $entry->getMethod() != self::NONE_RAR_COMPRESSION);
165
    }
166
167
    /**
168
     * @param string $fileName
169
     *
170
     * @return string|false
171
     */
172
    public function getFileContent($fileName)
173
    {
174
        $entry = $this->rar->getEntry($fileName);
175
        if ($entry->isDirectory()) return false;
176
        return stream_get_contents($entry->getStream());
177
    }
178
179
    /**
180
     * @param string $fileName
181
     *
182
     * @return bool|resource|string
183
     */
184
    public function getFileStream($fileName)
185
    {
186
        $entry = $this->rar->getEntry($fileName);
187
        if ($entry->isDirectory()) return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by wapmorgan\UnifiedArchive...Driver::getFileStream() of resource.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
188
        return $entry->getStream();
189
    }
190
191
    /**
192
     * @param string $outputFolder
193
     * @param array  $files
194
     *
195
     * @return false|int
196
     */
197
    public function extractFiles($outputFolder, array $files)
198
    {
199
        $count = 0;
200
        foreach ($files as $file) {
201
            if ($this->rar->getEntry($file)->extract($outputFolder)) {
202
                $count++;
203
            }
204
        }
205
        return $count;
206
    }
207
208
    /**
209
     * @param string $outputFolder
210
     *
211
     * @return false|resource
212
     */
213
    public function extractArchive($outputFolder)
214
    {
215
        return $this->extractFiles($outputFolder, $this->getFileNames());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->extractFil... $this->getFileNames()) returns the type integer which is incompatible with the documented return type false|resource.
Loading history...
216
    }
217
218
    /**
219
     * @param string $inArchiveName
220
     * @param string $content
221
     * @return bool|void
222
     * @throws UnsupportedOperationException
223
     */
224
    public function addFileFromString($inArchiveName, $content)
225
    {
226
        throw new UnsupportedOperationException();
227
    }
228
}