Passed
Push — master ( 0d8a9b...48a836 )
by f
12:20
created

OneFileDriver::getInstallationInstruction()   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 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace wapmorgan\UnifiedArchive\Drivers\OneFile;
3
4
use wapmorgan\UnifiedArchive\ArchiveEntry;
5
use wapmorgan\UnifiedArchive\ArchiveInformation;
6
use wapmorgan\UnifiedArchive\Drivers\Basic\BasicDriver;
7
use wapmorgan\UnifiedArchive\Drivers\Basic\BasicExtensionDriver;
8
use wapmorgan\UnifiedArchive\Exceptions\ArchiveCreationException;
9
use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException;
10
use wapmorgan\UnifiedArchive\Exceptions\EmptyFileListException;
11
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException;
12
use wapmorgan\UnifiedArchive\Formats;
13
14
abstract class OneFileDriver extends BasicExtensionDriver
15
{
16
    /** @var null|string Should be filled for real format like 'gz' or other */
17
    const FORMAT = null;
18
19
    protected $fileName;
20
    protected $inArchiveFileName;
21
    protected $uncompressedSize;
22
    protected $modificationTime;
23
24
    public static function getSupportedFormats()
25
    {
26
        return [static::FORMAT];
27
    }
28
29
    /**
30
     * @param $format
31
     * @return array
32
     */
33
    public static function checkFormatSupport($format)
34
    {
35
        if (!static::isInstalled()) {
36
            return [];
37
        }
38
        switch ($format) {
39
            case static::FORMAT:
40
                return [BasicDriver::OPEN, BasicDriver::EXTRACT_CONTENT, BasicDriver::STREAM_CONTENT, BasicDriver::CREATE];
41
        }
42
    }
43
44
    /**
45
     * @inheritDoc
46
     * @throws UnsupportedOperationException
47
     * @throws \Exception
48
     */
49
    public function __construct($archiveFileName, $format, $password = null)
50
    {
51
        $suffix = Formats::getFormatExtension(static::FORMAT);
52
        if ($suffix === null) {
53
            throw new \Exception('Format suffix is empty for ' . static::FORMAT . ', it should be initialized!');
54
        }
55
        if ($password !== null) {
56
            throw new UnsupportedOperationException($suffix . ' archive does not support password!');
57
        }
58
59
        parent::__construct($archiveFileName, $format);
60
        $this->inArchiveFileName = basename($archiveFileName, '.'.static::FORMAT_SUFFIX);
0 ignored issues
show
Bug introduced by
The constant wapmorgan\UnifiedArchive...leDriver::FORMAT_SUFFIX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
61
    }
62
63
    /**
64
     * @return ArchiveInformation
65
     */
66
    public function getArchiveInformation()
67
    {
68
        $information = new ArchiveInformation();
69
        $information->compressedFilesSize = filesize($this->fileName);
70
        $information->uncompressedFilesSize = $this->uncompressedSize;
71
        $information->files[] = $this->inArchiveFileName;
72
        return $information;
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function getFileNames()
79
    {
80
        return [$this->inArchiveFileName];
81
    }
82
83
    /**
84
     * @param string $fileName
85
     *
86
     * @return bool
87
     */
88
    public function isFileExists($fileName)
89
    {
90
        return $fileName === $this->inArchiveFileName;
91
    }
92
93
    /**
94
     * @param string $fileName
95
     * @return ArchiveEntry|false
96
     */
97
    public function getFileData($fileName)
98
    {
99
        return new ArchiveEntry(
100
            $this->inArchiveFileName,
101
            filesize($this->fileName),
102
            $this->uncompressedSize,
103
            $this->modificationTime);
104
    }
105
106
    /**
107
     * @param string $outputFolder
108
     * @param array $files
109
     * @return int
110
     * @throws ArchiveExtractionException
111
     */
112
    public function extractFiles($outputFolder, array $files = null)
113
    {
114
        return $this->extractArchive($outputFolder);
115
    }
116
117
    /**
118
     * @param string $outputFolder
119
     * @return int
120
     * @throws ArchiveExtractionException
121
     */
122
    public function extractArchive($outputFolder)
123
    {
124
        $data = $this->getFileContent($this->inArchiveFileName);
125
        if ($data === false)
126
            throw new ArchiveExtractionException('Could not extract archive');
127
128
        $size = strlen($data);
129
        $written = file_put_contents($outputFolder.$this->inArchiveFileName, $data);
130
131
        if ($written === true) {
132
            throw new ArchiveExtractionException('Could not extract file "'.$this->inArchiveFileName.'": could not write data');
133
        } else if ($written < $size) {
134
            throw new ArchiveExtractionException('Could not archive file "'.$this->inArchiveFileName.'": written '.$written.' of '.$size);
135
        }
136
        return 1;
137
    }
138
139
    /**
140
     * @param array $files
141
     * @param string $archiveFileName
142
     * @param int $archiveFormat
143
     * @param int $compressionLevel
144
     * @param null $password
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $password is correct as it would always require null to be passed?
Loading history...
145
     * @param $fileProgressCallable
146
     * @return int
147
     * @throws ArchiveCreationException
148
     * @throws UnsupportedOperationException
149
     */
150
    public static function createArchive(
151
        array $files,
152
        $archiveFileName,
153
        $archiveFormat,
154
        $compressionLevel = self::COMPRESSION_AVERAGE,
155
        $password = null,
156
        $fileProgressCallable = null
157
    ) {
158
        if (count($files) > 1) {
159
            throw new UnsupportedOperationException('One-file format ('.__CLASS__.') could not archive few files');
160
        }
161
        if (empty($files)) {
162
            throw new EmptyFileListException();
163
        }
164
        if ($password !== null) {
0 ignored issues
show
introduced by
The condition $password !== null is always false.
Loading history...
165
            throw new UnsupportedOperationException('One-file format ('.__CLASS__.') could not encrypt an archive');
166
        }
167
168
        $filename = array_shift($files);
169
170
        $compressed_content = static::compressData(file_get_contents($filename), $compressionLevel);
171
        $size = strlen($compressed_content);
172
        $written = file_put_contents($archiveFileName, $compressed_content);
173
174
        if ($written === true) {
175
            throw new ArchiveCreationException('Could not archive file: could not write data');
176
        } else if ($written < $size) {
177
            throw new ArchiveCreationException('Could not archive file: written '.$written.' of '.$size);
178
        }
179
        return 1;
180
    }
181
182
    /**
183
     * @param string $data
184
     * @param int $compressionLevel
185
     * @return mixed
186
     * @throws UnsupportedOperationException
187
     */
188
    abstract protected static function compressData($data, $compressionLevel);
189
}
190