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

Rar::addFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
namespace wapmorgan\UnifiedArchive\Formats;
3
4
use Exception;
5
use wapmorgan\UnifiedArchive\ArchiveEntry;
6
use wapmorgan\UnifiedArchive\ArchiveInformation;
7
use wapmorgan\UnifiedArchive\Exceptions\ArchiveModificationException;
8
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException;
9
use wapmorgan\UnifiedArchive\Formats;
10
11
class Rar extends BasicDriver
12
{
13
    const NONE_RAR_COMPRESSION = 48;
14
15
    /** @var \RarArchive */
16
    protected $rar;
17
18
    /**
19
     * @return array
20
     */
21
    public static function getSupportedFormats()
22
    {
23
        return [
24
            Formats::RAR,
25
        ];
26
    }
27
28
    /**
29
     * @param $format
30
     * @return bool
31
     */
32
    public static function checkFormatSupport($format)
33
    {
34
        switch ($format) {
35
            case Formats::RAR:
36
                return extension_loaded('rar');
37
        }
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public static function getDescription()
44
    {
45
        return 'adapter for ext-rar';
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    public static function getInstallationInstruction()
52
    {
53
        return !extension_loaded('rar')
54
            ? 'install `rar` extension'
55
            : null;
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    public static function canStream($format)
62
    {
63
        return true;
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function __construct($archiveFileName, $format, $password = null)
70
    {
71
        \RarException::setUsingExceptions(true);
72
        $this->open($archiveFileName, $password);
73
    }
74
75
    /**
76
     * @param $archiveFileName
77
     * @param $password
78
     * @throws Exception
79
     */
80
    protected function open($archiveFileName, $password)
81
    {
82
        $this->rar = \RarArchive::open($archiveFileName, $password);
83
        if ($this->rar === false) {
84
            throw new Exception('Could not open Rar archive');
85
        }
86
    }
87
88
    /**
89
     * Rar format destructor
90
     */
91
    public function __destruct()
92
    {
93
        $this->rar->close();
94
    }
95
96
    /**
97
     * @return ArchiveInformation
98
     */
99
    public function getArchiveInformation()
100
    {
101
        $information = new ArchiveInformation();
102
        foreach ($this->rar->getEntries() as $i => $entry) {
103
            if ($entry->isDirectory()) continue;
104
            $information->files[] = $entry->getName();
105
            $information->compressedFilesSize += $entry->getPackedSize();
106
            $information->uncompressedFilesSize += $entry->getUnpackedSize();
107
        }
108
        return $information;
109
    }
110
111
    /**
112
     * @return array
113
     */
114
    public function getFileNames()
115
    {
116
        $files = [];
117
        foreach ($this->rar->getEntries() as $i => $entry) {
118
            if ($entry->isDirectory()) continue;
119
            $files[] = $entry->getName();
120
        }
121
        return $files;
122
    }
123
124
    /**
125
     * @param string $fileName
126
     *
127
     * @return bool
128
     */
129
    public function isFileExists($fileName)
130
    {
131
        return $this->rar->getEntry($fileName) !== false;
132
    }
133
134
    /**
135
     * @param string $fileName
136
     *
137
     * @return ArchiveEntry|false
138
     */
139
    public function getFileData($fileName)
140
    {
141
        $entry = $this->rar->getEntry($fileName);
142
        return new ArchiveEntry($fileName, $entry->getPackedSize(), $entry->getUnpackedSize(),
143
            strtotime($entry->getFileTime()), $entry->getMethod() != self::NONE_RAR_COMPRESSION);
144
    }
145
146
    /**
147
     * @param string $fileName
148
     *
149
     * @return string|false
150
     */
151
    public function getFileContent($fileName)
152
    {
153
        $entry = $this->rar->getEntry($fileName);
154
        if ($entry->isDirectory()) return false;
155
        return stream_get_contents($entry->getStream());
156
    }
157
158
    /**
159
     * @param string $fileName
160
     *
161
     * @return bool|resource|string
162
     */
163
    public function getFileStream($fileName)
164
    {
165
        $entry = $this->rar->getEntry($fileName);
166
        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...
167
        return $entry->getStream();
168
    }
169
170
    /**
171
     * @param string $outputFolder
172
     * @param array  $files
173
     *
174
     * @return false|int
175
     */
176
    public function extractFiles($outputFolder, array $files)
177
    {
178
        $count = 0;
179
        foreach ($files as $file) {
180
            if ($this->rar->getEntry($file)->extract($outputFolder)) {
181
                $count++;
182
            }
183
        }
184
        return $count;
185
    }
186
187
    /**
188
     * @param string $outputFolder
189
     *
190
     * @return false|resource
191
     */
192
    public function extractArchive($outputFolder)
193
    {
194
        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...
195
    }
196
197
    /**
198
     * @param string $inArchiveName
199
     * @param string $content
200
     * @return bool|void
201
     * @throws UnsupportedOperationException
202
     */
203
    public function addFileFromString($inArchiveName, $content)
204
    {
205
        throw new UnsupportedOperationException();
206
    }
207
}