Passed
Push — master ( 48a836...671d99 )
by f
13:31
created

SplitbrainPhpArchive::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 9
rs 10
1
<?php
2
3
namespace wapmorgan\UnifiedArchive\Drivers;
4
5
use splitbrain\PHPArchive\ArchiveIllegalCompressionException;
0 ignored issues
show
Bug introduced by
The type splitbrain\PHPArchive\Ar...galCompressionException 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...
6
use splitbrain\PHPArchive\ArchiveIOException;
0 ignored issues
show
Bug introduced by
The type splitbrain\PHPArchive\ArchiveIOException 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...
7
use splitbrain\PHPArchive\FileInfo;
0 ignored issues
show
Bug introduced by
The type splitbrain\PHPArchive\FileInfo 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...
8
use splitbrain\PHPArchive\Tar;
0 ignored issues
show
Bug introduced by
The type splitbrain\PHPArchive\Tar 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...
9
use wapmorgan\UnifiedArchive\ArchiveEntry;
10
use wapmorgan\UnifiedArchive\ArchiveInformation;
11
use wapmorgan\UnifiedArchive\Drivers\Basic\BasicDriver;
12
use wapmorgan\UnifiedArchive\Drivers\Basic\BasicPureDriver;
13
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException;
14
use wapmorgan\UnifiedArchive\Formats;
15
16
class SplitbrainPhpArchive extends BasicPureDriver
17
{
18
    const PACKAGE_NAME = 'splitbrain/php-archive';
19
    const MAIN_CLASS = '\\splitbrain\\PHPArchive\\Archive';
20
21
    /**
22
     * @var \splitbrain\PHPArchive\Zip|Tar
0 ignored issues
show
Bug introduced by
The type splitbrain\PHPArchive\Zip 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...
23
     */
24
    protected $archive;
25
    /**
26
     * @var array
27
     */
28
    protected $files;
29
30
    /** @var FileInfo[] */
31
    protected $members;
32
33
    /**
34
     * @inheritDoc
35
     */
36
    public static function getDescription()
37
    {
38
        return 'php-library for zip/tar (with gzip/bzip-compression)';
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public static function getSupportedFormats()
45
    {
46
        return [
47
            Formats::ZIP,
48
            Formats::TAR,
49
            Formats::TAR_GZIP,
50
            Formats::TAR_BZIP,
51
        ];
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public static function checkFormatSupport($format)
58
    {
59
        if (!static::isInstalled()) {
60
            return [];
61
        }
62
63
        if (
64
            ($format === Formats::TAR_BZIP && !extension_loaded('bzip2'))
65
            || ($format === Formats::TAR_GZIP && !extension_loaded('zlib'))
66
        ) {
67
            return [];
68
        }
69
70
        switch ($format) {
71
            case Formats::ZIP:
72
            case Formats::TAR:
73
            case Formats::TAR_GZIP;
74
            case Formats::TAR_BZIP;
75
                return [
76
                    BasicDriver::OPEN,
77
//                    BasicDriver::EXTRACT_CONTENT,
78
                    BasicDriver::APPEND,
79
                    BasicDriver::CREATE,
80
                    BasicDriver::CREATE_ENCRYPTED,
81
                    BasicDriver::CREATE_IN_STRING,
82
                ];
83
        }
84
    }
85
86
    /**
87
     * @throws ArchiveIllegalCompressionException
88
     * @throws ArchiveIOException
89
     */
90
    public function __construct($archiveFileName, $format, $password = null)
91
    {
92
        parent::__construct($archiveFileName, $format, $password);
93
        if ($format === Formats::ZIP) {
94
            $this->archive = new \splitbrain\PHPArchive\Zip();
95
        } else {
96
            $this->archive = new Tar();
97
        }
98
        $this->archive->open($archiveFileName);
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104
    public function getArchiveInformation()
105
    {
106
        $this->files = [];
107
        $information = new ArchiveInformation();
108
109
        foreach ($this->archive->contents() as $member) {
110
            if ($member->getIsdir()) {
111
                continue;
112
            }
113
114
            $this->files[] = $information->files[] = str_replace('\\', '/', $member->getPath());
115
            $this->members[str_replace('\\', '/', $member->getPath())] = $member;
116
            $information->compressedFilesSize += (int)$member->getCompressedSize();
117
            $information->uncompressedFilesSize += (int)$member->getSize();
118
        }
119
        return $information;
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125
    public function getFileNames()
126
    {
127
        return $this->files;
128
    }
129
130
    /**
131
     * @inheritDoc
132
     */
133
    public function isFileExists($fileName)
134
    {
135
        return array_key_exists($fileName, $this->members);
136
    }
137
138
    /**
139
     * @inheritDoc
140
     */
141
    public function getFileData($fileName)
142
    {
143
        $entry = $this->members[$fileName];
144
        return new ArchiveEntry(
145
            $fileName,
146
            $entry->getCompressedSize(),
147
            $entry->getSize(),
148
            strtotime($entry->getMtime()),
149
            $entry->getSize() !== $entry->getCompressedSize(),
150
            $entry->getComment(),
151
            null
152
        );
153
    }
154
155
    /**
156
     * @inheritDoc
157
     * @throws UnsupportedOperationException
158
     */
159
    public function getFileContent($fileName)
160
    {
161
        throw new UnsupportedOperationException('Getting file content is not supported by ' . __CLASS__);
162
    }
163
164
    /**
165
     * @inheritDoc
166
     * @throws UnsupportedOperationException
167
     */
168
    public function getFileStream($fileName)
169
    {
170
        throw new UnsupportedOperationException('Getting file stream is not supported by ' . __CLASS__);
171
    }
172
173
    /**
174
     * @inheritDoc
175
     * @throws UnsupportedOperationException
176
     */
177
    public function extractFiles($outputFolder, array $files)
178
    {
179
        throw new UnsupportedOperationException('Extract specific files is not supported by ' . __CLASS__);
180
    }
181
182
    /**
183
     * @inheritDoc
184
     */
185
    public function extractArchive($outputFolder)
186
    {
187
        $this->archive->extract($outputFolder);
188
    }
189
}
190