Passed
Push — 1.1.x ( 3d3019...6d4a67 )
by f
02:55 queued 01:04
created

Bzip   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 8.33%

Importance

Changes 0
Metric Value
wmc 10
eloc 23
c 0
b 0
f 0
dl 0
loc 90
ccs 2
cts 24
cp 0.0833
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getSupportedFormats() 0 4 1
A __construct() 0 4 1
A compressData() 0 12 1
A getDescription() 0 3 1
A getFileStream() 0 3 1
A checkFormatSupport() 0 5 2
A getInstallationInstruction() 0 5 2
A getFileContent() 0 3 1
1
<?php
2
namespace wapmorgan\UnifiedArchive\Drivers\OneFile;
3
4
use wapmorgan\UnifiedArchive\Formats;
5
use wapmorgan\UnifiedArchive\Drivers\OneFile\OneFileDriver;
6
7
class Bzip extends OneFileDriver
8
{
9
    const FORMAT_SUFFIX =  'bz2';
10
11
    /**
12
     * @return array
13
     */
14 1
    public static function getSupportedFormats()
15
    {
16
        return [
17 1
            Formats::BZIP,
18
        ];
19
    }
20
21
    /**
22
     * @param $format
23
     * @return bool
24
     */
25
    public static function checkFormatSupport($format)
26
    {
27
        switch ($format) {
28
            case Formats::BZIP:
29
                return extension_loaded('bz2');
30
        }
31
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36
    public static function getDescription()
37
    {
38
        return 'adapter for ext-bzip2';
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public static function getInstallationInstruction()
45
    {
46
        return !extension_loaded('bz2')
47
            ? 'install `bz2` extension'
48
            : null;
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function __construct($archiveFileName, $format, $password = null)
55
    {
56
        parent::__construct($archiveFileName, $password);
57
        $this->modificationTime = filemtime($this->fileName);
58
    }
59
60
    /**
61
     * @param string $fileName
62
     *
63
     * @return string|false
64
     */
65
    public function getFileContent($fileName = null)
66
    {
67
        return bzdecompress(file_get_contents($this->fileName));
68
    }
69
70
    /**
71
     * @param string $fileName
72
     *
73
     * @return bool|resource|string
74
     */
75
    public function getFileStream($fileName = null)
76
    {
77
        return bzopen($this->fileName, 'r');
78
    }
79
80
    /**
81
     * @param string $data
82
     * @param int $compressionLevel
83
     * @return mixed|string
84
     */
85
    protected static function compressData($data, $compressionLevel)
86
    {
87
        static $compressionLevelMap = [
88
            self::COMPRESSION_NONE => 1,
89
            self::COMPRESSION_WEAK => 2,
90
            self::COMPRESSION_AVERAGE => 4,
91
            self::COMPRESSION_STRONG => 7,
92
            self::COMPRESSION_MAXIMUM => 9,
93
        ];
94
        // it seems not working at all
95
        $work_factor = ($compressionLevelMap[$compressionLevel] * 28);
96
        return bzcompress($data, $compressionLevelMap[$compressionLevel], $work_factor);
97
    }
98
}