Passed
Push — 1.1.x ( bc52f7...e8e75c )
by f
12:04
created

Gzip   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 6.45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 83
ccs 2
cts 31
cp 0.0645
rs 10
c 1
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 2
A gzipStat() 0 14 3
A compressData() 0 10 1
A getFileStream() 0 3 1
A getFileContent() 0 3 1
A __construct() 0 9 2
1
<?php
2
namespace wapmorgan\UnifiedArchive\Drivers\OneFile;
3
4
use Exception;
5
use wapmorgan\UnifiedArchive\Formats;
6
7
class Gzip extends OneFileDriver
8
{
9
    const FORMAT_SUFFIX = 'gz';
10
    const PHP_EXTENSION = 'zlib';
11
    const FORMAT = Formats::GZIP;
12
13
    /**
14
     * @inheritDoc
15 1
     */
16
    public static function getDescription()
17
    {
18 1
        return 'adapter for ext-zlib'.(defined('ZLIB_VERSION') ? ' ('.ZLIB_VERSION.')' : null);
19
    }
20
21
    /**
22
     * @param string $file GZipped file
23
     * @return array|false Array with 'mtime' and 'size' items
24
     */
25
    public static function gzipStat($file)
26
    {
27
        $fp = fopen($file, 'rb');
28
        if (filesize($file) < 18 || strcmp(fread($fp, 2), "\x1f\x8b")) {
29
            return false;  // Not GZIP format (See RFC 1952)
30
        }
31
        $method = fread($fp, 1);
0 ignored issues
show
Unused Code introduced by
The assignment to $method is dead and can be removed.
Loading history...
32
        $flags = fread($fp, 1);
0 ignored issues
show
Unused Code introduced by
The assignment to $flags is dead and can be removed.
Loading history...
33
        $stat = unpack('Vmtime', fread($fp, 4));
34
        fseek($fp, -4, SEEK_END);
35
        $stat += unpack('Vsize', fread($fp, 4));
36
        fclose($fp);
37
38
        return $stat;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function __construct($archiveFileName, $format, $password = null)
45
    {
46
        parent::__construct($archiveFileName, $format, $password);
47
        $stat = static::gzipStat($archiveFileName);
48
        if ($stat === false) {
49
            throw new Exception('Could not open Gzip file');
50
        }
51
        $this->uncompressedSize = $stat['size'];
52
        $this->modificationTime = $stat['mtime'];
53
    }
54
55
    /**
56
     * @param string $fileName
57
     *
58
     * @return string|false
59
     */
60
    public function getFileContent($fileName = null)
61
    {
62
        return gzdecode(file_get_contents($this->fileName));
63
    }
64
65
    /**
66
     * @param string $fileName
67
     *
68
     * @return bool|resource|string
69
     */
70
    public function getFileStream($fileName = null)
71
    {
72
        return gzopen($this->fileName, 'rb');
73
    }
74
75
    /**
76
     * @param $data
77
     * @param $compressionLevel
78
     * @return mixed|string
79
     */
80
    protected static function compressData($data, $compressionLevel)
81
    {
82
        static $compressionLevelMap = [
83
            self::COMPRESSION_NONE => 0,
84
            self::COMPRESSION_WEAK => 2,
85
            self::COMPRESSION_AVERAGE => 4,
86
            self::COMPRESSION_STRONG => 7,
87
            self::COMPRESSION_MAXIMUM => 9,
88
        ];
89
        return gzencode($data, $compressionLevelMap[$compressionLevel]);
90
    }
91
}
92