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

OneFileDriver::createArchive()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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

174
    protected static function compressData(/** @scrutinizer ignore-unused */ $data, $compressionLevel)

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...
175
    {
176
        throw new UnsupportedOperationException();
177
    }
178
179
    /**
180
     * @inheritDoc
181
     */
182
    public static function canCreateArchive($format)
183
    {
184
        return true;
185
    }
186
187
    /**
188
     * @inheritDoc
189
     */
190
    public static function canStream($format)
191
    {
192
        return true;
193
    }
194
}