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

Bzip::getInstallationInstruction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
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
}